Chapter 7
description
Transcript of Chapter 7
![Page 1: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/1.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 1
Chapter 7Chapter 7
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
Binary Search TreesBinary Search Trees
![Page 2: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/2.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 2
7-1 Basic Concepts
Binary search trees provide an excellent structure for searching Binary search trees provide an excellent structure for searching a list and at the same time for inserting and deleting data into a list and at the same time for inserting and deleting data into the list.the list.
A A binary search treebinary search tree (BST) is a binary tree with following properties: (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 left of the tree are less than the root.
•All items in the right subtree are greater than or equal to the root.All items in the right subtree are greater than or equal to the root.
•Each subtree is itself a binary search tree.Each subtree is itself a binary search tree.
![Page 3: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/3.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 3
![Page 4: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/4.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 4
![Page 5: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/5.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 5
7-2 BST Operations
There are four basic BST operations: traversal, search, insert, There are four basic BST operations: traversal, search, insert, and delete.and delete.
Traversals• Searches• Insertion• Deletion
![Page 6: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/6.jpg)
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
![Page 7: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/7.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 7
Searches
![Page 8: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/8.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 8
![Page 9: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/9.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 9
![Page 10: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/10.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 10
![Page 11: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/11.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 11
![Page 12: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/12.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 12
![Page 13: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/13.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 13
![Page 14: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/14.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 14
![Page 15: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/15.jpg)
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:
1. The node to be deleted has no children. All we need to do is delete the node.
2. 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.
3. 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.
4. 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.
![Page 16: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/16.jpg)
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.
1. We can find the largest node in the deleted node’s left subtree and move its data to replace the deleted node’s data
2. 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.
![Page 17: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/17.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 17
![Page 18: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/18.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 18
(continued)
![Page 19: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/19.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 19
![Page 20: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/20.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 20
7-3 Binary Search Tree ADT
Discussion of the BST data structure includes:Discussion of the BST data structure includes:
• Data Structure• Algorithms
![Page 21: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/21.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 21
![Page 22: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/22.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 22
![Page 23: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/23.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 23
![Page 24: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/24.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 24
![Page 25: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/25.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 25
![Page 26: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/26.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 26
![Page 27: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/27.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 27
![Page 28: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/28.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 28
![Page 29: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/29.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 29
![Page 30: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/30.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 30
![Page 31: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/31.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 31
![Page 32: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/32.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 32
![Page 33: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/33.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 33
![Page 34: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/34.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 34
![Page 35: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/35.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 35
![Page 36: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/36.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 36
![Page 37: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/37.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 37
![Page 38: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/38.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 38
![Page 39: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/39.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 39
![Page 40: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/40.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 40
![Page 41: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/41.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 41
![Page 42: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/42.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 42
![Page 43: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/43.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 43
![Page 44: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/44.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 44
![Page 45: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/45.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 45
![Page 46: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/46.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 46
![Page 47: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/47.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 47
![Page 48: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/48.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 48
![Page 49: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/49.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 49
![Page 50: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/50.jpg)
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 This section develops two applications that use the BST ADT. We begin the discussion of BST Applications with a simple begin the discussion of BST Applications with a simple application that manipulates integers. The second application, application that manipulates integers. The second application, student list, requires a structure to hold the student's data.student list, requires a structure to hold the student's data.
• Integer Application• Student List Application
![Page 51: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/51.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 51
![Page 52: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/52.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 52
![Page 53: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/53.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 53
![Page 54: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/54.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 54
![Page 55: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/55.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 55
![Page 56: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/56.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 56
![Page 57: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/57.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 57
![Page 58: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/58.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 58
![Page 59: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/59.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 59
![Page 60: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/60.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 60
![Page 61: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/61.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 61
![Page 62: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/62.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 62
![Page 63: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/63.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 63
![Page 64: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/64.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 64
![Page 65: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/65.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 65
![Page 66: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/66.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 66
![Page 67: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/67.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 67
![Page 68: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/68.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 68
![Page 69: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/69.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 69
![Page 70: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/70.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 70
![Page 71: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/71.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 71
![Page 72: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/72.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 72
![Page 73: Chapter 7](https://reader035.fdocuments.in/reader035/viewer/2022062309/56813b17550346895da3c66b/html5/thumbnails/73.jpg)
Data Structures: A Pseudocode Approach with C, Second Edition 73