You are on page 1of 36

UNIT-III Search Trees

Binary Tree
A binary tree consists of a finite set of nodes that is either empty, or consists of one
specially designated node called the root of the binary tree, and the elements of two
disjoint binary trees called the left subtree and right subtree of the root.
Note that the definition above is recursive: we have defined a binary tree in terms of
binary trees. This is appropriate since recursion is an innate characteristic of tree
structures.

Diagram 1: A binary tree

Binary Tree Terminology


Tree terminology is generally derived from the terminology of family trees
(specifically, the type of family tree called a lineal chart).
 Each root is said to be the parent of the roots of its subtrees.
 Two nodes with the same parent are said to be siblings; they are the children
of their parent.
 The root node has no parent.
 A great deal of tree processing takes advantage of the relationship between a
parent and its children, and we commonly say a directed edge (or
simply an edge) extends from a parent to its children. Thus edges connect a
root with the roots of each subtree. An undirected edge extends in both
directions between a parent and a child.
 Grandparent and grandchild relations can be defined in a similar manner;
we could also extend this terminology further if we wished (designating
nodes as cousins, as an uncle or aunt, etc.).

Other Tree Terms

 The number of subtrees of a node is called the degree of the node. In a


binary tree, all nodes have degree 0, 1, or 2.
 A node of degree zero is called a terminal node or leaf node.
 A non-leaf node is often called a branch node.
 The degree of a tree is the maximum degree of a node in the tree. A binary
tree is degree 2.
 A directed path from node n1 to nk is defined as a sequence of nodes n1, n2,
..., nk such that ni is the parent of ni+1 for 1 <= i < k. An undirected path is a
similar sequence of undirected edges. The length of this path is the number
of edges on the path, namely k – 1 (i.e., the number of nodes – 1). There is a
path of length zero from every node to itself. Notice that in a binary tree
there is exactly one path from the root to each node.
 The level or depth of a node with respect to a tree is defined recursively: the
level of the root is zero; and the level of any other node is one higher than
that of its parent. Or to put it another way, the level or depth of a node ni is
the length of the unique path from the root to ni.
 The height of ni is the length of the longest path from ni to a leaf. Thus all
leaves in the tree are at height 0.
 The height of a tree is equal to the height of the root. The depth of a tree is
equal to the level or depth of the deepest leaf; this is always equal to the
height of the tree.
 If there is a directed path from n1 to n2, then n1 is an ancestor of n2 and n2
is a descendant of n1.
Special Forms of Binary Trees

There are a few special forms of binary tree worth mentioning.


If every non-leaf node in a binary tree has nonempty left and right subtrees, the tree
is termed a strictly binary tree. Or, to put it another way, all of the nodes in a
strictly binary tree are of degree zero or two, never degree one. A strictly binary
tree with N leaves always contains 2N – 1 nodes.
Some texts call this a "full" binary tree.
A complete binary tree of depth d is the strictly binary tree all of whose leaves are
at level d.

The total number of nodes in a complete binary tree of depth d equals 2d+1 – 1. Since all
leaves in such a tree are at level d, the tree contains 2d leaves and, therefore, 2d - 1
internal nodes.

Diagram 2: A complete binary tree

A binary tree of depth d is an almost complete binary tree if:


 Each leaf in the tree is either at level d or at level d – 1.
 For any node nd in the tree with a right descendant at level d, all the left
descendants of nd that are leaves are also at level d.

Diagram 3: An almost complete binary tree


An almost complete strictly binary tree with N leaves has 2N – 1 nodes (as does
any other strictly binary tree). An almost complete binary tree with N leaves that is
not strictly binary has 2N nodes. There are two distinct almost complete binary
trees with N leaves, one of which is strictly binary and one of which is not.
There is only a single almost complete binary tree with N nodes. This tree is
strictly binary if and only if N is odd.

Representing Binary Trees in Memory

Array Representation

For a complete or almost complete binary tree, storing the binary tree as an array
may be a good choice.
One way to do this is to store the root of the tree in the first element of the array.
Then, for each node in the tree that is stored at subscript k, the node's left child can
be stored at subscript 2k+1 and the right child can be stored at subscript 2k+2. For
example, the almost complete binary tree shown in Diagram 2 can be stored in an
array like so:

