# Chapter 7 - PowerPoint PPT Presentation

1 / 73
Chapter 7

## Chapter 7

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

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

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

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

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

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

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

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