Sunteți pe pagina 1din 15

Binary Search Tree

A binary search tree is a data structure, which meets the following requirement

 It is a binary tree.
 Each node contains a value.
 Left subtree of a node contains only lesser than node’s value.
 Right subtree of node contains only greater than node’s value.

Example of Binary Search Tree:-

An important application of binary search tree is their use in searching. This structure enables one to search for and find
an element with an average running time. It is also enables one to easily in
insert
sert and delete elements.

Operation on BST:-

1. Search for a value


2. Add a new value.
3. Remove a value

1. Search for a value

Searching for a value in a BST is very similar to add operation. Search algorithm traverses the tree "in-depth",
"in choosing
appropriate way to go, following binary search tree property and compares value of each visited node with the one, we
are looking for. Algorithm stops in two cases:

• a node with necessary value is found;


• algorithm has no way to go.

Search algorithm in detail

Now, let's see more detailed description of the search algorithm. Like an add operation, and almost every operation on
BST, search algorithm utilizes recursion. Starting from the root,

1. check, whether value in current node and searched value are equal. If so, value is found. Otherwise,
2. if searched value is less, than the node's value:
o if current node has no left child, searched value doesn't exist in the BST;
o otherwise, handle the left child with the same algorithm.
3. if a new value is greater, than the node's value:
o if current node has no right child, searched value doesn't exist in the BST;
o otherwise, handle the right child with the same algorithm.

Just before code snippets, let us have a look on the example, demonstrating searching for a value in the binary search
tree.

Example

Search for 3 in the tree, shown above.

2. Adding a value

Adding a value to BST can be divided into two stages:

• search for a place to put a new element;


• insert the new element to this place.

Let us see these stages in more detail.


Search for a place

At this stage an algorithm should follow binary search tree property. If a new value is less, than the current node's value,
go to the left subtree; else go to the right subtree. Following this simple rule, the algorithm reaches a node, which has
no left or right subtree. By the moment a place for insertion is found, we can say for sure, that a new value has no
duplicate in the tree. Initially, a new node has no children, so it is a leaf. Let us see it at the picture. Gray circles indicate
ind
possible places for a new node.

Now, let's go down to algorithm itself. Here and in almost every operation on BST recursion is utilized. Starting from the
root,

1. check, whether value in current node and a new value are equal. If so, duplicate is found. Otherwise,
2. if a new value is less, than the node's value:
o if a current node has no left child, place for insertion has been found;
o otherwise, handle the left child with the same algorithm.
3. if a new value is greater, than the node's value:
o if a current node has no right child, place for insertion has been found;
o otherwise, handle the right child with the same algorithm.

Just before code snippets, let us have a look on the example, demonstrating a case of insertion in the binary search tree.

Example

Insert 4 to the tree, shown above.


3. Removing a node

Remove operation on binary search tree is more complicated, than add and search. Basically, in can be divided into two
stages:

• search for a node to remove;


• if the node is found, run remove algorithm.

Remove algorithm in detail

Now, let's see more detailed description of a remove algorithm. First stage is identical to algorithm for searching, except
we should track the parent of the current node. Seco
Second
nd part is more tricky. There are three cases, which are described
below.

Case 1. Node to be removed has no children.

This case is quite simple. Algorithm sets corresponding link of the parent to NULL and disposes the node.

Example. Remove -4 from a BST.

Case 2. Node to be removed has one child.


