0% found this document useful (0 votes)
3 views

DS_UNIT_IV[1]

Unit IV covers the fundamentals of trees, including binary trees and their properties, representations using arrays and linked lists, and operations such as insertion and deletion. It also discusses binary search trees, their advantages for search efficiency, and traversal methods like preorder, inorder, and postorder. Additionally, the unit explores expression trees, heap sort, and balanced binary trees like AVL trees.

Uploaded by

radha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DS_UNIT_IV[1]

Unit IV covers the fundamentals of trees, including binary trees and their properties, representations using arrays and linked lists, and operations such as insertion and deletion. It also discusses binary search trees, their advantages for search efficiency, and traversal methods like preorder, inorder, and postorder. Additionally, the unit explores expression trees, heap sort, and balanced binary trees like AVL trees.

Uploaded by

radha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Unit-IV TREESR16

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.

Binary Tree Traversals:-


A tree traversal is a method of visiting every node in the tree. By visit, we mean that
some type of operation is performed. For example, we may want to print the contents
of the nodes. There are three standard ways of traversing a binary tree T with root R.
They are:
1. Preorder Traversal
2. Inorder Traversal
3. Postorder Traversal
Preorder Traversal:-
1. Process the root R.
2. Traverse the left subtree of R in preorder.
3. Traverse the right subtree of R in preorder.

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.

Algorithm for Inorder:-


INORDER( ROOT )
Temp = ROOT
If temp = NULL
return
If temp - > left ≠ NULL
INORDER ( temp - > left )
display temp -> data
If temp -> right ≠ NULL
INORDER ( temp - > right )
Postorder Traversal:-
In the postorder traversal method the left subtree is visited first, then the right
subtree and at last the node element is processed. For example, A is the root node.
Since A has the left subtree the postorder traversal method is applied recursively on
the left subtree of A. Then when left subtree of A is completely is processed, the
postorder traversal method is recursively applied on the right subtree of the node A.
If right subtree is completely processed, then the node element A is processed.

Algorithm for Postorder:-


POSTORDER( ROOT )
Temp = ROOT
If temp = NULL
return
If temp - > left ≠ NULL
POSTORDER ( temp - > left )
If temp -> right ≠ NULL
POSTORDER ( temp - > right )
display temp ->data

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.

A min heap is a complete binary tree that is also a min tree. A


min tree is a tree in which the key value in each node is smaller
than the key values of its children if any.
Insertion Into A Max Heap:-
Let us consider a max heap of five elements. When an element
is added to this heap, the resulting is six element heap and it is
a complete binary tree. To determine the correct place for the
element to be inserted we use bubbling up process that begin
at new node and move to the root. The node we want to insert
bubbles up to ensure a max heap.

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.

To do this we remove the element in position 6. Now we have


right structure. But the root is vacant and the element 2 is not in
the heap. If 2 is inserted into the root then the result binary tree is
not max heap.
I-SEM
The element at the root should be largest in the tree apart from
left and right child. This element is 20. It is moves to the root and
create vacancy at position 3. Since it has no children we insert 2 at
this place.

Binary Search Trees:-


A Binary Search Tree (BST) is a binary tree. It may be empty or it may if not
empty than it satisfies the following properties.
 Each node has exactly one key and the keys in the tree are distinct
 The keys if any in the left sub tree are smaller than the key in the root
 The keys if any in the right sub tree are larger than the key in the root
 The left and right sub trees are also binary search trees.
The reason why we go for a Binary Search tree is to improve the searching efficiency.
The average case time complexity of the search operation in a binary search tree is

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.

This process is repeated until all the numbers are inserted


into the tree. Remember that if a number to be inserted is
greater than a particular node element, then we move towards the right of the node
and start comparing again.

Search Operation In A Binary Search Tree:-


The search operation on a BST returns the address of the node where the element is
found. The pointer LOC is used to store the address of the node where the element is
found. The pointer PAR is used to point to the parent of LOC. Initially the pointer TEMP
is made to point to the root node.
Let us search for a value 70 in the following BST. Let k = 70. The k value is compared
with 38. As k is greater that 38, move to the right child of 38, i.e., 56. k is greater
than 56 and hence we move to the right child of 56, which is 82. Now since k is lesser
than 82, temp is moved to the left child of 82. The k value matches here and hence
the address of this node is stored in the pointer LOC.
Every time the temp pointer is moved to the next node, the current node is made
pointed by PAR. Hence we get the address of that node where the k value is found,
and also the address of its parent node though PAR.
Algorithm SEARCH( ROOT, k ):-
temp = ROOT, par = NULL, loc = NULL
while temp ≠ NULL
If k = temp - > data
loc = temp
break
If k < temp - > data
par = temp
temp = temp - > left
else
par=temp
temp=temp->right
Insert Into In A Binary Search Tree:-
The BST itself is constructed using the insert operation described below. Consider the
following list of numbers. A binary tree can be constructed using this list of numbers.