However, if this scheme is used to store a binary tree that is not complete or almost
complete, we can end up with a great deal of wasted space in the array.
For example, the following binary tree

would be stored using this techinque like so:

Linked Representation

If a binary tree is not complete or almost complete, a better choice for storing it is
to use a linked representation similar to the linked list structures covered earlier in
the semester:
Each tree node has two pointers (usually named left and right). The tree class has
a pointer to the root node of the tree (labeled root in the diagram above).
Any pointer in the tree structure that does not point to a node will normally
contain the value NULL. A linked tree with N nodes will always contain N + 1
null links.
Tree Traversal:
Traversal is a process to visit all the nodes of a tree and may print their values too.
Because, all nodes are connected via edges (links) we always start from the root
(head) node. That is, we cannot randomly access a node in a tree. There are three
ways which we use to traverse a tree −
 In-order Traversal
 Pre-order Traversal
 Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or
to print all the values it contains.
In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the
right sub-tree. We should always remember that every node may represent a
subtree itself. If a binary tree is traversed in-order, the output will produce sorted
key values in an ascending order.

We start from A, and following in-order traversal, we move to its left subtree B. B
is also traversed in-order. The process goes on until all the nodes are visited. The
output of inorder traversal of this tree will be −

D→B→E→A→F→C→G
Algorithm

Until all nodes are traversed −


Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

Pre-order Traversal

In this traversal method, the root node is visited first, then the left subtree and
finally the right subtree.

We start from A, and following pre-order traversal, we first visit A itself and then
move to its left subtree B. B is also traversed pre-order. The process goes on until
all the nodes are visited. The output of pre-order traversal of this tree will be −

A→B→D→E→C→F→G

Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
Post-order Traversal

In this traversal method, the root node is visited last, hence the name. First we
traverse the left subtree, then the right subtree and finally the root node.

We start from A, and following Post-order traversal, we first visit the left subtree
B. B is also traversed post-order. The process goes on until all the nodes are
visited. The output of post-order traversal of this tree will be −
D→E→B→F→G→C→A

Algorithm

Until all nodes are traversed −


Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
Binary Search Tree
1. Binary Search tree can be defined as a class of binary trees, in which the nodes are arranged
in a specific order. This is also called ordered binary tree.

Binary search tree (BST) is a special kind of binary tree where each node contains-
2. Only larger values in its right subtree.
3. Only smaller values in its left subtree

left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)

A Binary search tree is shown in the above figure. As the constraint applied on the BST, we can
see that the root node 30 doesn't contain any value greater than or equal to 30 in its left sub-tree
and it also doesn't contain any value less than 30 in its right sub-tree.

Advantages of using binary search tree


1. Searching become very efficient in a binary search tree since, we get a hint at each step,
about which sub-tree contains the desired element.
2. The binary search tree is considered as efficient data structure in compare to arrays and
linked lists. In searching process, it removes half sub-tree at every step. Searching for an
element in a binary search tree takes o(log2n) time. In worst case, the time it takes to search
an element is 0(n).
3. It also speed up the insertion and deletion operations as compare to that in array and linked
list.

Q. Create the binary search tree using the following data elements.

43, 10, 79, 90, 12, 54, 11, 9, 50

1. Insert 43 into the tree as the root of the tree.


2. Read the next element, if it is lesser than the root node element, insert it as the root of the
left sub-tree.
3. Otherwise, insert it as the root of the right of the right sub-tree.

The process of creating BST by using the given elements, is shown in the image below.
Binary Search implementation
Binary Search Tree Operations-

Commonly performed operations on binary search tree are-

1. Search Operation-

Search Operation is performed to search a particular element in the Binary


Search Tree.

Rules-

For searching a given key in the BST,


 Compare the key with the value of root node.
 If the key is present at the root node, then return the root node.
 If the key is greater than the root node value, then recur for the root node’s right subtree.
 If the key is smaller than the root node value, then recur for the root node’s left subtree.
Example-

