Bst
-
Upload
university-of-central-punjab -
Category
Technology
-
view
161 -
download
1
Transcript of Bst
Search
• Sorted Array: Binary Search • Linked List: Linear Search • Can we Apply the Binary Search
• O(log N) • O(N)
• Can we Apply the Binary Search algorithm on a linked list?
• Why not?
Sorted Array
• Rigid Structure– Fixed Size– Need to know the size of the largest data set– Need to know the size of the largest data set– Wastage
• Search: O (log n)• Insertion: O (n)• Deletion: O (n)
Binary Search Tree
• Binary Tree• Dynamic Structure (size is flexible) • Data is stored in a sorted fashion• Data is stored in a sorted fashion• A special class of BST has the following
properties:– Search: O (log n)– Insertion: O (log n)– Deletion: O (log n)
Binary Search Tree (BST)
1. Data value in the root node is greater
• A BST is a binary tree with the following properties:
2. Both the left subtree and right subtree are BSTs.
1. Data value in the root node is greater than all the data values stored in the left subtree and is less than or equal to all the values stored in the right subtree.
Binary Search Trees
Storage of elements for efficient access.
The binary-search-tree property:
Supporting dynamic set operations.
The binary-search-tree property:
If node y in left subtree of node x, then key[y] < key[x].
If node y in right subtree of node x, then key[y] ≥ key[x].
A BST Example
50
30 55
25 35
10
20
31 37
53 60
62
Search path for 37
Search time O(h) where h is tree height
15
2010
2 12 18 252 12 18 25
281411 23
23
3018
10 20 27 3210 20 27 32
50156
23
3018
10 20 27 3210 20 27 32
50196
Violating the condition for BST
Inorder Traversal of BST
50
30 55
25 35
10
20
31 37
53 60
62
Prints out keys in sorted order: 10, 20, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62
search(12)
15
2010
2 12 18 25
15
10
122 12 18 25
281411 23
12
t
search(23)
15
2010
2 12 18 25
15
20
252 12 18 25
281411 23
t
25
23
Binary Search TreeSearch
TreeNode * BinaryTree::search(int key)
{
TreeNode *t = root;
while (t) {
if (t->data == key) break;if (t->data == key) break;
else if (t->data > key) t = t->left;
else t = t->right;
}
return t;
}
search(13)
15
2010
2 12 18 25
15
10
122 12 18 25
281411 23
12
t
14
Tree-Minimum
TreeNode *Tree_Minimum()
{ TreeNode * x=root;
While (x->left != null)While (x->left != null)
x= x->left;
return x
}
Tree Maximum
TreeNode *Tree_Maximum()
{ TreeNode* x=root;
While (x->right != null)While (x->right != null)
x= x->right;
return x
}
Successor and Predecessor
50
30 55
x
10, 20, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62
25 35
10
20
31 37
53 60
62
The predecessor of x isthe rightmost node inits left subtree:
The successor of xis the leftmost nodein its right subtree
BST Operations Preview
25
10 37
15 30 65
Insert 810
25
158
37
30 6515 30 65
Delete 25
158 30 65
The successor of 25
65
3710
30
15
15
2010
2 12 18 25
insert(13)insert(19)insert(4)
2 12 18 25
281411 23
Binary Search Tree – Insertvoid BinaryTree::insert(int data){
TreeNode *t1, *t2, *t3;t1 = new TreeNode;t1->left = NULL; t1->right = NULL;t2 = root;if (!root) root = t1;else {else {
while (t2) {t3 = t2; if (t2->data > key) t2 = t2->left;else t2 = t2->right;
}if (t2->data > key) t3->left = t1;else t3->right = t1;
}}
insert(13)
15
2010
2 12 18 25
15
10
122 12 18 25
281411 23
12
14
13
Insertion
Example: insert z = 32 into the BST below
25
20 35
xCompare 32 and 25traverse the right subtree
40
20
12
35
Compare 32and 35, traverse theleft subtree
insert 32 as left child
40
35
12
20
25
x
y
35
12
20
25
4032
y
z
delete(2)15
2010
2 12 18 25
delete(14)delete(12)
2 12 18 25
281411 23
13
Delete a node from a BST
1. Locate the desired node by search; call it t2. If t is a leaf, disconnect it from its parent
and set the pointer in the parent node equal to NULLto NULL
3. If it has only one child then remove t from the tree by making t’s parent point to its child.
4. Otherwise, find the largest/smallest among t’s LST/RST; call it p. Copy p’s information into t. Delete p.
Deletion (A)
Case A: node x (to be deleted) is a leaf nodeOperation: update the parent node to have an empty subtree.
25
15 40
20
17
4530 453020
15 40
25
x
Deletion (B)
Case B: node x has a left child but no right childOperation: attach the left subtree of x to the parent
25
15 40
20
17
4530 453017
15 40
25
x
Deletion (C)
Case C: node x has a right child but no left childOperation: attach the right subtree of x to the parent
25
15 40
20
17
4530 453017
20 40
25
x
Deletion (D)
Case D: node x has two children
Operation: 1. Select as the replacement (of x) node y, the successor of x. y has the smallest valuegreater than that of x.
2. Unlink y from the tree.3. Connect y’s right subtree to its parent. 4. Finally, connect y at the deleted node. 4. Finally, connect y at the deleted node.
A
CD
r
x
y
zB zA
y
C D
r
B
Deletion (D) - an Example
40
30
40
33
10, 25, 28, 30, 33, 34, 35, 40, 50, 65 10, 25, 28, 33, 34, 35, 40, 50, 65
30
25
10 28
35
33
65
50
replacementof 30 34
33
3525
10 28
65
3450
Time Complexity
15
2010
2 12 18 252 12 18 25
281411 23
Search
Insert
Delete
15
18
20
26
insert(15)
insert(18)
insert(20)
insert(26)
insert(27)
insert(34)
27
34
insert(34)
Time Complexity
O(k) where k is the height
Height Balanced Treesk = log (n)k = log (n)
Running Time
All basic operations on a binary search tree of height h run in O(h) time.
Main question: How to guarantee h = O(lg n)?
One answer: red-black trees and AVL