Transcript Lecture 10

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
1
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
2
Data Structures: A Pseudocode Approach with C, Second Edition
3
Data Structures: A Pseudocode Approach with C, Second Edition
4
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
5
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
6
Searches
Data Structures: A Pseudocode Approach with C, Second Edition
7
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
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.
Data Structures: A Pseudocode Approach with C, Second Edition
15
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.
2.
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
16
Data Structures: A Pseudocode Approach with C, Second Edition
17
(continued)
Data Structures: A Pseudocode Approach with C, Second Edition
18
Data Structures: A Pseudocode Approach with C, Second Edition
19
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
20
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
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
50
Data Structures: A Pseudocode Approach with C, Second Edition
51
Data Structures: A Pseudocode Approach with C, Second Edition
52
Data Structures: A Pseudocode Approach with C, Second Edition
53
Data Structures: A Pseudocode Approach with C, Second Edition
54
Data Structures: A Pseudocode Approach with C, Second Edition
55
Data Structures: A Pseudocode Approach with C, Second Edition
56
Data Structures: A Pseudocode Approach with C, Second Edition
57
Data Structures: A Pseudocode Approach with C, Second Edition
58
Data Structures: A Pseudocode Approach with C, Second Edition
59
Data Structures: A Pseudocode Approach with C, Second Edition
60
Data Structures: A Pseudocode Approach with C, Second Edition
61
Data Structures: A Pseudocode Approach with C, Second Edition
62
Data Structures: A Pseudocode Approach with C, Second Edition
63
Data Structures: A Pseudocode Approach with C, Second Edition
64
Data Structures: A Pseudocode Approach with C, Second Edition
65
Data Structures: A Pseudocode Approach with C, Second Edition
66
Data Structures: A Pseudocode Approach with C, Second Edition
67
Data Structures: A Pseudocode Approach with C, Second Edition
68
Data Structures: A Pseudocode Approach with C, Second Edition
69
Data Structures: A Pseudocode Approach with C, Second Edition
70
Data Structures: A Pseudocode Approach with C, Second Edition
71
Data Structures: A Pseudocode Approach with C, Second Edition
72
Data Structures: A Pseudocode Approach with C, Second Edition
73