1 / 62

Binary Search Trees

Binary Search Trees. Lecture 6 Asst. Prof. Dr. İlker Kocabaş. Binary search tree sort. Binary-search-tree property. Binary-search-tree property. Binary Search Tree can be implemented as a linked data structure in which each node is an object with three pointer fields.

dcupples
Download Presentation

Binary Search Trees

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 Lecture 6 Asst. Prof. Dr. İlker Kocabaş

  2. Binary search tree sort

  3. Binary-search-tree property

  4. Binary-search-tree property • Binary Search Tree can be implemented as a linked data structure in which each node is an object with three pointer fields. • The three pointer fields left, right and p point to the nodes corresponding to the left child, right child and the parent respectively. • NIL in any pointer field signifies that there exists no corresponding child or parent. • The root node is the only node in the BTS structure with NIL in its p field.

  5. Binary-search-tree property

  6. Inorder-tree walk During this type of walk, we visit the root of a subtree between the left subtree visit and right subtree visit.

  7. Preorder-tree walk In this case we visit the root node before the nodes in either subtree.

  8. Postorder-tree walk We visit the root node after the nodes in its subtrees.

  9. 1 2 6 Sorting by binary-search-tree 1 2 3 1 2 2 6 1 NIL • PRINT 2 4 7 3 NIL 3 PRINT 3 4 5 2 8 • 1 • NIL • PRINT 5 • NIL 5 3 PRINT 6 4 • 1 • NIL • PRINT 7 • 4 7 7 • 1 • NIL • PRINT 8 • NIL 8

  10. Sorting by binary-search-tree

  11. Sorting by binary-search-tree(contnd.)

  12. Searching for a key 15 18 6 7 3 17 20 4 2 Search for 13 13 9

  13. Searching for a key

  14. Searching for minimum 15 18 6 7 3 17 20 4 2 13 Minimum 9

  15. Searching for minimum

  16. Searching for maximum 15 18 6 7 3 17 20 Maximum 4 2 13 9

  17. Searching for maximum

  18. Searching for successor The successor of a node x is the node with the smallest key greather than key[x]

  19. Searching for successor 15 18 6 7 3 17 20 Successor of 15 4 2 13 9 Case 1: The right subtree of node xis nonempty

  20. Searching for successor Successor of 13 15 18 6 7 3 17 20 4 2 13 9 Case 2: The right subtree of node xis empty

  21. Searching for successor 15 Successor of 4 search until y.left = x 18 6 7 3 17 20 4 2 13 9 Case 2: The right subtree of node xis empty

  22. Searching for successor  Go upper until y.left = x

  23. Insertion 12 18 5 9 2 15 19 13 17 Inserting an item with key 13

  24. Insertion

  25. Deletion 15 16 5 5 12 3 2 20 13 z 10 23 18 6 7 Deleting an item with key 13 (z has no children)

  26. Deletion 15 16 5 5 12 3 2 20 10 23 18 6 7 Deleting an item with key 13 (z has no children)

  27. Deletion 15 16 z 5 5 12 3 2 20 13 10 23 18 6 7 Deleting a node with key 16 (z has only one child )

  28. Deletion 15 5 5 20 12 3 2 23 18 13 10 6 7 Deleting a node with key 16 (z has only one child )

  29. Deletion 15 z 5 16 5 12 3 2 20 10 13 18 23 6 y 7 Deleting a node with key 5 (6 successor of 5)(z has two children)

  30. Deletion 15 6 y z 5 16 5 12 3 2 20 10 23 18 7 Deleting a node with key 5 (z has two children)

  31. Deletion 15 6 16 5 12 3 2 20 10 23 18 7 Deleting a node with key 5 (z has two children)

  32. Deletion

  33. Analysis of BST sort The expected time to built the tree is asymptotically the same as the running time of quicksort

  34. Red Black Trees

  35. Balanced search trees

  36. Red-black trees

  37. Example of a red-black tree

  38. Example of a red-black tree

  39. Example of a red-black tree

  40. Example of a red-black tree

  41. Example of a red-black tree

  42. Height of a red-black tree

  43. Height of a red-black tree

  44. Height of a red-black tree

  45. Height of a red-black tree

  46. Height of a red-black tree

  47. Height of a red-black tree

  48. Proof (continued)

  49. Query operations

  50. Modifying operations

More Related