Cinda Heeren / Geoffrey Tien Binary Search Trees Properties Insertion October 10, 2017 Cinda Heeren / Geoffrey Tien
Before we begin... Another type of tree traversal We have seen pre-order, in-order, post-order traversals What about a traversal that visits every node in a level before working on the next level? level-order traversal 41 33 87 21 74 36 45 78 25 Use some ADT to support this? October 10, 2017 Cinda Heeren / Geoffrey Tien
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 Stumpjumper The favourite baby of VanCity planners Z125 Pro Fun in the sun! GL1800 Quiet comfort Insert Feet Useful for something, presumably Find(Z125 Pro) Z125 Pro Fun in the sun! October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien Search/Set ADT Stores only key values Keys may be any homogenous comparable type Tests quickly for membership Operations Create Destroy Insert Find Remove Chips Pizza Cookie Popcorn KD Peanuts Chocolate Insert Jolly Rancher Find(Nachos) NOT FOUND October 10, 2017 Cinda Heeren / Geoffrey Tien
Data structures for Dictionary ADT Naïve implementations, complexity Search Insert Remove Linked list Unsorted array Sorted array Ordered linked list October 10, 2017 Cinda Heeren / Geoffrey Tien
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 October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien BST example 17 13 27 9 16 20 39 11 October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien BST inOrder traversal inOrder(nd->leftchild); inOrder traversal on a BST retrieves data in sorted order visit(nd); 5 inOrder(nd->rightchild); 17 3 7 inOrder(left) inOrder(left) 13 27 visit visit inOrder(right) inOrder(right) 1 4 6 8 inOrder(left) inOrder(left) inOrder(left) 9 visit 16 20 visit 39 visit inOrder(right) inOrder(right) inOrder(right) inOrder(left) 2 visit inOrder(right) inOrder(left) 11 visit inOrder(right) October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien 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 October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien 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 October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien BST search example search(27); 17 27 October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien BST search example search(16); 17 13 16 October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien BST search example search(12); 17 13 9 11 October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien 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) October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien BST insertion example Insert 43 47 Create new node Find position 32 63 Link node 19 41 54 79 43 10 23 37 44 53 59 96 7 12 30 43 57 91 97 October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien BST insertion example Create new BST 3 Insert 3 Insert 15 15 Insert 21 Insert 23 21 Insert 37 Search 45 23 How many operations for Search? Complexity? 37 October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien 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 7 9 33 52 56 67 27 39 50 21 October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien Removal Notice that insertion into a BST always adds a new leaf node so the structure of the tree above the inserted node is not affected but generally, nodes existing in the tree may be internal nodes or leaf nodes Removal can be requested at any node in the tree, and removing an internal node may create difficulties with enforcing all the necessary structural properties (tree, binary tree, BST) more complicated logic required to maintain BST properties after removal – next class! October 10, 2017 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien Exercise Elements from a sorted list (ascending order) are inserted into a BST one by one. Which traversal method(s) will return the list in ascending order? descending order? neither ascending nor descending order? Write recursive and iterative implementations of findMin, findMax Assume key type is int Assume there is a member attribute Node* root int findMin(Node* nd); Write iterative and recursive implementations of insert void insert(int key); Node* insert(int key, Node* nd); October 10, 2017 Cinda Heeren / Geoffrey Tien
Readings for this lesson Koffman Chapter 8.3 – 8.4 (Binary trees, binary search trees) October 10, 2017 Cinda Heeren / Geoffrey Tien