1 / 73

Chapter 7

Chapter 7. Binary Search Trees. Objectives. Create and implement binary search trees Understand the operation of the binary search tree ADT Write application programs using the binary search tree ADT Design and implement a list using a BST. 7-1 Basic Concepts.

clancy
Télécharger la présentation

Chapter 7

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. Chapter 7 Binary Search Trees Objectives • Create and implement binary search trees • Understand the operation of the binary search tree ADT • Write application programs using the binary search tree ADT • Design and implement a list using a BST Data Structures: A Pseudocode Approach with C, Second Edition

  2. 7-1 Basic Concepts • Binary search trees provide an excellent structure for searching a list and at the same time for inserting and deleting data into the list. • A binary search tree (BST) is a binary tree with following properties: • All items in the left of the tree are less than the root. • All items in the right subtree are greater than or equal to the root. • Each subtree is itself a binary search tree. Data Structures: A Pseudocode Approach with C, Second Edition

  3. Data Structures: A Pseudocode Approach with C, Second Edition

  4. Data Structures: A Pseudocode Approach with C, Second Edition

  5. 7-2 BST Operations • There are four basic BST operations: traversal, search, insert, and delete. • Traversals • Searches • Insertion • Deletion Data Structures: A Pseudocode Approach with C, Second Edition

  6. Preorder Traversal : 23 18 12 20 44 35 52 Postorder Traversal: 12 20 8 35 52 44 23 Inorder Traversal: 12 18 20 23 35 44 52 Data Structures: A Pseudocode Approach with C, Second Edition

  7. Searches Data Structures: A Pseudocode Approach with C, Second Edition

  8. Data Structures: A Pseudocode Approach with C, Second Edition

  9. Data Structures: A Pseudocode Approach with C, Second Edition

  10. Data Structures: A Pseudocode Approach with C, Second Edition

  11. Data Structures: A Pseudocode Approach with C, Second Edition

  12. Data Structures: A Pseudocode Approach with C, Second Edition

  13. Data Structures: A Pseudocode Approach with C, Second Edition

  14. Data Structures: A Pseudocode Approach with C, Second Edition

  15. Deletion • To delete a node from a binary search tree, we must first locate it. There are four cases for deletion: • The node to be deleted has no children. All we need to do is delete the node. • The node to be deleted has only a right subtree. We need to delete the node and attach the right subtree to the deleted node’s parent. • The node to be deleted has only a left subtree. We need to delete the node and attach the left subtree to the deleted node’s parent. • The node to deleted has two subtrees. It is possible to delete a node from the middle of a tree, but the result tends to a very unbalanced trees. Rather, we try to maintain existing structure by finding data to take place of the deleted data. Data Structures: A Pseudocode Approach with C, Second Edition

  16. When the node to deleted has two subtrees. It is possible to delete a node from the middle of a tree, but the result tends to a very unbalanced trees. Rather, we try to maintain existing structure by finding data to take place of the deleted data. • We can find the largest node in the deleted node’s left subtree and move its data to replace the deleted node’s data • Or we can find the smallest node in the deleted node’s right subtree and move its data to replace the deleted node’s data • Regardless of which logic we choose, we will be moving data from a leaf or leaf-like node that can be deleted. Data Structures: A Pseudocode Approach with C, Second Edition

  17. Data Structures: A Pseudocode Approach with C, Second Edition

  18. (continued) Data Structures: A Pseudocode Approach with C, Second Edition

  19. Data Structures: A Pseudocode Approach with C, Second Edition

  20. 7-3 Binary Search Tree ADT • Discussion of the BST data structure includes: • Data Structure • Algorithms Data Structures: A Pseudocode Approach with C, Second Edition

  21. Data Structures: A Pseudocode Approach with C, Second Edition

  22. Data Structures: A Pseudocode Approach with C, Second Edition

  23. Data Structures: A Pseudocode Approach with C, Second Edition

  24. Data Structures: A Pseudocode Approach with C, Second Edition

  25. Data Structures: A Pseudocode Approach with C, Second Edition

  26. Data Structures: A Pseudocode Approach with C, Second Edition

  27. Data Structures: A Pseudocode Approach with C, Second Edition

  28. Data Structures: A Pseudocode Approach with C, Second Edition

  29. Data Structures: A Pseudocode Approach with C, Second Edition

  30. Data Structures: A Pseudocode Approach with C, Second Edition

  31. Data Structures: A Pseudocode Approach with C, Second Edition

  32. Data Structures: A Pseudocode Approach with C, Second Edition

  33. Data Structures: A Pseudocode Approach with C, Second Edition

  34. Data Structures: A Pseudocode Approach with C, Second Edition

  35. Data Structures: A Pseudocode Approach with C, Second Edition

  36. Data Structures: A Pseudocode Approach with C, Second Edition

  37. Data Structures: A Pseudocode Approach with C, Second Edition

  38. Data Structures: A Pseudocode Approach with C, Second Edition

  39. Data Structures: A Pseudocode Approach with C, Second Edition

  40. Data Structures: A Pseudocode Approach with C, Second Edition

  41. Data Structures: A Pseudocode Approach with C, Second Edition

  42. Data Structures: A Pseudocode Approach with C, Second Edition

  43. Data Structures: A Pseudocode Approach with C, Second Edition

  44. Data Structures: A Pseudocode Approach with C, Second Edition

  45. Data Structures: A Pseudocode Approach with C, Second Edition

  46. Data Structures: A Pseudocode Approach with C, Second Edition

  47. Data Structures: A Pseudocode Approach with C, Second Edition

  48. Data Structures: A Pseudocode Approach with C, Second Edition

  49. Data Structures: A Pseudocode Approach with C, Second Edition

  50. 7-4 BST Applications • This section develops two applications that use the BST ADT. We begin the discussion of BST Applications with a simple application that manipulates integers. The second application, student list, requires a structure to hold the student's data. • Integer Application • Student List Application Data Structures: A Pseudocode Approach with C, Second Edition

More Related