38, 14, 8, 23, 18, 20, 56, 45, 82.


For example we want to insert the element is 70. While
inserting a node into the binary search tree first we have
find the appropriate position in the binary search tree. We
start comparing the node value 70 with the root if it is
greater than the root then it is inserted on the right branch
of the root else on the left branch of the root.

Now compare the node 70 with root node 38. As node


70 is greater than the root 38 we will move to the right
subtree. Now compare node 70 with the node 56 as it
greater then move to right and compare node 70 with
node 82 as it less than the node 82 we attach 70 as left
child of node 82. The diagram is shown below.

Algorithm INSERT( ROOT, k ):-


1. Read the value for the node which is to be created and store
it in a node called new.
2. Initially if(root!=NULL) then root = new
3. Again read the next value of node created in new
4. If(new - > data < root - > data) then attach the new node as a left child of root otherwise
attach the new node as a right child of root
5. Repeat step 3 and 4 for constructing required binary search tree completely.
Unit-IV TREES
Deletion From A Binary Search Tree:-
The deletion of a node from a binary search tree occurs with three possibilities
1. Deletion of a leaf node.
2. Deletion of a node having one child.
3. Deletion of a node having two children.
Deletion of a leaf node:-
This is the simplest deletion in which we can simple
remove it from the tree. For example consider the
binary search tree as follows.
From the above tree diagram the node we want to
delete is the node 8, then we will set the left pointer of
its parent(node 14) to NULL. Then after deletion the
binary search tree is as follows.
Algorithm:-
if(temp - > left == NULL && temp - > right ==
NULL) if(parent - > left == temp) parent - >
left = NULL
else parent - > right = NULL
Deletion of a node having one child:-
The node if we want to delete is having only one child( i.e.
either left or right child), delete it and replace it with its child.
From the diagram the node we want to delete is having the
value 23 then we simple copy node 18 at the place of 23 and
set the node free.
Algorithm:-
if(temp - > left !=NULL && temp - > right == NULL)
if(parent - > left ==temp) parent - > left = temp - > left
else parent - > right = temp - > left
temp == NULL
delete temp
Deletion of a node having two children:-
Suppose the node to be deleted is called N. We replace the value of N with either its
in-order successor (the left-most child of the right subtree) or the in-order
precedessor(the right-most child of the the left subtree).
The node if we want to delete is having two children. From the
diagram the node we want to delete is having the value 23 then
we find the inorder successor of the node 23 and it is copied at
the place of 23 and set the node 25 left pointer to NULL.

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

Height Of A Binary Search Tree:-


The height of a binary search tree with “n” elements can become as large as “n”. For
instance, when the values like 1, 2, ------n are inserted into the empty binary search
tree. If insertions nd deletions are made at random then the height of the binary
search tree is O(log n) on average. Search trees with worst case height of O(log n) are
called balanced search trees. These trees permit insertions, deletions and searches to
be performed at time O(h). for example, AVL trees, Red / Black Trees, B-Trees, 2 – 3
Trees etc.

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.

Operations on AVL tree:-


Due to the fact that, AVL tree is also a binary search tree therefore, all the operations
are performed in the same way as they are performed in a binary search tree.
Searching and traversing do not lead to the violation in property of AVL tree. However,
insertion and deletion are the operations which can violate this property and
therefore, they need to be revisited.

SN Operatio Description
O n

1 Insertion Insertion in AVL tree is performed in the same way as it is


performed in a binary search tree. However, it may lead to
violation in the AVL tree property and therefore the tree may
need balancing. The tree can be balanced by applying rotations.

2 Deletion Deletion can also be performed in the same way as it is


performed in a binary search tree. Deletion may also disturb the
balance of the tree therefore, various types of rotations are used
to rebalance the tree.

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.

Let us understand each and every step very clearly:


State Action

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.

After performing RR rotation, node C is still unbalanced, i.e.,


having balance factor 2, as inserted node A is in the left of left
of C

Now we perform LL clockwise rotation on full tree, i.e. on node C.


node C has now become the right subtree of node B, A is left
subtree of B

Balance factor of each node is now either -1, 0, or 1, i.e. BST is


balanced now.

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.

After performing LL rotation, node A is still unbalanced, i.e. having


balance factor -2, which is because of the right-subtree of the
right-subtree node A.

Now we perform RR rotation (anticlockwise rotation) on full tree,


i.e. on node A. node C has now become the right subtree of node
B, and node A has become the left subtree of B.

Balance factor of each node is now either -1, 0, or 1, i.e., BST is


balanced now.

You might also like