Consider key = 45 has to be searched in the given BST-

We start our search from the root node 25.


 As 45 > 25, so we search in 25’s right subtree.
 As 45 < 50, so we search in 50’s left subtree.
 As 45 > 35, so we search in 35’s right subtree.
 As 45 > 44, so we search in 44’s right subtree but 44 has no subtrees.
 So, we conclude that 45 is not present in the above BST.
2. Insertion Operation-

Insertion Operation is performed to insert an element in the Binary Search Tree.

Rules-

The insertion of a new key always takes place as the child of some leaf node.
For finding out the suitable leaf node,
 Search the key to be inserted from the root node till some leaf node is reached.
 Once a leaf node is reached, insert the key as child of that leaf node.

Example-
Consider the following example where key = 40 is inserted in the given BST-

 We start searching for value 40 from the root node 100.


 As 40 < 100, so we search in 100’s left subtree.
 As 40 > 20, so we search in 20’s right subtree.
 As 40 > 30, so we add 40 to 30’s right subtree.
3. Deletion Operation-
Deletion Operation is performed to delete a particular element from the Binary
Search Tree.

When it comes to deleting a node from the binary search tree, following three cases are possible-
Case-01: Deletion Of A Node Having No Child (Leaf Node)-

Just remove / disconnect the leaf node that is to deleted from the tree.
Example-

Consider the following example where node with value = 20 is deleted from the BST-

Case-02: Deletion Of A Node Having Only One Child-

Just make the child of the deleting node, the child of its grandparent.

Example-

Consider the following example where node with value = 30 is deleted from the BST-
Case-02: Deletion Of A Node Having Two Children-

A node with two children may be deleted from the BST in the following two ways-
Method-01:
 Visit to the right sub tree of the deleting node.
 Pluck the least value element called as in order successor.
 Replace the deleting element with its in order successor.
Example-

Consider the following example where node with value = 15 is deleted from the BST-

Method-02:

 Visit to the left sub tree of the deleting node.


 Pluck the greatest value element called as in order predecessor.
 Replace the deleting element with its in order predecessor.
Example-

Consider the following example where node with value = 15 is deleted from the BST-
AVL Tree
AVL tree is a height-balanced binary search tree. That means, an AVL tree is also a binary search
tree but it is a balanced tree. A binary tree is said to be balanced if, the difference between the
heights of left and right sub trees of every node in the tree is either -1, 0 or +1. In other words, a
binary tree is said to be balanced if the height of left and right children of every node differ by
either -1, 0 or +1. In an AVL tree, every node maintains an extra information known as balance
factor. The AVL tree was introduced in the year 1962 by G.M. Adelson-Velsky and E.M. Landis.
An AVL tree is defined as follows...
An AVL tree is a balanced binary search tree. In an AVL tree, balance factor of every node is either -1, 0 or +1.

Balance factor of a node is the difference between the heights of the left and right subtrees of that
node. The balance factor of a node is calculated either height of left sub tree - height of right
sub tree (OR) height of right sub tree - height of left sub tree. In the following explanation, we
calculate as follows...
Balance factor = heightOfLeftSubtree - heightOfRightSubtree
Example of AVL Tree

The above tree is a binary search tree and every node is satisfying balance factor condition. So
this tree is said to be an AVL tree.
Every AVL Tree is a binary search tree but every Binary Search Tree need not be AVL tree.

AVL Tree Rotations


In AVL tree, after performing operations like insertion and deletion we need to check the balance
factor of every node in the tree. If every node satisfies the balance factor condition then we
conclude the operation otherwise we must make it balanced. Whenever the tree becomes
imbalanced due to any operation we use rotation operations to make the tree balanced.

Rotation operations are used to make the tree balanced.

Rotation is the process of moving nodes either to left or to right to make the tree balanced.
There are four rotations and they are classified into two types.
Single Left Rotation (LL Rotation)
In LL Rotation, every node moves one position to left from the current position. To understand LL Rotation, let us
consider the following insertion operation in AVL Tree...

Single Right Rotation (RR Rotation)


