2
Most read
3
Most read
7
Most read
Sorted Tree
Dictionary
Logic Programming - Fall 12




   Benjamin GUILLET
What's the matter?
● We want to build a index (dictionary) of
  performances of horses

● Let's build a database!
                     winnings(abaris, 582).
                     winnings(careful,17).
                     winnings(jingling_silver, 300).
                     winnings(maloja, 356).


 ● It's slow! O(n)
Solution

● Create a Sorted Tree Dictionary
● Basically: a Binary Search Tree (BST).




● Search: O(log n)
Implementation: w(H, W, L, G).
              w(massinga, 858, , ).


 w(braemar,385, , _).         w(panorama, 158, , _).


w(adela, 588, _, _).        w(nettleweed, 579, _, _ ).

                                          w(massinga, 858,
                                             w(braemar,385,
                                                  w(adela, 588,_,_),
                                             _),
                                             w(panorama, 158,
                                                  w(nettleweed, 579,_,_),
                                             _)
                                          ).
Implementation: lookup
 lookup(H, w(H, G, _, _), G1) :- !, G1=G.

 lookup(H, w(H1, _, Before, _), G) :-
    H @< H1,
    lookup(H, Before, G).

 lookup(H, w(H1, _, _, After), G) :-
    H @> H1,
    lookup(H,After,G).
Experiment (1)
lookup(massinga, X, 858), lookup(braemar, X, 385), lookup(nettleweed, X,
579), lookup(panorama, X, 158).

w(massinga,858,
   w(braemar,385,_,_),
   w(nettleweed,579,_,
     w(panorama,158,_,_)
   )                                   w(massinga, 858, , ).
).
                                                    w(nettleweed, 579, , _ ).

                           w(braemar,385, _, _).
                                                     w(panorama, 158, _, _).
Experiment (2)
lookup(adela, X, 588), lookup(braemar, X, 385), lookup(nettleweed, X, 579),
lookup(massinga, X, 858).

X = w(adela,588,_,w(braemar,385,_,w(nettleweed,579,w(massinga,858,_,_),
_))) ?

w(adela,588,_,
  w(braemar,385,_,
                                       w(adela, 588, _, ).
    w(nettleweed,579,
       w(massinga,858,_,_),
    _)                                          w(braemar,385, _, ).
  )
)                                                     w(nettleweed, 579, , _ ).


                                  w(massinga, 858, _, _).
Extension: Is it a heap?!
heap:
● Shape property: almost complete binary tree
● Heap property: each node is greater than or equal to each of its children
Extension (2)
| ?- heap1(node(5, node(3, empty, empty), node(4, empty,
empty))).

1. The empty tree is a binary heap.
2. For node(K, L, R), it is a heap if:
   a. L and R are heaps.
   b. If L and/or R are not empty, then their key is less
      than or equal to K.
   c. If L has depth d, then R has depth d or d - 1.
Extension (3)
heap1(H) :- heap1(H, _).

heap1(empty, 0).

heap1(node(K, L, R), H) :-
 heap1(L, LH), heap1(R, RH),
 (L = node(LK, _, _) *-> K @>= LK; true),
 (R = node(RK, _, _) *-> K @>= RK; true),
 (LH is RH; LH is RH + 1), H is LH + 1.
Extension (4)
|?- heap1(node(5, node(4, node(6,empty,empty),
empty), node(3,empty,empty))).
Thank you!

More Related Content

PDF
Asp.Net Core MVC , Razor page , Entity Framework Core
PPT
Java servlet life cycle - methods ppt
PDF
Lecture 3: Servlets - Session Management
PPTX
jQuery Fundamentals
PPTX
Exception handling
PPT
PDF
JavaScript - Chapter 4 - Types and Statements
PPTX
2CPP14 - Abstraction
Asp.Net Core MVC , Razor page , Entity Framework Core
Java servlet life cycle - methods ppt
Lecture 3: Servlets - Session Management
jQuery Fundamentals
Exception handling
JavaScript - Chapter 4 - Types and Statements
2CPP14 - Abstraction

What's hot (20)

PPT
SQLITE Android
PPTX
Introduction to Node.js
PDF
JUnit & Mockito, first steps
PDF
Basic Java Programming
PDF
GPT-2: Language Models are Unsupervised Multitask Learners
PPTX
Golang - Overview of Go (golang) Language
PDF
Unit testing with JUnit
PDF
JSP Components and Directives.pdf
PPT
Python GUI Programming
PDF
Trends and future of C++: Evolving a systems language for performance - by Bj...
PPT
C# Exceptions Handling
PDF
Mocking in Java with Mockito
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Write microservice in golang
PDF
Unit II - 2 - Operating System - Threads
PDF
Python sqlite3
PPTX
Mutation Testing: Testing your tests
PPT
security configuration
PPTX
Advanced Security With GeoServer
PPTX
A Simple Introduction to Word Embeddings
SQLITE Android
Introduction to Node.js
JUnit & Mockito, first steps
Basic Java Programming
GPT-2: Language Models are Unsupervised Multitask Learners
Golang - Overview of Go (golang) Language
Unit testing with JUnit
JSP Components and Directives.pdf
Python GUI Programming
Trends and future of C++: Evolving a systems language for performance - by Bj...
C# Exceptions Handling
Mocking in Java with Mockito
[OOP - Lec 19] Static Member Functions
Write microservice in golang
Unit II - 2 - Operating System - Threads
Python sqlite3
Mutation Testing: Testing your tests
security configuration
Advanced Security With GeoServer
A Simple Introduction to Word Embeddings
Ad

