Binary search trees Dictionary ADT Binary search tree
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 class: – https: //en. wikibooks. org/wiki/Alevel_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
Motivation for an efficient lookup Dictionary ADT • Stores values associated with user-specified keys – Values may be any (homogenous) type – Keys may be any (homogenous) comparable type • Dictionary operations – – – Create Destroy Insert Find Remove Insert • Feet • Useful for something, presumably Find(Z 125 Pro) • February 04, 2019 • Super 9 LC • Park it on the sidewalk, it's OK • Z 125 Pro • Fun in the sun! • CB 300 F • For the mild-mannered commuter Z 125 Pro • Fun in the sun! Cinda Heeren / Will Evans / Geoffrey Tien 3
Data structures for Dictionary ADT Naïve implementations, complexity Search Insert Remove • Linked list • Unsorted array • Sorted array • Ordered linked list February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 4
Binary search tree property • A binary search tree is a binary tree with a special property – For all nodes in the tree: • All nodes in a left subtree have labels less than the label of the subtree's root • All nodes in a right subtree have labels greater than or equal to the label of the subtree's root • Binary search trees are fully ordered February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 5
BST example 17 13 9 27 16 20 39 11 February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 6
BST in. Order traversal in. Order(nd->leftchild); visit(nd); 5 in. Order(nd->rightchild); 17 in. Order traversal on a BST retrieves data in sorted order 3 13 in. Order(left) visit in. Order(right) 2 in. Order(left) visit in. Order(right) February 04, 2019 in. Order(left) visit in. Order(right) 4 1 9 7 16 27 8 6 20 in. Order(left) visit in. Order(right) 39 in. Order(left) visit in. Order(right) 11 Cinda Heeren / Will Evans / Geoffrey Tien 7
BST implementation • Binary search trees can be implemented using a reference structure • Tree nodes contain data and two pointers to nodes Node* leftchild data Node* rightchild Data to be stored in the tree (usually an object) References or pointers to other tree Nodes February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 8
BST search • To find a value in a BST search from the root node: – 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 …) • How many comparisons? – One for each node on the path – Worst case: height of the tree + 1 February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 9
BST search example search(27); 17 27 February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 10
BST search example search(16); 17 13 16 February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 11
BST search example search(12); 17 13 9 11 February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 12
BST insertion • The BST property must hold after insertion • Therefore the new node must be inserted in the correct position – 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 • The cost is about the same as the cost for the search algorithm, O(height) February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 13
BST insertion example Insert 43 47 Create new node Find position 32 63 Link node 19 43 10 7 41 23 12 February 04, 2019 54 37 30 44 79 53 43 Cinda Heeren / Will Evans / Geoffrey Tien 59 57 96 91 97 14
BST insertion example Create new BST 3 Insert 15 15 Insert 21 Insert 23 21 Insert 37 Search 45 23 How many operations for Search? Complexity? 37 February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 15
Find min, Find max • Find minimum: – From the root, keep following left child links until no more left child exists • Find maximum: – From the root, follow right child links until no more right child exists 43 18 68 12 33 7 27 9 February 04, 2019 52 39 50 21 Cinda Heeren / Will Evans / Geoffrey Tien 56 67 16
Readings for this lesson • Carrano & Henry – Chapter 15. 2 – 15. 3 (Binary/search tree) – Chapter 18. 1 (Dictionary) • Next class: – Chapter 15. 3, 16. 1 – 16. 2 (Binary search tree) February 04, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 17
- Slides: 17