In RR Rotation, every node moves one position to right from the current position. To understand RR Rotation, let us
consider the following insertion operation in AVL Tree...
Left Right Rotation (LR Rotation)
The LR Rotation is a sequence of single left rotation followed by a single right rotation. In LR Rotation, at first, every
node moves one position to the left and one position to right from the current position. To understand LR Rotation, let
us consider the following insertion operation in AVL Tree...

Right Left Rotation (RL Rotation)


The RL Rotation is sequence of single right rotation followed by single left rotation. In RL Rotation, at first every node
moves one position to right and one position to left from the current position. To understand RL Rotation, let us
consider the following insertion operation in AVL Tree...
Operations on an AVL Tree
The following operations are performed on AVL tree...

1. Search
2. Insertion
3. Deletion

Search Operation in AVL Tree


In an AVL tree, the search operation is performed with O(log n) time complexity. The search
operation in the AVL tree is similar to the search operation in a Binary search tree. We use the
following steps to search an element in AVL tree...

 Step 1 - Read the search element from the user.


 Step 2 - Compare the search element with the value of root node in the tree.
 Step 3 - If both are matched, then display "Given node is found!!!" and terminate the
function
 Step 4 - If both are not matched, then check whether search element is smaller or larger
than that node value.
 Step 5 - If search element is smaller, then continue the search process in left subtree.
 Step 6 - If search element is larger, then continue the search process in right subtree.
 Step 7 - Repeat the same until we find the exact element or until the search element is
compared with the leaf node.
 Step 8 - If we reach to the node having the value equal to the search value, then display
"Element is found" and terminate the function.
 Step 9 - If we reach to the leaf node and if it is also not matched with the search element,
then display "Element is not found" and terminate the function.
Insertion Operation in AVL Tree
In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL
Tree, a new node is always inserted as a leaf node. The insertion operation is performed as
follows...

 Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.
 Step 2 - After insertion, check the Balance Factor of every node.
 Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.
 Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said to
be imbalanced. In this case, perform suitable Rotation to make it balanced and go for next
operation.

Example: Construct an AVL Tree by inserting numbers from 1 to 8.




Deletion Operation in AVL Tree
The deletion operation in AVL Tree is similar to deletion operation in BST. But after
every deletion operation, we need to check with the Balance Factor condition. If the
tree is balanced after deletion go for next operation otherwise perform suitable rotation
to make the tree Balanced.
Red Black Tree

A Red Black Tree is a type of self-balancing binary search tree, in which every node is colored
with a red or black. The red black tree satisfies all the properties of the binary search tree but there
are some additional properties which were added in a Red Black Tree. The height of a Red-Black
tree is O(Logn) where (n is the number of nodes in the tree).

Properties of Red Black Tree

1. The root node should always be black in color.


2. Every null child of a node is black in red black tree.
3. The children of a red node are black. It can be possible that parent of red node is black
node.
4. All the leaves have the same black depth.
5. Every simple path from the root node to the (downward) leaf node contains the same
number of black nodes.

Representation of Red Black Tree


While representing the red black tree color of each node should be shown. In this tree leaf nodes
are simply termed as null nodes which means they are not physical nodes. It can be checked easily
in the above-given tree there are two types of node in which one of them is red and another one is
black in color. The above-given tree follows all the properties of a red black tree that are

1. It is a binary search tree.


2. The root node is black.
3. The children’s of red node are black.
4. All the root to external node paths contain same number of black nodes.
Example: Consider path 75-90-80-88-null and 75-40-30-null in both these paths 3 black
nodes are there.

Advantages of Red Black Tree

1. Red black tree are useful when we need insertion and deletion relatively frequent.
2. Red-black trees are self-balancing so these operations are guaranteed to be O(logn).
3. They have relatively low constants in a wide variety of scenarios.

Insertion in Red black Tree

 Every node which needs to be inserted should be marked as red.


 Not every insertion causes imbalancing but if imbalancing occurs then it can be removed,
depending upon the configuration of tree before the new insertion is made.
 In Red black tree if imbalancing occurs then for removing it two methods are used that
are: 1) Recoloring and 2) Rotation

To understand insertion operation, let us understand the keys required to define the following
nodes:

