SlideShare a Scribd company logo
2
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

What's hot (20)

PPT
Amortized analysis
ajmalcs
 
PPT
Constructor and destructor in C++
Lovely Professional University
 
PPTX
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Simplilearn
 
PDF
Daa notes 1
smruti sarangi
 
PPTX
Semantic analysis
Lahore Garrison University
 
PPT
1.7 data reduction
Krish_ver2
 
PDF
All pairs shortest path algorithm
Srikrishnan Suresh
 
PPTX
Boyer moore algorithm
AYESHA JAVED
 
PDF
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
PPTX
OPTIMAL BINARY SEARCH
Cool Guy
 
PPTX
Role-of-lexical-analysis
Dattatray Gandhmal
 
PPTX
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
PPTX
AI-06 Search Techniques - Informed.pptx
Pankaj Debbarma
 
PPTX
Divide and Conquer - Part 1
Amrinder Arora
 
PPTX
Symbol Table
Akhil Kaushik
 
PDF
Code generation in Compiler Design
Kuppusamy P
 
PPTX
Training algorithms for Neural Networks
Mrinmoy Majumder
 
PDF
Sorting
Zaid Shabbir
 
PDF
String matching, naive,
Amit Kumar Rathi
 
PPTX
Introdution and designing a learning system
swapnac12
 
Amortized analysis
ajmalcs
 
Constructor and destructor in C++
Lovely Professional University
 
Support Vector Machine - How Support Vector Machine works | SVM in Machine Le...
Simplilearn
 
Daa notes 1
smruti sarangi
 
Semantic analysis
Lahore Garrison University
 
1.7 data reduction
Krish_ver2
 
All pairs shortest path algorithm
Srikrishnan Suresh
 
Boyer moore algorithm
AYESHA JAVED
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
OPTIMAL BINARY SEARCH
Cool Guy
 
Role-of-lexical-analysis
Dattatray Gandhmal
 
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
AI-06 Search Techniques - Informed.pptx
Pankaj Debbarma
 
Divide and Conquer - Part 1
Amrinder Arora
 
Symbol Table
Akhil Kaushik
 
Code generation in Compiler Design
Kuppusamy P
 
Training algorithms for Neural Networks
Mrinmoy Majumder
 
Sorting
Zaid Shabbir
 
String matching, naive,
Amit Kumar Rathi
 
Introdution and designing a learning system
swapnac12
 

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

PDF
Mlab2012 tabei 20120806
Yasuo Tabei
 
PDF
Dmss2011 public
Yasuo Tabei
 
PDF
WABI2012-SuccinctMultibitTree
Yasuo Tabei
 
PDF
Gwt sdm public
Yasuo Tabei
 
PPTX
Succinct Data Structure for Analyzing Document Collection
Preferred Networks
 
PPT
Dic hash
ppreeta
 
PDF
Traversing on Algebraic Datatype
Yun-Yan Chi
 
PDF
Red Black Trees
Varun Mahajan
 
PDF
32 -longest-common-prefix
Sanjeev Gupta
 
PDF
Refined types (FP-Syd)
Dom De Re
 
PDF
Gwt presen alsip-20111201
Yasuo Tabei
 
PPTX
Week 4
a_akhavan
 
PPT
15-btrees.ppt
plagcheck
 
PPT
Data structures
IIUI
 
PPT
Class1
issbp
 
PDF
Sp 3828 10.1007-2_f978-3-642-31265-6_20
Ali Akbar Dadjouyan
 
PDF
Haskell in the Real World
osfameron
 
PPT
chap09alg.ppt for string matching algorithm
SadiaSharmin40
 
Mlab2012 tabei 20120806
Yasuo Tabei
 
Dmss2011 public
Yasuo Tabei
 
WABI2012-SuccinctMultibitTree
Yasuo Tabei
 
Gwt sdm public
Yasuo Tabei
 
Succinct Data Structure for Analyzing Document Collection
Preferred Networks
 
Dic hash
ppreeta
 
Traversing on Algebraic Datatype
Yun-Yan Chi
 
Red Black Trees
Varun Mahajan
 
32 -longest-common-prefix
Sanjeev Gupta
 
Refined types (FP-Syd)
Dom De Re
 
Gwt presen alsip-20111201
Yasuo Tabei
 
Week 4
a_akhavan
 
15-btrees.ppt
plagcheck
 
Data structures
IIUI
 
Class1
issbp
 
Sp 3828 10.1007-2_f978-3-642-31265-6_20
Ali Akbar Dadjouyan
 
Haskell in the Real World
osfameron
 
chap09alg.ppt for string matching algorithm
SadiaSharmin40
 
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))).