Вы находитесь на странице: 1из 58

DATA STRUCTURES AND

ALGORITHMS
MODULE 4
Binary search trees
BST insertion

BINARY SEARCH TREE


A Binary search tree is a binary tree->
It may be empty->
If it is not empty, then it satisfies the following properties:
1. Every element has a key and no two elements have
the same key (no duplicates)
2. The keys (if any) in the left subtree are smaller than
the key in the root
3. The keys (if any) in the right subtree are larger than
the key in the root
4. The left and right subtrees are also binary search
trees

Eg: BINARY SEARCH TREE


root
7

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Insert 4

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Insert 4

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Insert 4

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Insert 4

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Insert 3

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Insert 3

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Insert 3

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Insert 3

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Insert 3

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Insert 3

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Insert 3

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
Insert 5

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
Insert 4

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
Insert 4

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
Insert 4

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
Insert 4

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
Insert 4

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Construction of binary search tree


Insert 7,2,1,4,3,8,5,9
root
7

Construct a binary search tree

BST : INSERT(5)
Newnode=GETSPACE(newnode)
Newnode->data=item
Newnode->lchild=NULL
Newnode->rchild=NULL

5
5
X
X
newnode

Root
=NULL

BST : INSERT(5)
If(root=NULL)
Root=newnode

Root
=NULL
Root
X
5
X
5
X
X
newnode

BST : INSERT(2)
Newnode=GETSPACE(newnode)
Newnode->data=item
Newnode->lchild=NULL
Newnode->rchild=NULL
Root
X
5
X
5
X
X

2
2
X
X
newnode

BST : INSERT(2)
If(root !=NULL)
ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else
ptr=ptr->lchild
EndIf
EndWhile

2
2
X
X
newnode

Root
X
5
X
5
X
X
ptr

BST : INSERT(2)
If(root !=NULL)
ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else
ptr=ptr->lchild
EndIf
EndWhile

2
2
X
X
newnode

Root
X
5
parent
X
5
X
X
ptr

BST : INSERT(2)
If(root !=NULL)
ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else
ptr=ptr->lchild
EndIf
EndWhile

2
2
X
X
newnode

Root
X
5
parent
X
5
X
X
ptr= NULL

BST : INSERT(2)
If(item>parent->data)
Parent->rchild=newnode
Else
Parent->lchild=newnode
EndIf
Root
5
parent
5
X
X
ptr= NULL
2
2
X
X
newnode

BST : INSERT(7)
Newnode=GETSPACE(newnode)
Newnode->data=item
Newnode->lchild=NULL
Newnode->rchild=NULL
Root
5
5
X
X

2
2
X
X

7
7
X
X
newnode

BST : INSERT(7)
If(root !=NULL)
ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else
ptr=ptr->lchild
EndIf
EndWhile

2
2
X
X

Root
5
5
X
X

parent
ptr

7
7
X
X
newnode

BST : INSERT(7)
If(root !=NULL)
ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else
ptr=ptr->lchild
EndIf
EndWhile

2
2
X
X

Root
5
5
X
X

parent
ptr= NULL

7
7
X
X
newnode

BST : INSERT(7)
If(item>parent->data)
Parent->rchild=newnode
Else
Parent->lchild=newnode
EndIf
Root
5

2
2
X
X

parent
ptr

7
7
X
X
newnode

Insert an element into a binary search


tree

BST : Algorithm
Root
10
10

1
1
X
X

X
X

5
5
X
X

13
13
X

8
8
X
X

12
12
X
X

15
15
X
X

BST : insert (9)


Where to insert 9?

Root
10
10

1
1
X
X

X
X

5
5
X
X

13
13
8
8
X
X

12
12
X
X

9
9
X
X

15
15
X
X

BST : insert (9)


Where to insert 9?

Root
10
10

1
1
X
X

13
13

parent
X 8
X 8

5
5
X
X

12
12
X
X

9
9
X
X
newnod
e

15
15
X
X

Newnode=GETSPACE(newn
BST : insert (9)
ode)
Newnode->data=item
Newnode->lchild=NULL
Newnode->rchild=NULL

Root
10
10

1
1
X
X

X
X

5
5
X
X

13
13

12
12
X
X

9
9
X
X
newnod
e

15
15
X
X

