Upload
ankit-jain
View
227
Download
0
Embed Size (px)
Citation preview
8/11/2019 bst imp (1)
1/34
Data Structures: A Pseudocode Approach with C, Second Edition 1
A Binary Search Treeis a binarytree with the following properties:
All items in the left subtree are less than
the root.
All items in the right subtree are greateror equal to the root.
Each subtree is itself a binary search tree.
8/11/2019 bst imp (1)
2/34
Data Structures: A Pseudocode Approach with C, Second Edition 2
Basic Property
In a binary search tree,
the left subtree contains key values lessthan the root
the right subtree contains key valuesgreaterthan or equalto the root.
8/11/2019 bst imp (1)
3/34
Data Structures: A Pseudocode Approach with C, Second Edition 3
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.
8/11/2019 bst imp (1)
4/34
Data Structures: A Pseudocode Approach with C, Second Edition 4
8/11/2019 bst imp (1)
5/34
Data Structures: A Pseudocode Approach with C, Second Edition 5
(a), (b) - complete and balanced trees;
(d)nearly complete and balanced tree;
(c), (e)neither complete nor balanced trees
8/11/2019 bst imp (1)
6/34
Data Structures: A Pseudocode Approach with C, Second Edition 6
8/11/2019 bst imp (1)
7/34
Data Structures: A Pseudocode Approach with C, Second Edition 7
7-2 BST Operations
We discuss four basic BST operations: traversal, search, insert,
and delete; and develop algorithms for searches, insertion, and
deletion.
Traversals
Searches
Insertion
Deletion
8/11/2019 bst imp (1)
8/34
Data Structures: A Pseudocode Approach with C, Second Edition 8
8/11/2019 bst imp (1)
9/34
Data Structures: A Pseudocode Approach with C, Second Edition 9
8/11/2019 bst imp (1)
10/34
Data Structures: A Pseudocode Approach with C, Second Edition 10
Preorder Traversal
23 18 12 20 44 35 52
8/11/2019 bst imp (1)
11/34
Data Structures: A Pseudocode Approach with C, Second Edition 11
Postorder Traversal
12 20 18 35 52 44 23
8/11/2019 bst imp (1)
12/34
Data Structures: A Pseudocode Approach with C, Second Edition 12
Inorder Traversal
12 18 20 23 35 44 52
Inorder traversal of a binary search tree produces a
sequenced list
8/11/2019 bst imp (1)
13/34
Data Structures: A Pseudocode Approach with C, Second Edition 13
Three BST search algorithms:
Find the smallestnode
Find the largestnode
Find a requestednode
8/11/2019 bst imp (1)
14/34
Data Structures: A Pseudocode Approach with C, Second Edition 14
8/11/2019 bst imp (1)
15/34
Data Structures: A Pseudocode Approach with C, Second Edition 15
8/11/2019 bst imp (1)
16/34
Data Structures: A Pseudocode Approach with C, Second Edition 16
8/11/2019 bst imp (1)
17/34
Data Structures: A Pseudocode Approach with C, Second Edition 17
8/11/2019 bst imp (1)
18/34
Data Structures: A Pseudocode Approach with C, Second Edition 18
8/11/2019 bst imp (1)
19/34
Data Structures: A Pseudocode Approach with C, Second Edition 19
8/11/2019 bst imp (1)
20/34
Data Structures: A Pseudocode Approach with C, Second Edition 20
BST Insertion
To insert data all we need to do is followthe branches to an empty subtree andthen insert the new node.
In other words, all inserts take place at aleaf or at a leaflike nodea node that hasonly one null subtree.
8/11/2019 bst imp (1)
21/34
Data Structures: A Pseudocode Approach with C, Second Edition 21
8/11/2019 bst imp (1)
22/34
Data Structures: A Pseudocode Approach with C, Second Edition 22
8/11/2019 bst imp (1)
23/34
Data Structures: A Pseudocode Approach with C, Second Edition 23
8/11/2019 bst imp (1)
24/34
Data Structures: A Pseudocode Approach with C, Second Edition 24
30
30 30
30
8/11/2019 bst imp (1)
25/34
Data Structures: A Pseudocode Approach with C, Second Edition 25
Deletion
There are the following possible cases when we delete anode:
The node to be deleted has no children. In this case, allwe need to do is delete the node.
The node to be deleted has only a right subtree. Wedelete the node and attach the right subtree to thedeleted nodes parent.
The node to be deleted has only a left subtree. Wedelete the node and attach the left subtree to thedeleted nodes parent.
The node to be deleted has two subtrees. It is possibleto delete a node from the middle of a tree, but the resulttends to create very unbalanced trees.
8/11/2019 bst imp (1)
26/34
Data Structures: A Pseudocode Approach with C, Second Edition 26
Deletion from the middle of a tree
Rather than simply delete the node, wetry to maintain the existing structure asmuch as possible by finding data to take
the place of the deleted data. This can bedone in one of two ways.
8/11/2019 bst imp (1)
27/34
Data Structures: A Pseudocode Approach with C, Second Edition 27
Deletion from the middle of a tree
We can find the largest node in thedeleted nodes left subtree and move itsdata to replace the deleted nodes data.
We can find the smallest node on thedeleted nodes right subtree and move itsdata to replace the deleted nodes data.
Either of these moves preserves theintegrity of the binary search tree.
8/11/2019 bst imp (1)
28/34
Data Structures: A Pseudocode Approach with C, Second Edition 28
8/11/2019 bst imp (1)
29/34
Data Structures: A Pseudocode Approach with C, Second Edition 29
(continued)
8/11/2019 bst imp (1)
30/34
Data Structures: A Pseudocode Approach with C, Second Edition 30
27 27
27 27
8/11/2019 bst imp (1)
31/34
Data Structures: A Pseudocode Approach with C, Second Edition 31
7-3 Binary Search Tree ADT
We begin this section with a discussion of the BST data structure
and write the header f i le for the ADT.
Data StructureAlgorithms
8/11/2019 bst imp (1)
32/34
Data Structures: A Pseudocode Approach with C, Second Edition 32
8/11/2019 bst imp (1)
33/34
Data Structures: A Pseudocode Approach with C, Second Edition 33
8/11/2019 bst imp (1)
34/34
Homework:Preparation for the final test
Chapter 6 (pp. 265-282)
Chapter 7 (Sections 7.1; 7.2)
p. 292: ex. 1; p. 293: ex. 6, ex. 12
P. 337: ex. 3, ex. 4, ex.6, ex. 7
P. 338: ex. 13, ex. 14
Data Structures: A Pseudocode Approach with C Second Edition 34