1. Let u is newly inserted node.


2. p is the parent node of u.
3. g is the grandparent node of u.
4. Un is the uncle node of u.

When insertion occurs the new node is inserted which is already a balanced tree. But after
inserting many nodes there can be an imbalancing condition occurs which violates the property of
the red black tree. So in this case first we try to recolor first then we go for a rotation.
Case 1)

The imbalancing is concerned with the color of grandparent child i.e. Uncle Node. If uncle node is
red then there are four cases then in this, by doing recoloring imbalancing can be removed.

1) LRr imbalance

In this Red Black Tree violates its property in such a manner that parent and inserted child will be
in a red color at a position of left and right with respect to grandparent. Therefore it is termed as
Left Right imbalance.

Removal of LRr imbalance can be done by:

i. Change color of p from red to black.


ii. Change color of Un from red to black.
iii. Change color of g from black to red, provided g is not a root node.

Note: If given g is root node then there will be no changes in color of g.

2) LLr imbalance

In this red black tree violates its property in such a manner that parent and inserted child will be in
a red color at a position of left and left with respect to grandparent. Therefore it is termed as LEFT
LEFT imbalance.
Removal of LLr imbalance can be done by:

i. Change color of p from red to black.


ii. Change color of Un from red to black.
iii. Change color of g from black to red, provided g is not a root node.

Note: If given g is root node then there will be no changes in color of g.

3) RRr imbalance

In this red black tree violates its property in such a manner that parent and inserted child will be in
a red color at a position of right and right with respect to grandparent. Therefore it is termed as
RIGHT RIGHT imbalance.
Removal of RRr imbalance can be done by:

i. Change color of p from red to black.


ii. Change color of Un from red to black.
iii. Change color of g from black to red, provided g is not a root node.

Note: If given g is root node then there will be no changes in color of g.

4) RLr imbalance

In this red black tree violates its property in such a manner that parent and inserted child will be in
a red color at a position of right and left with respect to grandparent. Therefore it is termed as
RIGHT LEFT imbalance.
Removal of RLr imbalance can be done by:

i. Change color of p from red to black.


ii. Change color of Un from red to black.
iii. Change color of g from black to red, provided g is not a root node.

Note: If given g is root node then there will be no changes in color of g.

Case 2)

The imbalancing can also be occurred when the child of grandparent i.e. uncle node is black. Then
know also four cases will be arises and in this case imbalancing can be removed by using rotation
technique.

1. LR imbalancing
2. LL imbalancing
3. RR imbalancing
4. RL imbalancing

LL and RR imbalancing can be removed by following two steps i.e. are:

a. Apply single rotation of p about g.


b. Recolor p to black and g to red.
LR and RL imbalancing can be removed by following steps:

a. Apply double rotation of u about p followed by u about g.


b. For LR imbalancing recolor u to black and recolor p and g to red.
c. For RL imbalancing recolor u to black and recolor g and p to red.
Note:

1. While inserting any node its color is by default red.


2. If uncle node is NULL then it will be consider as black.
splay trees
A splay tree is a self-balancing binary search tree with the additional property that recently
accessed elements are quick to access again. It performs basic operations such as insertion,
look-up and removal in O(log n) amortized time. For many sequences of non-random
operations, splay trees perform better than other search trees, even when the specific pattern
of the sequence is unknown. The splay tree was invented by Daniel Sleator and Robert
Tarjan in 1985.
All normal operations on a binary search tree are combined with one basic operation,
called splaying. Splaying the tree for a certain element rearranges the tree so that the
element is placed at the root of the tree. One way to do this with the basic search operation
is to first perform a standard binary tree search for the element in question, and then
use tree rotations in a specific fashion to bring the element to the top. Alternatively, a top-
down algorithm can combine the search and the tree reorganization into a single phase.

Advantages

Good performance for a splay tree depends on the fact that it is self-optimizing, in that frequently
accessed nodes will move nearer to the root where they can be accessed more quickly. The worst-
case height—though unlikely—is O(n), with the average being O(log n). Having frequently-used
nodes near the root is an advantage for many practical applications (also see Locality of
reference), and is particularly useful for implementing caches and garbage collection algorithms.
Advantages include:

 Comparable performance: Average-case performance is as efficient as other trees. [2]


 Small memory footprint: Splay trees do not need to store any bookkeeping data.

