Dsc++ Unit III
Dsc++ Unit III
Tree - Terminology
Terminology
Root:-In a tree data structure, the first node is called as Root Node. Every
tree must have root node. We can say that root node is the origin of tree
data structure. In any tree, there must be only one root node. We never
have multiple root nodes in a tree.
Leaf :- Node which does not have a child is called as LEAF Node. In simple
words, a leaf is a node with no child. In the figure E,F,C,G,C,H are the leaf
nodes
Level :- 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 the above figure, the node A is at level 0, the node
B, C, Dare at level 1, the nodes E, F, G, H are at level 2.
Tree Height :- Number of edges in the longest path from the root to the leaf
node.
the height of the tree is 3
Internal Nodes:-The nodes in the tree which are other than leaf nodes and
the root node are called as internal nodes. These nodes are present in
between root node and leaf nodes in the tree that’s why these nodes are
called as internal node. Here, B and D are internal nodes.
Degree of tree :-The maximum degree of the node in the tree is called
the degree of the tree. Here, the degree of tree is 3.
binary tree
A binary tree is a finite set of nodes that is either empty or consist a root
node and two disjoint binary trees called the left subtree and the right
subtree.
In other words, a binary tree is a non-linear data structure in which each
node has maximum of two child nodes. The tree connections can be called as
branches
A binary tree is a special case of an ordered tree, where k is 2.
1) Complete Binary Tree
A binary tree T is said to be complete binary tree if -
1. All its levels, except possibly except possibly the last, have the maximum
number of nodes and
2. All the nodes at the last level appears as far left as possible.
left and right child nodes denote operands and parent node indicates
operator.
Log2(N+1) ⌉
In this representation of binary tree root will contain the location of the
root R of T. If any one of the subtree is empty, then the corresponding
pointer will contain the null value if the
tree T itself is empty, the ROOT will contain the null value.
Example
Consider the binary tree T in the figure. A schematic diagram of the linked list
representation
of T appears in the following figure. Observe that each node is pictured with
its three fields, and that the empty subtree is pictured by using x for null
entries.
Binary Tree
Linked Representation of the Binary Tree
For Example:
Preorder traversal
Inorder traversal
Postorder traversal
1) Preorder traversal
To traverse a binary tree in preorder, following operations are carried out:
Algorithm:
Algorithm preorder(t)
/*t is a binary tree. Each node of t has
three fields: lchild, data, and rchild.*/
{
If t! =0 then
{
Visit(t);
Preorder(t-
>lchild);
Preorder(t-
>rchild);
}
}
Example: Let us consider the given binary tree,
2) Inorder traversal
To traverse a binary tree in inorder traversal, following operations are carried
out:
Algorithm:
Algorithm inorder(t)
3) Postorder traversal
To traverse a binary tree in postorder traversal, following operations are carried
out:
Algorithm:
Algorithm postorder(t)
1. In the in order traversal When The right NULL pointer of each leaf node can
be replaced by a thread to the successor of that node then it is called a
right thread, and the resultant tree called a right threaded tree or right
threaded binary tree.
2. When The left NULL pointer of each node can be replaced by a thread to the
predecessor of that node under in order traversal it is called left thread and
the resultant tree will call a left threaded tree.
3. In the in order traversal, the left and the right NULL pointer can be used to
point to predecessor and successor of that node respectively. then the
resultant tree is called a fully threaded tree.
In the threaded binary tree when there is only one thread is used then it is
called as one way threaded tree and when both the threads are used then it
is called the two way threaded tree. The pointer point to the root node when
If there is no in-order predecessor or in-order successor.
Consider the following binary tree:
PRIORITY QUEUE
There are two types of heap data structures and they are as follows...
1. Max Heap
2. Min Heap
Every heap data structure
h has t e following properties...
Property #1 (Ordering): Nodes must be arranged in an order according
to their values based on Max heap or Min heap.
Property #2 (Structural): All levels in a heap must be full except the last
level and all nodes must be filled from left to right strictly.
Max Heap
Max heap data structure is a specialized full binary tree data structure. In a max
heap nodes are arranged based on node value.
Example
Above tree is satisfying Ordering property and Structural property according to
both Heap data the Max
structure.
Operations on Max
Heap
The following operations are performed on a Max heap data structure...
1. Finding Maximum
2. Insertion
3. Deletion
Finding Maximum Value
e Op ration in Max Heap
Finding the node which has maximum value in a max heap ismax heap, the root root node value
y
very simple. In node has the maximum value than all other
nodes. So, directly we can displa as maximum value in max
heap.
Insertion Operation in Max Heap
Insertion Operation in max heap is performed as follows...
Step 1 - Insert the newNode as last leaf from left to right.
Step 2 - Compare newNode value with its Parent node.
Step 3 - If newNode value is greater than its parent, then swap both of
them.
Step 4 - Repeat step 2 and step 3 until newNode value is less than its
parent node (or) newNode reaches to root.
Example
Consider the above max heap. Insert a new node with value 85.
Step 1 - Insert the newNode with value 85 as last leaf from left to right.
That means newNode is added as a right child of node with value 75. After
adding max heap is as follows...
Step 2 - Compare newNode value (85) with its Parent node value (75).
That means 85 > 75
Step 3 - Here newNode value (85) is greater than its parent value (75),
then swap both of
Here, newNode value smaller than its parent node value we stop insertion
(85) is (89). So,
process. Finally, max heap after insertion of a new node with value 85 is as
follows...
Example
Consider the above max heap. Delete root node (90) from the max heap.
Step 1 - Swap the root node (90) with last node 75 in max heap. After
swapping max heap is as follows...
Step 2 - Delete last node. Here the last node is 90. After deleting node
with value 90 from heap, max heap is as follows...
Step 3 - Compare root node (75) with its left child (89).
Here, root value (75)er than its left child value (89). So, compare left child
is small (89) with
Here, node with value 75 is larger than its left child. So, we compare node
75 with its right child 85.
ap both of them.
Step 6 - Here, node with value 75 is smaller than its right child
w (85). So, we s