It this case, node is cut from the tree and algorithm links single child (with it's subtree) directly to the parent of
the removed node.

Example. Remove 18 from a BST.

Case 3. Node to be removed has two children.

This is the most complex case. To solve it, let us see one useful BST property first. We are going to use the idea,
that the same set of values may be represented as different binary
binary-search
search trees. For example those BSTs:

contains the same values {5, 19, 21, 25}. To transform first tree into second one, we can do following:
o choose minimum element from the right subtree (19 in the example);
o replace 5 by 19;
o hang 5 as a left child.

The same approach can be utilized to remove a node, which has two children:

o find a minimum value in the right subtree;


o replace value of the node to be removed with found minimum. Now, right subtree contains a duplicate!
o apply remove to the right subtree to remove a duplicate.

Notice, that the node with


ith minimum value has no left child and, therefore, it's removal may result in first or
second cases only.

Example. Remove 12 from a BST.

Find minimum element in the right subtree of the node to be removed. In current example it is 19.

Replace 12 with 19. Notice, that only values are replaced, not nodes. Now we have two nodes with the same
value.
Remove 19 from the left subtree.

**************************************************************************************************

Height Balance Tree (AVL Tree)


An AVL tree is a special type of binary tree that is always "partially" balanced. The criteria that is used to determine the
"level" of "balanced-ness"
ness" is the difference between the heights of subtrees of a root in the tree. The "height" of tree is
the "number
mber of levels" in the tree. Or to be more formal, the height of a tree is defined as follows:

1. The height of a tree with no elements is 0


2. The height of a tree with 1 element is 1
3. The height of a tree with > 1 element is equal to 1 + the height of its ta
tallest subtree.

If ‘T’ is a binary tree with TL and TR are left and right subtree respectively, then T is a height balance tree if
Height of lest subtree is either one more or one less or equal with the Height of the right subtree.

In this case the balance factor of a node is BF=Height of left subtree – Height of right subtree

Here if two subtrees are of same height, then BF=0


If right subtree is higher, then BF=-1
If left subtree is higher, then B=+1

The Balance Factor of a node in an AVL Tree is either 0, +1 or -1.


If the balance factor of any node is ±2 or more then it is called pivot node. We need to rotate the tree to make balance
the height of the tree by using varieties of rotation technique.
Due to Russian Mathematician G.M Adelson Velskii and E.M Lendis the tree is named like AVL tree.

Types of Rotation:-
1. LL rotation
2. RR rotation
3. LR rotation
4. RL rotation

1. LL rotation:-
It is used when a new the new node is inserted in the left subtree of left subtree of a root node.

An example of a single rotation is as follows: Suppose I have a tree that looks like this:
C
BF of B=0
BF of C=+1

Now I insert the item "a" and get the resulting binary tree:

C
BF of A=0
BF of B=+1
B BF of C=+2

Now, this resulting tree violates the "AVL criteria", the left subtree has a height of 2 but the right subtree has a height of
0 so the difference in the two heights is "2" (which is greater than 1). Then rotate the tree by taking the immediate
parent of the node which is inserted at last and due to which the tree is unbalanced, as the ROOT node. The rotation is
clockwise according to BST properties.
B

A C

This tree is now balanced.


2. RR rotation:-
It is used when a new the new node is inserted in the right subtree of right subtree of a root node.

An example of a single rotation is as follows: Suppose I have a tree that looks like this:
A
BF of B=0
BF of A=-1

Now I insert the item "C" and get the resulting binary tree:

A
BF of C=0
BF of B=-1
B BF of A=-2

Now, this resulting tree violates the "AVL criteria", the right subtree has a height of 2 but the left subtree has a height of
0 so the difference in the two heights is "2" (which is greater than 1). Then rotate the tree by taking the immediate
parent of the node which is inserted at last and due to which the tree is unbalanced, as the ROOT node. The rotation is
anti clockwise according to BST properties.
B

A C

This tree is now balanced.

3. LR Rotation:-
It is used when a new the new node is inserted in the right subtree of left subtree of a root node.

An example of a single rotation is as follows: Suppose I have a tree that looks like this:

C
Now I insert the item "B" and get the resulting binary tree:

C BF of B=0
BF of C=-1
Bf of A=-2
A

This resulting tree also violates the "AVL criteria" so we fix it by first rotating "A" down to the left (so we get "C-B-A"),
and then rotating "C" down to the right so that the tree is transformed into this:

A C

4. RR Rotation:-
It is used when a new the new node is inserted in the right subtree of left subtree of a root node.

An example of a single rotation is as follows: Suppose I have a tree that looks like this:

Now I insert the item "C" and get the resulting binary tree:

BF of B=0
A
BF of C=+1

BF of A=-2
C

B
This resulting tree also violates the "AVL criteria" so we fix it by first rotating "C" down to the right (so we get "A-B-C"),
and then rotating "A" down to the left so that the tree is transformed into this:

Note: In case of LR & RL Rotation, rotate the tree by taking the node which is inserted at last
and due to which the Tree is unbalanced as the ROOT node. Then rotation may be clockwise or
anti clockwise according to the rules of BST.

Example:
Insert the following nodes into the AVL tree.
55,66,77,15,11,99,100,89,88

Solution:
0 -1 55 -2
55 55 66 55 77
-1
66
66 0

77 0

0 +1
66 66

RR 15
0 0 55 +1 77 0
55 77

15 0

+2
66
66 +1

11 55 +2 77 0 0 0
LL 77
15

15 +1 0 0
11 55

11 0
66 66
0 -1

99 0 -1 100 0 -2
15 77 77
15

0 0 99 0 0 0 99 -1
11 55 55
11

100 0

0
66

15 0 99 0
RR

11 0 55 0 77 0 100 0

66 -1

89
15 0 99 +1

11 100
0 55 0 77 -1 0

89
-2 0
66

15 0 99 +2

88

11 55 77 100
0 0 -2 0

+1
89

88
-1
66

88 0 +1
15 99

11 0 0 0 100 0
55 88

77 0 89 0

Deletion of node from AVL Tree:-

Step-1: Let T holds the address of the Root of the Tree.

Step-2: If T==NULL; Then Print “No Node is there to be deleted” and exit.

Step-3: Read ITEM to be delete.

Step-4: Case1

--- If deleted ITEM is leaf node, then simply delete it

--- Find out new Balance Factor

--- If required rebalance the Tree

(End of Case1)

Step-5: Case2

--- If deleted ITEM is a non leaf node, then replace the value with its in-order predecessor.

--- Find out new Balance Factor

--- If required rebalance the Tree

(End of Case2)

Step-6: Exit
Example: Given that the AVL Tree

0
5

3 +1 8 0

2 10
+1 4 0 7 +1 0

1 0 0 9 0 11 0
6

Delete the Node 4 from the AVL Tree

Solution: Here the deleted item or node is leaf node that’s why just delete the ITEM and find the Balance Factor of each
Node.

5 0

3 +2 8 +1

2 10
+1 7 +1 0

1 6 9 11
0 0 0 0

After finding the new BF, we need to rebalance the Tree due to node 3.

So applying the LL Rotation to make balance.


5

2 8

1 10
3 7

6 9 11

Now the Tree is balanced.

Delete the node 8 from the AVL Tree.

Solution: Here the ITEM is an non leaf, so applying the case2 of the algorithm i.e. replacing the node with its in-order
predecessor.

In-order Traversal: 1 2 3 5 6 7 8 9 10 11.


So the in-order predecessor of 8 is 7. So replace 8 with 7. Then find the new BF of each node.

-1
5

2 0 7 -1

10
1 0 3 0 6 0 0

9 0 11 0

Now the Tree is completely Balance.

S-ar putea să vă placă și