Binary search trees
Dictionary ADT Binary search tree properties Search Insertion
February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1
Binary search trees Dictionary ADT Binary search tree properties - - PowerPoint PPT Presentation
Binary search trees Dictionary ADT Binary search tree properties Search Insertion February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1 By the way... Here is a reference to the "three dots" tree traversal shown in
February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1
– https://en.wikibooks.org/wiki/A- level_Computing/AQA/Paper_1/Fundamentals_of_algorithms/Tree_tra versal – Trace around the tree counter-clockwise – Visit a node when you hit one of its markers for pre-, post-, or in-order
February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 2
– Values may be any (homogenous) type – Keys may be any (homogenous) comparable type
– Create – Destroy – Insert – Find – Remove
February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 3
Dictionary ADT
commuter
Insert
presumably
Find(Z125 Pro)
February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 4
Naïve implementations, complexity
– For all nodes in the tree:
root
the subtree's root
Cinda Heeren / Will Evans / Geoffrey Tien 5 February 04, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 6
17 13 27 9 16 20 39 11
February 04, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 7
visit(nd);
5 3 1 2 4 7 6 8
inOrder(nd->leftchild); inOrder(nd->rightchild);
visit inOrder(left) inOrder(right)
17 13 9 11 16 27 20 39
visit inOrder(left) inOrder(right) visit inOrder(left) inOrder(right) visit inOrder(left) inOrder(right) visit inOrder(left) inOrder(right) visit inOrder(left) inOrder(right) visit inOrder(left) inOrder(right)
inOrder traversal on a BST retrieves data in sorted order
February 04, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 8
data Node* rightchild Node* leftchild Data to be stored in the tree (usually an object) References or pointers to other tree Nodes
February 04, 2019
– If the target is less than the value in the node search its left subtree – If the target is greater than the value in the node search its right subtree – Otherwise return true, (or a pointer to the data, or …)
– One for each node on the path – Worst case: height of the tree + 1
Cinda Heeren / Will Evans / Geoffrey Tien 9 February 04, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 10
17 27 search(27);
February 04, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 11
17 13 16 search(16);
February 04, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 12
17 13 9 11 search(12);
February 04, 2019
– This position is found by performing a search – If the search ends at the (null) left child of a node make its left child refer to the new node – If the search ends at the right child of a node make its right child refer to the new node
Cinda Heeren / Will Evans / Geoffrey Tien 13 February 04, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 14
47 32 63 19 10 23 41 37 44 54 53 59 79 96 7 12 30 43 57 91 97 Insert 43 Create new node 43 Find position Link node
February 04, 2019
Cinda Heeren / Will Evans / Geoffrey Tien 15
Create new BST Insert 3 Insert 15 Insert 21 Insert 23 Insert 37 3 15 21 23 37 Search 45 How many operations for Search? Complexity?
February 04, 2019
– From the root, keep following left child links until no more left child exists
– From the root, follow right child links until no more right child exists
February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 16
43 18 68 12 7 9 33 52 56 67 27 39 50 21
– Chapter 15.2 – 15.3 (Binary/search tree) – Chapter 18.1 (Dictionary)
– Chapter 15.3, 16.1 – 16.2 (Binary search tree)
February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 17