Professional Documents
Culture Documents
Unit 5 Tree Lect Notes
Unit 5 Tree Lect Notes
Unit 5 Tree Lect Notes
2. Edge
In a tree data structure, the connecting link between any two nodes is
called as EDGE. In a tree with 'N' number of nodes there will be a
maximum of 'N-1' number of edges.
Tree
Siblings Terminology
In a tree data structure, nodes which belong to same Parent are called
as SIBLINGS. In simple words, the nodes with the same parent are
called Sibling nodes.
Leaf
In a tree data structure, the node which does not have a child is called
as LEAF Node. In simple words, a leaf is a node with no child. In a tree data
structure, the leaf nodes are also called as External Nodes. External node is
also a node with no child. In a tree, leaf node is also called as 'Terminal'
node.
Tree
Internal Nodes Terminology
In a tree data structure, the node which has at least one child is called
as INTERNAL Node. In simple words, an internal node is a node w ith at
least one child.
Degree
In a tree data structure, the total number of children of a node is
called as DEGREE of that Node. In simple words, the Degree of a
node is total
number of children it has. The highest degree of a node among all the
nodes in a tree is called as 'Degree of Tree'
Tree
Level Terminology
In a tree data structure, the root node is said to be at Level 0 and the children
of root node are at Level 1 and the children of the nodes which are at Level 1
will be at Level 2 and so on... In simple words, in a tree each step from top to
bo om is called as a Level and the Level count sta s with '0' and incremented
by one at each level (Step).
Height
In a tree data structure, the total number of edges from leaf node to a pa
icular node in the longest path is called as HEIGHT of that Node. In a tree,
height of the root node is said to be height of the tree. In a tree, height of all
leaf nodes is '0'.
Tree
Terminology
Depth
In a tree data structure, the total number of egdes from root node to a
pa icular node is called as DEPTH of that Node. depth of the root node is
'0'.
Tree
Path
Terminology
In a tree data structure, the sequence of Nodes and Edges from one node
to another node is called as PATH between that two Nodes. Length of a
Path is total number of nodes in that path. In below example the path A - B
- E - J has length 4.
Tree
Sub Tree Terminology
In a tree data structure, each child from a node forms a subtree
recursively. Every child node w ill form a subtree on its pare
Binary
Tree
A tree in which every node can have a maximum of two children is called
Binary Tree. In a binary tree, every node can have either 0 children or 1 child
or 2 children but not more than 2 children. There are different types of
binary trees and they are...
Extended Binary Tree :The full binary tree obtained by adding dummy
nodes to a binary tree is called as Extended Binary Tree.
Binary Tree
Representat ions
A binary tree data structure is represented using two methods. Those methods are
as follows...
Array Representation
Linked List
Representation
LR Rotation
LR rotation = RR rotation + LL rotation, i.e., first RR rotation is pe ormed
on subtree and then LL rotation is pe ormed on full tree, by full tree we
mean the first node from the path of inse ed node whose balance
factor is other than -1, 0, or 1.
Construct an AVL tree having the following
elements H, I, J , B , A, E, C, F, D, G, K, L
Inse H , I,
J
Inse K
The resultant balanced tree a er RR rotation is Inse
L
Heap
Tree
A heap is a complete binary tree , and the binary tree is a tree in which
the node can have utmost two children.
How can w e arrange the nodes in the
Tree? There are two types of the heap:
Min Heap
Max
heap
Min Heap: The value of the parent node should be less than or equal to
either of its children.In other words, the min-heap can be defined as, for
every node i, the value of node i is greater than or equal to its parent value
except the root node. Mathematically, it can be defined as: A[Parent(i)] < =
A [ i]
Heap
Tree
Max Heap: The value of the parent node is greater than or
equal to its children. Or In other words, the max heap can be
defined as for every node i; the value of node i is less than or
equal to its parent value except the root node.
Mathematically, it can be defined as:
A[Parent(i)] > = A[i]
Inse ion in the Heap
To create thetree
max heap tree using:
44 , 33, 77, 11, 55 , 88 ,
66
Inse ion in t he Heap
t ree is pe ormed to inse an element in the heap
Inse ion Operation
tree.
Step-01:
Inse the new element as a next leaf node from le to
right. Step-02:
Ensure that the tree remains a max heap.
• Check that every non-leaf node contains a greater or
equal value element than its child nodes.
• If there exists any node that does not satisfies the
ordering prope y of max heap, swap the elements.
• Sta checking from a non-leaf node with the highest
index ( bo om to top and right to le ).
Deletion in Heap
Tree
When it comes to deleting a node from the heap tree, following two
cases are possible-
Case-01: Deletion Of Last Node-
Just remove / disconnect the last leaf node from the heap
tree. Case-02: Deletion Of Some Other Node-
Deleting a node other than the last node disturbs the heap prope
ies. The steps involved in deleting such a node are-
Step-01:
Delete the desired element from the heap tree.
Pluck the last node and put in place of the deleted
node. Step-02:
Ensure that the tree remains a max heap.
• Check that every non-leaf node contains a greater or equal value
element than its child nodes.
• If there exists any node that does not satisfies the ordering prope y
of max heap, swap the elements.
• Sta checking from a non-leaf node with the highest index (bo om
Delet ion in Heap
Tree
Consider the follow ing max
heap- 50 , 30, 20, 15, 10, 8, 16
Delete a node w ith value
50 . Step-02:
This is the required max heap a er deleting the node with value
50 .
THANK
YOU