Disadvantages

The most significant disadvantage of splay trees is that the height of a splay tree can be linear. For
example, this will be the case after accessing all n elements in non-decreasing order. Since the
height of a tree corresponds to the worst-case access time, this means that the actual cost of an
operation can be high. However the amortized access cost of this worst case is logarithmic,
O(log n). Also, the expected access cost can be reduced to O(log n) by using a randomized
variant.[3]
The representation of splay trees can change even when they are accessed in a 'read-only' manner
(i.e. by find operations). This complicates the use of such splay trees in a multi-threaded
environment. Specifically, extra management is needed if multiple threads are allowed to
perform find operations concurrently. This also makes them unsuitable for general use in purely
functional programming, although even there they can be used in limited ways to implement
priority queues.
Operations
When a node x is accessed, a splay operation is performed on x to move it to the root. To perform
a splay operation we carry out a sequence of splay steps, each of which movesx closer to the root.
By performing a splay operation on the node of interest after every access, the recently accessed
nodes are kept near the root and the tree remains roughly balanced, so that we achieve the desired
amortized time bounds.
Each particular step depends on three factors:

 Whether x is the left or right child of its parent node, p,


 whether p is the root or not, and if not
 whether p is the left or right child of its parent, g (the grandparent of x).
It is important to remember to set gg (the great-grandparent of x) to now point to x after any
splay operation. If gg is null, then x obviously is now the root and must be updated as such.
There are three types of splay steps, each of which has two symmetric variants: left- and right-
handed. For the sake of brevity, only one of these two is shown for each type. These three types
are:
Zig step: this step is done when p is the root. The tree is rotated on the edge between x and p. Zig
steps exist to deal with the parity issue and will be done only as the last step in a splay operation
and only when x has odd depth at the beginning of the operation.

Zig-zig step: this step is done when p is not the root and x and p are either both right children or
are both left children. The picture below shows the case where x and p are both left children. The
tree is rotated on the edge joining p with its parent g, then rotated on the edge joining x with p.
Note that zig-zig steps are the only thing that differentiate splay trees from the rotate to
root method introduced by Allen and Munro[4] prior to the introduction of splay trees.
Zig-zag step: this step is done when p is not the root and x is a right child and p is a left child or
vice versa. The tree is rotated on the edge between p and x, and then rotated on the resulting edge
between x and g.

Join
Given two trees S and T such that all elements of S are smaller than the elements of T, the
following steps can be used to join them to a single tree:

 Splay the largest item in S. Now this item is in the root of S and has a null right child.
 Set the right child of the new root to T.
Split
Given a tree and an element x, return two new trees: one containing all elements less than or equal
to x and the other containing all elements greater than x. This can be done in the following way:

 Splay x. Now it is in the root so the tree to its left contains all elements smaller than x and the
tree to its right contains all element larger than x.
 Split the right subtree from the rest of the tree.
Insertion
To insert a value x into a splay tree:

 Insert x as with a normal binary search tree.


 when an item is inserted, a splay is performed.
 As a result, the newly inserted node x becomes the root of the tree.
ALTERNATIVE:
 Use the split operation to split the tree at the value of x to two sub-trees: S and T.
 Create a new tree in which x is the root, S is its left sub-tree and T its right sub-tree.
Deletion
To delete a node x, use the same method as with a binary search tree: if x has two children, swap
its value with that of either the rightmost node of its left sub tree (its in-order predecessor) or the
leftmost node of its right subtree (its in-order successor). Then remove that node instead. In this
way, deletion is reduced to the problem of removing a node with 0 or 1 children. Unlike a binary
search tree, in a splay tree after deletion, we splay the parent of the removed node to the top of the
tree.
ALTERNATIVE:

 The node to be deleted is first splayed, i.e. brought to the root of the tree and then deleted.
leaves the tree with two sub trees.
 The two sub-trees are then joined using a "join" operation.

You might also like