Similar to Binary Search Tree and Heap in Prolog (20)

PDF
Mlab2012 tabei 20120806
PDF
Dmss2011 public
PDF
WABI2012-SuccinctMultibitTree
PDF
Gwt sdm public
PPTX
Succinct Data Structure for Analyzing Document Collection
PPT
Dic hash
PDF
Traversing on Algebraic Datatype
PDF
Red Black Trees
PDF
32 -longest-common-prefix
PDF
Refined types (FP-Syd)
PDF
Gwt presen alsip-20111201
PPTX
Week 4
PPT
15-btrees.ppt
PPT
Data structures
PPT
Class1
PDF
Sp 3828 10.1007-2_f978-3-642-31265-6_20
PDF
Haskell in the Real World
PPT
chap09alg.ppt for string matching algorithm
Mlab2012 tabei 20120806
Dmss2011 public
WABI2012-SuccinctMultibitTree
Gwt sdm public
Succinct Data Structure for Analyzing Document Collection
Dic hash
Traversing on Algebraic Datatype
Red Black Trees
32 -longest-common-prefix
Refined types (FP-Syd)
Gwt presen alsip-20111201
Week 4
15-btrees.ppt
Data structures
Class1
Sp 3828 10.1007-2_f978-3-642-31265-6_20
Haskell in the Real World
chap09alg.ppt for string matching algorithm
Ad

Binary Search Tree and Heap in Prolog

  • 1. Sorted Tree Dictionary Logic Programming - Fall 12 Benjamin GUILLET
  • 2. What's the matter? ● We want to build a index (dictionary) of performances of horses ● Let's build a database! winnings(abaris, 582). winnings(careful,17). winnings(jingling_silver, 300). winnings(maloja, 356). ● It's slow! O(n)
  • 3. Solution ● Create a Sorted Tree Dictionary ● Basically: a Binary Search Tree (BST). ● Search: O(log n)
  • 4. Implementation: w(H, W, L, G). w(massinga, 858, , ). w(braemar,385, , _). w(panorama, 158, , _). w(adela, 588, _, _). w(nettleweed, 579, _, _ ). w(massinga, 858, w(braemar,385, w(adela, 588,_,_), _), w(panorama, 158, w(nettleweed, 579,_,_), _) ).
  • 5. Implementation: lookup lookup(H, w(H, G, _, _), G1) :- !, G1=G. lookup(H, w(H1, _, Before, _), G) :- H @< H1, lookup(H, Before, G). lookup(H, w(H1, _, _, After), G) :- H @> H1, lookup(H,After,G).
  • 6. Experiment (1) lookup(massinga, X, 858), lookup(braemar, X, 385), lookup(nettleweed, X, 579), lookup(panorama, X, 158). w(massinga,858, w(braemar,385,_,_), w(nettleweed,579,_, w(panorama,158,_,_) ) w(massinga, 858, , ). ). w(nettleweed, 579, , _ ). w(braemar,385, _, _). w(panorama, 158, _, _).
  • 7. Experiment (2) lookup(adela, X, 588), lookup(braemar, X, 385), lookup(nettleweed, X, 579), lookup(massinga, X, 858). X = w(adela,588,_,w(braemar,385,_,w(nettleweed,579,w(massinga,858,_,_), _))) ? w(adela,588,_, w(braemar,385,_, w(adela, 588, _, ). w(nettleweed,579, w(massinga,858,_,_), _) w(braemar,385, _, ). ) ) w(nettleweed, 579, , _ ). w(massinga, 858, _, _).
  • 8. Extension: Is it a heap?! heap: ● Shape property: almost complete binary tree ● Heap property: each node is greater than or equal to each of its children
  • 9. Extension (2) | ?- heap1(node(5, node(3, empty, empty), node(4, empty, empty))). 1. The empty tree is a binary heap. 2. For node(K, L, R), it is a heap if: a. L and R are heaps. b. If L and/or R are not empty, then their key is less than or equal to K. c. If L has depth d, then R has depth d or d - 1.
  • 10. Extension (3) heap1(H) :- heap1(H, _). heap1(empty, 0). heap1(node(K, L, R), H) :- heap1(L, LH), heap1(R, RH), (L = node(LK, _, _) *-> K @>= LK; true), (R = node(RK, _, _) *-> K @>= RK; true), (LH is RH; LH is RH + 1), H is LH + 1.
  • 11. Extension (4) |?- heap1(node(5, node(4, node(6,empty,empty), empty), node(3,empty,empty))).