Binary Search Trees BST Slide 1 Binary Search
Binary Search Trees (BST) Slide 1
Binary Search Trees (BST) Binary tree (but not BST) Binary Search Tree (Balanced) Binary Search Tree (Un-balanced) Slide 2
Definitions • Binary tree – Each node has at most two children • Binary Search Tree (BST): Is a binary tree where: – Left subtree is always less than the node – Right subtree is always greater than or equal the node Slide 3
Height of BST • Can be balanced – Left of the root ≈ Right of the root – In this case the height O(Log n) • Can be un-balanced – In this case the height is worst case O(n) Slide 4
BST Traversal Three main types • Pre-Order Traversal • Post-Order Traversal • In-Order Traversal Slide 5
Traversing a Binary Search Tree (BST) Pre-Order Slide 6
Outline of Pre-Order Traversal • For each node – Do the work first (Current) – Traverse Left – Traverse Right • Work can be anything (E. g. , print) Slide 7
• Traverse the tree “Pre-order”: –Visit the current and do work –Visit the tree’s left sub-tree –Visit right sub-tree Slide 8
Pre-Order Traversal Procedure procedure Pre_Order(Ptr Pointer to Tree_Node) if( Ptr <> NIL ) then Do_Whatever( Ptr->data ) Pre_Order( Ptr->left_child ) Pre_Order( Ptr->right_child ) endif endprocedure Two recursive calls Slide 9
Proc Pre. Order. Print(pointer) pointer NOT NIL? P print(data) L Pre. Order. Print(left child) R Pre. Order. Print(right child) root 22 67 9 Output 22, 9, 3, 1, 7, 14, 67, 36, 44, 97 3 1 14 7 36 94 44 97 Slide 10
Traversing a Binary Search Tree (BST) Post-Order Slide 11
Outline of Post-Order Traversal • For each node – Traverse Left – Traverse Right – Do the work (Current) Slide 12
Post-Order Traversal Procedure procedure Post_Order(Ptr Pointer to Tree_Node) if( Ptr <> NIL ) then Post_Order( Ptr->left_child ) Post_Order( Ptr->right_child ) Do_Whatever( Ptr->data ) endif Two recursive endprocedure calls Slide 13
Proc Post. Order. Print(pointer) pointer NOT NIL? L Post. Order. Print(left child) R Post. Order. Print(right child) P print(data) root 22 67 9 Output 1, 7, 3, 14, 9, 44, 36, 97, 94, 67, 22 3 1 14 7 36 94 44 97 Slide 14
Traversing a Binary Search Tree (BST) In-Order Slide 15
Outline of In-Order Traversal • For each node – Traverse Left – Do the work (Current) – Traverse Right Slide 16
In-Order Traversal Procedure procedure In_Order(Ptr Pointer to Tree_Node) if( Ptr <> NIL ) then In_Order( Ptr->left_child ) Do_Whatever( Ptr->data ) In_Order( Ptr->right_child ) endif endprocedure Two recursive calls Slide 17
Proc In. Order. Print(pointer) pointer NOT NIL? L In. Order. Print(left child) P print(data) R In. Order. Print(right child) root 22 67 9 Output 1, 3, 7, 9, 14, 22, 36, 44, 67, 94, 97 3 In-order traversal produces a sorted list 1 14 7 36 94 44 97 Slide 18
Notes • The three traversal types (Pre-, In-, Post-)Order apply to all types of binary trees – Whether binary search tree or not • In-Order traversal is applicable only for binary trees – Not for N-way trees, where N > 2 10 • • • 20 60 3 -way tree Pre-Order and Post-Order traversal are applicable In-Order traversal is not applicable 33 Slide 19
Analysis of Tree Traversal • Assume the tree has n nodes • Any the three types of traversal is linear O(n) • Each node is visited only once (E. g. , printing is done once) Slide 20
Searching Slide 21
Problem Definition • Input – Binary tree – Value k to search for Algorithm depdens on whether it is binary search tree (BST) or not Slide 22
Searching Binary Trees (Not BST) Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else return Tree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure If the current node contains k, return true Call the left and right subtrees recursively (Will check the L. H. S first) Slide 23
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else return Tree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure Slide 24
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else return Tree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure Slide 25
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else return Tree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure Slide 26
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else return Tree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure Slide 27
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else return Tree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure Slide 28
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else return Tree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure Slide 29
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else return Tree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure Slide 30
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else return Tree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure Similar to Pre-Order traversal - Check the node first - Check the L. H. S - Check the R. H. S Slide 31
Time Complexity For Binary Tree Searching Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else return Tree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure • • How many times each node is checked? – At most once Searching a binary tree is linear O(n) Slide 32
Find K in Binary Search Trees (BST) • Should make use of the BST property to search in an efficient way Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else if (k < ptr. data) return Tree-Search(ptr->left, k) else return Tree-Search(ptr->right, k) end if; End procedure Slide 33
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else if (k < ptr. data) return Tree-Search(ptr->left, k) else return Tree-Search(ptr->right, k) end if; End procedure Slide 34
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else if (k < ptr. data) return Tree-Search(ptr->left, k) else return Tree-Search(ptr->right, k) end if; End procedure Slide 35
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else if (k < ptr. data) return Tree-Search(ptr->left, k) else return Tree-Search(ptr->right, k) end if; End procedure Slide 36
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else if (k < ptr. data) return Tree-Search(ptr->left, k) else return Tree-Search(ptr->right, k) end if; End procedure Slide 37
Example: Search For 6 Tree-Search(ptr pointer to current root, value k) if (ptr == NULL) return false; else if (ptr. data = k) return true; else if (k < ptr. data) return Tree-Search(ptr->left, k) else return Tree-Search(ptr->right, k) end if; End procedure Not Found Slide 38
Time Complexity For BST Searching • In BST search we follow ONLY on path from root to a leaf • What is the Worst Case Complexity? – O(h), where h is the tree height – If BST is balanced O (Log n) – If BST is not balanced O (n) Slide 39
Insertions & Deletions in BST Slide 40
Insertion of V in BST • Search for the correct empty place to put the new node – Correct: if V >= current node move right if V < current node move left – Empty: Find link with Null value Slide 41
Example: Insert value 6 • Search for the correct empty place to put the new node – Correct: if V >= current node move right if V < current node move left – Empty: Find link with Null value Slide 42
Example: Insert value 6 • Search for the correct empty place to put the new node – Correct: if V >= current node move right if V < current node move left – Empty: Find link with Null value Slide 43
Example: Insert value 6 • Search for the correct empty place to put the new node – Correct: if V >= current node move right if V < current node move left – Empty: Find link with Null value Slide 44
Example: Insert value 6 • Search for the correct empty place to put the new node – Correct: if V >= current node move right if V < current node move left – Empty: Find link with Null value Slide 45
Example: Insert value 6 • Search for the correct empty place to put the new node – Correct: if V >= current node move right if V < current node move left – Empty: Find link with Null value Slide 46
Example: Insert value 6 • Search for the correct empty place to put the new node – Correct: if V >= current node move right if V < current node move left – Empty: Find link with Null value Time complexity for insertion is O(h), h: is the tree height Slide 47
Deletion of V from BST • Search for V in the tree – Not Found We are done – Found • Case 1: Node V has no children – Delete V • Case 2: Node V has one child – The child takes the place of V • Case 3: Node V has two children – Find the largest node smaller than V Say X – Put X in the place of V How to find this value (Successor of V)? ? Slide 48
Find the Successor Of Given Node V • Successor of V: is the smallest value larger than V • How to find it – Go to the R. H. S child node – Follow the left link all the way (until Null) • Successor of 22 – 36 Successor of 3 – 6 • Time complexity for finding the successor is O(h), h is the tree height Slide 49
- Slides: 49