1 / 49

Binary Search Trees (BST)

Binary Search Trees (BST). Binary Search Trees (BST). Binary tree (but not BST). Binary Search Tree (Balanced). Binary Search Tree (Un-balanced). Definitions. Binary tree Each node has at most two children Binary Search Tree (BST): Is a binary tree where:

asaro
Download Presentation

Binary Search Trees (BST)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary Search Trees(BST)

  2. Binary Search Trees (BST) Binary tree (but not BST) Binary Search Tree (Balanced) Binary Search Tree (Un-balanced)

  3. 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

  4. 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)

  5. BST Traversal Three main types • Pre-Order Traversal • Post-Order Traversal • In-Order Traversal

  6. Traversing a Binary Search Tree (BST)Pre-Order

  7. Outline of Pre-Order Traversal • For each node • Do the work first (Current) • Traverse Left • Traverse Right • Work can be anything (E.g., print)

  8. Traverse the tree “Pre-order”: • Visit the current and do work • Visit the tree’s left sub-tree • Visit right sub-tree

  9. 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

  10. 7 97 94 44 14 1 36 67 9 22 3 Proc PreOrderPrint(pointer) pointer NOT NIL? print(data) PreOrderPrint(left child) PreOrderPrint(right child) root P L R Output 22, 9, 3, 1, 7, 14, 67, 36, 44, 94, 97

  11. Traversing a Binary Search Tree (BST)Post-Order

  12. Outline of Post-Order Traversal • For each node • Traverse Left • Traverse Right • Do the work (Current)

  13. 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 • endprocedure Two recursive calls

  14. 7 97 94 44 14 1 36 67 9 22 3 Proc PostOrderPrint(pointer) pointer NOT NIL? PostOrderPrint(left child) PostOrderPrint(right child) print(data) root L R P Output 1, 7, 3, 14, 9, 44, 36, 97, 94, 67, 22

  15. Traversing a Binary Search Tree (BST)In-Order

  16. Outline of In-Order Traversal • For each node • Traverse Left • Do the work (Current) • Traverse Right

  17. 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

  18. 94 97 7 44 14 1 36 67 9 22 3 ProcInOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R Output 1, 3, 7, 9, 14, 22, 36, 44, 67, 94, 97 In-order traversal produces a sorted list

  19. 10 33 20 60 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 • 3-way tree • Pre-Order and Post-Order traversal are applicable • In-Order traversal is not applicable

  20. Analysis of Tree Traversal • Assume the tree has n nodes • Any the three types of traversal is linearO(n) • Each node is visited only once (E.g., printing is done once)

  21. Searching

  22. Problem Definition • Input • Binary tree • Value k to search for Algorithm depdens on whether it is binary search tree (BST) or not

  23. 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 returnTree-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)

  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 returnTree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure

  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 returnTree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure

  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 returnTree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure

  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 returnTree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure

  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 returnTree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure

  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 returnTree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure

  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 returnTree-Search(ptr->left, k) || Tree-Search(ptr->right, k) end if; End procedure

  31. 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 returnTree-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

  32. 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 returnTree-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)

  33. 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) returnTree-Search(ptr->left, k) else return Tree-Search(ptr->right, k) end if; End procedure

  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) returnTree-Search(ptr->left, k) else return Tree-Search(ptr->right, k) end if; End procedure

  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) returnTree-Search(ptr->left, k) else return Tree-Search(ptr->right, k) end if; End procedure

  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) returnTree-Search(ptr->left, k) else return Tree-Search(ptr->right, k) end if; End procedure

  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) returnTree-Search(ptr->left, k) else return Tree-Search(ptr->right, k) end if; End procedure

  38. 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) returnTree-Search(ptr->left, k) else return Tree-Search(ptr->right, k) end if; End procedure Not Found

  39. 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)

  40. Insertions & Deletions in BST

  41. Insertion of V in BST • Search for the correctempty 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

  42. Example: Insert value 6 • Search for the correctempty 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

  43. Example: Insert value 6 • Search for the correctempty 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

  44. Example: Insert value 6 • Search for the correctempty 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

  45. Example: Insert value 6 • Search for the correctempty 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

  46. Example: Insert value 6 • Search for the correctempty 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

  47. Example: Insert value 6 • Search for the correctempty 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

  48. 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)??

  49. 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

More Related