DS_UNIT_IV[1]
DS_UNIT_IV[1]
I/II-SEM
UNIT IV
Trees: Basic Terminology in Trees, Binary Trees-Properties, Representation of Binary
Trees using Arrays and Linked lists. Binary Search Trees- Basic Concepts, BST
Operations: Insertion, Deletion, Tree Traversals, Applications-Expression Trees, Heap
Sort, Balanced Binary Trees- AVL Trees, Insertion, Deletion and Rotations.
--------------------------------------------------------------------------------------------------------------------------------
-
TREES:-
A tree is a non-linear data structure that is used to represents hierarchical
relationships between individual data items. “A tree is a finite set of one or more
nodes such that, there is a specially designated node called root. The remaining
nodes are partitioned into n>=0 disjoint sets T1, T2,..Tn, where each of these set is a
tree T1,…Tn are called the subtrees of the root.”
Root: An Unique node in the tree to which subtrees are attached.
Subtree: A Subtree is a subset of a tree that is itself a tree.
Leaf node: A node with no children is called a leaf.
Path: A sequence of consecutive edges is called a path.
Ancestor node: Ancestor of a node is any predecessor node on the path from root to
that node. Hence root is ancestor of all the nodes in the tree and it doesn’t have any
ancestors.
Descendant node: Descendant of a node is any successor node on any path from
the node to a leaf node. Leaf nodes do not have any descendants.
Level: Level of a node is defined by letting root at level one. If a node is at level L,
then its children are at level L + 1.
Degree: It is equal to the number of children that a node has. The degree of leaf
node is zero.
In-Degree: It is the number of edges arriving at that node.
Out-degree: It is the number of edges leaving that node.
Parent: The node having the sub-branches.
Siblings: All nodes that are at same level and share the same parent are said to be
siblings.
Branch: Branch is the link between the parent and its child.
Children: The nodes branching from a particular node X are called children of X.
Height or depth: The height or depth of a tree is defined to be the maximum level
of any node in the tree.
Climbing: The process of traversing the tree from the leaf to the root is called
climbing the tree.
Descending: The process of traversing the tree from the root to the leaf is
called descending the tree.
Forest: It is a collection of disjoint
trees. It is obtained by removing root.
Non – Terminals: The nodes other
than root node and leaf nodes.
Predecessor: Consider the node X,
then the node previous to node X is
called predecessor node.
Successor: Consider the node X, then
the node that comes next to node X is
called successor node.
Unit-IV TREES
Binary Trees:-
A binary tree is a tree either empty or consists two
disjoint binary trees called the left subtree and right
subtree.
Left child: The node present to the left of the parent node
is called the left child.
Right child: The node present to the right of the parent
node is called the right child.
Types of Binary Trees:-
Skewed Binary tree: If the new nodes in the tree are added only to one side of the
binary tree then it is a skewed binary tree.
Strictly binary tree: If the binary tree has each node consisting of either two nodes
or no nodes at all, then it is called a strictly binary tree.
Complete binary tree: A complete binary tree is a binary tree that satisfies 2
properties. First, in a complete binary tree, every level, except possibly the last, is
completely filled. Second, all nodes appear as far left as possible.
Properties Of Binary Trees:-
1. The maximum number of nodes on level i of a binary tree is 2i-1,i>=1
2. The maximum number of nodes in a binary tree of depth k is 2k -1, k>=1.
3. The total number of edges in a full binary tree with n node is n - 1.
Binary Tree Representation:-
There are two ways in which a binary tree can be represented. They are:
1. Array representation of binary trees.
2. Linked representation of binary trees.
Array Representation Of Binary Trees:-
When arrays are used to represent the binary trees, then an array of size 2 k is
declared where, k is the depth of the tree. For example if the depth of the binary tree
is 3, then maximum 23 - 1 = 7 elements will be present in the node and hence the
array size will be 8. This is because the elements are stored from position one leaving
the position 0 vacant. But an array of bigger size is declared so that later new nodes
can be added to the existing tree. The following binary tree can be represented using
arrays as shown.
The root element is always stored in position 1. The left child of node i is stored in
position 2i and right child of node is stored in position 2i + 1. The formulas for
identifying the parent, left child and right child of a particular node.
Parent( i ) = i / 2, if I ≠ 1. If i = 1 then i is the root node and root does not has
parent.
Left child( i ) = 2i, if 2i ≤ 2 n, where n is the maximum number of elements in the
tree. If 2i > n, then i has no left child.
Right child( i ) = 2i + 1, if 2i + 1 ≤ 2 n. If 2i + 1 > n, then i has no right child.
The empty positions in the tree where no node is connected are represented in the
array using -1, indicating absence of a node. Using the formula, we can see that for a
node 3, the parent is 3/2 is 1. Referring to the array locations, we find that 50 is the
parent of 40. The left child of node 3 is 2*3 is 6. But the position 6 consists of -1
indicating that the left child does not exist for the node 3. Hence 50 does not have a
left child. The right child of node 3 is 2*3 + 1 is 7. The position 7 in the array consists
of 20. Hence, 20 is the right child of 40.
Linked Representation Of Binary Trees:-
In linked representation of binary trees, instead of arrays, pointers are used to
connect the various nodes of the tree. Hence each node of the binary tree consists of
three parts namely, the data, left and right. The data part stores the data, left part
stores the address of the left child and the right part stores the address of the right
child.
struct binarytree
{
struct binarytree *LeftChild;
int data;
struct binarytree *RightChild;
};
struct binarytree node;
node *root = NULL;
Logically the binary tree in linked form can be represented as shown.
The pointers storing NULL value indicates that there is no node attached to it.
Traversing through this type of representation is very easy. The left child of a
particular node can be accessed by following the left link of that node and the right
child of a particular node can be accessed by following the right link of that node.
Inorder Traversal:-
1. Traverse the left subtree of R in inorder.
2. Process the root R.
3. Traverse the right subtree of R in inorder.
Postorde r Traversal:-
1. Traverse the left subtree of R in postorder.
2. Traverse the right subtree of R in postorder.
3. Process the root R.
Observe that each algorithm contains the same three steps, and that the left subtree
of R is always traversed before the right subtree. The difference between the
algorithms is the time at which the root R is processed. The three algorithms are
sometimes called the node-left-right (NLR) traversal, the left-node-right (LNR)
traversal and the left-right-node (LRN) traversal. Traversal algorithms using recursive
approach.
Preorder Traversal:-
In the preorder traversal, the node element is visited first and then the left subtree of
the node and then the right subtree of the node is visited. Consider we have 6 nodes
in the tree A, B, C, D, E, F. The traversal always starts from the root of the tree. The
node A is the root and hence it is visited first. The value at this node is processed.
Now we check if there exists any left child for this node if so apply the preorder
procedure on the left subtree. Now check if there is any right subtree for the node A,
the preorder procedure is applied on the right subtree. Since there exists a left
subtree for node A, B is now considered as the root of the left subtree of A and
preorder procedure is applied. Hence we find that B is processed next and then it is
checked if B has a left subtree. This recursive method is continued until all the nodes
are visited.
Algorithm for Preorder:- PREORDER( ROOT )
Temp = ROOT
If Temp = NULL
return
display temp -> data
If Temp - > left ≠ NULL
PREORDER ( temp - > left )
If Temp -> right ≠ NULL
PREORDER ( temp - > right )
Inorder Traversal:-
In the Inorder traversal method, the left subtree of the node element is visited first
and then the node element is processed and at last the right subtree of the node
element is visited. For example, the traversal starts with the root of the binary tree.
The node A is the root and it is checked if it has the left subtree. Then the inorder
traversal procedure is applied on the left subtree of the node A.
Now we find that node D does not have left subtree. Hence the node D is processed
and then it is checked if here is a right subtree for node D. Since there is no right
subtree, the control returns back to the previous function which was applied on B.
Since left of B is already visited, now B is processed. It is checked if B has the right
subtree. If so apply the inorder traversal method on the right subtree of the node B.
This recursive procedure is followed till all the nodes are visited.
Expression Trees:-
The trees are many times used to represent
an expression and if done so, those types of trees
are called expression trees. The following
expression is represented using the binary tree,
where the leaves represent the operands and the
internal nodes represent the operators. B ^ A + B
*A–C
If the expression tree is traversed using preorder, inorder and postorder traversal
methods, then we get the expressions in prefix, infix and postfix forms as shown.
-+^BA*BA–
CB^A+B*A–
CBA^BA*C–
Heap Sort:-
Heaps are used to implement priority queues. In this type of queues the element to
be deleted is one with highest(lowest) priority. We can insert the element at arbitrary
priority can be inserted into the queue.
A max heap is a complete binary tree that is also a max tree. A
max tree is a tree in which the key value in each node is larger
than the key values of its children if any.
If the element we want to insert is with key value 1, it may be inserted as the left child
of 2. But if the key value we want to insert is 5 then we cannot insert as left child of 2
because heap property fails. So 2 is moved down as left child and the place for 5 is
the old place of 2.
Deletion From A Max Heap:-
When an element is to be deleted from the max heap it is taken
from the root of the heap. For example, a deletion from the heap
results in removal of element 21 then the heap will have only
five elements.
O( log n ).
Consider the following list of numbers. A binary search tree can be constructed using
this list of numbers, as shown.
38, 14, 8, 23, 18, 20, 56, 45, 82, 70
Initially 38 is taken and placed as the root node. The next number 14 is taken and
compared with 38. As 14 is lesser than 38, it is placed as the
left child of 38. Now the third number 8 is taken and
compared starting from the root node 38. Since is 8 is less
than 38 move towards left of 38. Now 8 is compared with
14, and as it is less than 14 and also 14 does not have any
child, 8 is attached as the left child of 14.
Algorithm:-
if(temp - > left != NULL && temp - > right != NULL)
parent = temp
temp_succ = temp - > right
while(temp_succ - > left != NULL)
parent = temp_succ
temp_succ = temp_succ - > left
temp - > data = temp_succ - > data parent - >right = NULL
AVL Trees:-
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is
named AVL in honour of its inventors. AVL Tree can be defined as height balanced
binary search tree in which each node is associated with a balance factor which is
calculated by subtracting the height of its right sub-tree from that of its left sub-tree.
Tree is said to be balanced if balance factor of each node is in between -1 to 1,
otherwise, the tree will be unbalanced and need to be balanced.
Balance Factor (k) = height (left(k)) - height (right(k))
If balance factor of any node is 1, it means that the left
sub-tree is one level higher than the right sub-tree.
If balance factor of any node is 0, it means that the left sub-tree and right sub-tree
contain equal height.
If balance factor of any node is -1, it means that the left sub-tree is one level lower
than the right sub-tree.
We can see that, balance factor associated with each node is in between -1 and +1.
therefore, it is an example of AVL tree.
SN Operatio Description
O n
AVL Rotations:-
We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and
1. There are basically four types of rotations which are as follows:
1. L L rotation: Inserted node is in the left subtree of left subtree of A
2. R R rotation : Inserted node is in the right subtree of right subtree of A
3. L R rotation : Inserted node is in the right subtree of left subtree of A
4. R L rotation : Inserted node is in the left subtree of right subtree of A
Where node A is the node whose balance Factor is other than -1, 0, 1.
The first two rotations LL and RR are single rotations and the next two rotations LR
and RL are double rotations. For a tree to be unbalanced, minimum height must be at
least 2, Let us understand each rotation.
RR Rotation:-
When BST becomes unbalanced, due to a node is inserted into the right subtree of the
right subtree of A, then we perform RR rotation, RR rotation is an anticlockwise
rotation, which is applied on the edge below a node having balance factor -2.
In above example, node A has balance factor -2 because a node C is inserted in the
right subtree of A right subtree. We perform the RR rotation on the edge below A.
LL Rotation:-
When BST becomes unbalanced, due to a node is inserted into the left subtree of the
left subtree of C, then we perform LL rotation, LL rotation is clockwise rotation, which
is applied on the edge below a node having balance factor 2.
In above example, node C has balance factor 2 because a node A is inserted in the left
subtree of C left subtree. We perform the LL rotation on the edge below A.
LR Rotation:-
Double rotations are bit tougher than single rotation which has already explained
above. LR rotation = RR rotation + LL rotation, i.e., first RR rotation is performed on
subtree and then LL rotation is performed on full tree, by full tree we mean the first
node from the path of inserted node whose balance factor is other than -1, 0, or 1.
A node B has been inserted into the right subtree of A the left
subtree of C, because of which C has become an unbalanced node
having balance factor 2. This case is L R rotation where: Inserted
node is in the right subtree of left subtree of C
As LR rotation = RR + LL rotation, hence RR (anticlockwise) on
subtree rooted at A is performed first. By doing RR rotation,
node A, has become the left subtree of B.
RL Rotation:-
As already discussed, that double rotations are bit tougher than single rotation which
has already explained above. R L rotation = LL rotation + RR rotation, i.e., first LL
rotation is performed on subtree and then RR rotation is performed on full tree, by full
tree we mean the first node from the path of inserted node whose balance factor is
other than -1, 0, or 1.
State Action
A node B has been inserted into the left subtree of C the right
subtree of A, because of which A has become an unbalanced node
having balance factor - 2. This case is RL rotation where: Inserted
node is in the left subtree of right subtree of A
As RL rotation = LL rotation + RR rotation, hence, LL (clockwise)
on subtree rooted at C is performed first. By doing RR rotation,
node C has become the right subtree of B.