If(root=NULL)
BST : insert (9)
Root=newnode
Root
10
10

1
1
X
X

X
X

5
5
X
X

13
13

12
12
X
X

9
9
X
X
newnod
e

15
15
X
X

ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else If(item<ptr->data)
ptr=ptr->lchild
Else
exit
EndIf
EndWhile

BST : insert (9)


Find the position

Root
10
10

1
1
X
X

X
X

5
5
X
X

13
13

12
12
X
X

9
9
X
X
newnod
e

15
15
X
X

ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else If(item<ptr->data)
ptr=ptr->lchild
Else
exit
EndIf
endwhile

BST : insert (9)


ptr
Root
10
10

1
1
X
X

X
X

5
5
X
X

13
13

12
12
X
X

9
9
X
X
newnod
e

15
15
X
X

ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else If(item<ptr->data)
ptr=ptr->lchild
Else
exit
EndIf
endwhile

BST : insert (9)


paren
tptr
Root
10
10

1
1
X
X

X
X

5
5
X
X

13
13

12
12
X
X

9
9
X
X
newnod
e

15
15
X
X

ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else If(item<ptr->data)
ptr=ptr->lchild
Else
exit
EndIf
endwhile

BST : insert (9)


paren
tptr
Root
10
10
ptr
6

1
1
X
X

X
X

5
5
X
X

13
13

12
12
X
X

9
9
X
X
newnod
e

15
15
X
X

ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else If(item<ptr->data)
ptr=ptr->lchild
Else
exit
EndIf
endwhile

BST : insert (9)


paren
t
Root
10
10

paren
t
ptr
6

1
1
X
X

X
X

5
5
X
X

13
13

12
12
X
X

9
9
X
X
newnod
e

15
15
X
X

ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else If(item<ptr->data)
ptr=ptr->lchild
Else
exit
EndIf
endwhile

BST : insert (9)


Root
10
10

paren
t
ptr
6

13
13

ptr
3

1
1
X
X

X
X

5
5
X
X

12
12
X
X

9
9
X
X
newnod
e

15
15
X
X

ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else If(item<ptr->data)
ptr=ptr->lchild
Else
exit
EndIf
endwhile

BST : insert (9)


Root
10
10

paren
t
6

1
1
X
X

6
paren
ptr
t

X
X

5
5
X
X

13
13

12
12
X
X

9
9
X
X
newnod
e

15
15
X
X

ptr=root
While(ptr!=NULL)
Parent=ptr
If(item>ptr->data)
ptr=ptr->rchild
Else If(item<ptr->data)
ptr=ptr->lchild
Else
exit
EndIf
endwhile

BST : insert (9)


Root
10
10

1
1
X
X

paren
t
X 8 X
X 8 X

5
5
X
X

13
13

12
12
X
X

9
X
9
X
X
newnod
e

15
15
X
X
ptr=NULL

If(item>parent->data)
Parent>rchild=newnode
Else
Parent>lchild=newnode
EndIf

BST : insert (9)


Root
10
10

1
1
X
X

paren
t
X 8 X
X 8 X

5
5
X
X

13
13

12
12
X
X

9
X
9
X
X
newnod
e

15
15
X
X
ptr=NULL

BST : 9 inserted
Root
10
10

1
1
X
X

13
13

X
X

5
5
X
X

12
12
X
X

9
9
X
X

15
15
X
X

BST : INSERTION
6.

Else
1. ptr=root
2. While(ptr!=NULL)

1. Parent=ptr
Algorithm BST_INSERT(item)
2. If(item>ptr->data)
Input: Item is the data component of a node
1. ptr=ptr->rchild
that has to be inserted->
Output: Item inserted in to the proper place of
3. Else If(item<ptr->data)
the tree->
1. ptr=ptr->lchild
Data structure: Linked structure
4. Else

1. Print(duplicate node);
Steps:
2. exit
1. Newnode=GETSPACE(newnode
5. EndIf
)
3. EndWhile
2. Newnode->data=item
4. If(item>parent->data)
1. Parent->rchild=newnode
3. Newnode->lchild=NULL
5. Else
4. Newnode->rchild=NULL
1. Parent->lchild=newnode
5. If(root=NULL)
6. EndIf
1. Root=newnode
7.
EndIf
8.
Stop

Вам также может понравиться