0% found this document useful (0 votes)
15 views33 pages

Search

The document discusses binary search trees (BSTs) and their properties and operations. A BST is a binary tree where the value in each internal node is greater than all values in its left subtree and less than or equal to all values in its right subtree. This property allows searching a BST in O(log n) time. Other BST operations like insertion and deletion also take O(log n) time. The document explains how to implement searching, insertion, deletion and other common BST operations along with examples.

Uploaded by

Mr Mudassir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views33 pages

Search

The document discusses binary search trees (BSTs) and their properties and operations. A BST is a binary tree where the value in each internal node is greater than all values in its left subtree and less than or equal to all values in its right subtree. This property allows searching a BST in O(log n) time. Other BST operations like insertion and deletion also take O(log n) time. The document explains how to implement searching, insertion, deletion and other common BST operations along with examples.

Uploaded by

Mr Mudassir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Search

• Sorted Array: Binary Search • O(log N)


• Linked List: Linear Search • 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
– 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
• 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)
• A BST is a binary tree with the
following properties:
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.
2. Both the left subtree and right subtree
are BSTs.
Binary Search Trees
Storage of elements for efficient access.
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 53 60
 

10
 31 62
37
20 Search path for 37

Search time O(h) where h is tree height


15

10 20

2 12 18 25

11 14 23 28
23

18 30

10 20 27 32

6 15 50
23

18 30

10 20 27 32

6 19 50

Violating the condition for BST


Inorder Traversal of BST

50

30 55

25 53 60
35
10
31 62
37
20

Prints out keys in sorted order: 10, 20, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62
search(12)
15

10 20

2 12 18 25

11 14 23 28

t
search(23)
15

10 20

2 12 18 25

11 14 23 28

t
Binary Search Tree
Search
TreeNode * BinaryTree::search(int key)
{
TreeNode *t = root;
while (t) {
if (t->data == key) break;
else if (t->data > key) t = t->left;
else t = t->right;
}
return t;
}
search(13)
15

10 20

2 12 18 25

11 14 23 28

t 
Tree-Minimum
TreeNode *Tree_Minimum()
{ TreeNode * x=root;
While (x->left != null)
x= x->left;
return x
}
Tree Maximum
TreeNode *Tree_Maximum()
{ TreeNode* x=root;
While (x->right != null)
x= x->right;
return x
}
Successor and Predecessor
10, 20, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62
x 50

30 55

25 35 53 60

10 The successor of x
31 62
37 is the leftmost node
in its right subtree
20
The predecessor of x is
the rightmost node in
its left subtree:
BST Operations Preview

25 25
Insert 8
10 37 10 37
15 30 65 8 15 30 65

The successor of 25
30
Delete 25
10 37

15 65
insert(19)
insert(13)
insert(4)
15

10 20

2 12 18 25

11 14 23 28
Binary Search Tree – Insert
void BinaryTree::insert(int data)
{
TreeNode *t1, *t2, *t3;
t1 = new TreeNode;
t1->left = NULL; t1->right = NULL;
t2 = root;
if (!root) root = t1;
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

10 20

2 12 18 25

11 14 23 28

1
3

 
Insertion
Example: insert z = 32 into the BST below

x 25
Compare 32 and 25
traverse the right subtree
20 35

12 40 insert 32 as
Compare 32 left child
and 35, y 25
traverse the 25
left subtree y
x 20 35
20 35

12 40 12 32 40
z
delete(12)
delete(14)
delete(2)
15

10 20

2 12 18 25

11 14 23 28

13
Delete a node from a BST
1. Locate the desired node by search; call it t
2. If t is a leaf, disconnect it from its parent
and set the pointer in the parent node equal
to 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 node
Operation: update the parent node to have an empty subtree.

25 25

15 40
15 40

20 30 45 45
20 30
17
x
Deletion (B)
Case B: node x has a left child but no right child
Operation: attach the left subtree of x to the parent

25 25

15 40
15 40

20 30 45 45
17 30
x
17
Deletion (C)
Case C: node x has a right child but no left child
Operation: attach the right subtree of x to the parent

25 25

x 15 40
20 40

20 30 45 45
17 30
17
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 value
greater 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.
r r
y
x
A A
B z B z
y
D C D
C
Deletion (D) - an Example
10, 25, 28, 30, 33, 34, 35, 40, 50, 65 10, 25, 28, 33, 34, 35, 40, 50, 65

40 40

30 33
65
65
35
25 35
25
50
50
10 28 28 34
33 10
replacement
of 30 34
Time Complexity

15

10 20

2 12 18 25

11 14 23 28

Search
Insert

Delete
insert(15) 15

insert(18) 18
insert(20)
insert(26) 20

insert(27)
26
insert(34)
27

34

Time Complexity
O(k) where k is the height
Height Balanced Trees
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

You might also like