2310DataStructures Unit III Trees
2310DataStructures Unit III Trees
2310DataStructures Unit III Trees
SEMESTER-IV
BSc Second YEAR, 2021
UNIT III
Syllabus :
Trees: Binary Tree, Definition, Properties, ADT, Array and Linked
representations, Implementations and Applications. Binary Search
Trees (BST) – Definition, ADT, Operations and Implementations, BST
Applications. Introduction to Threaded Binary Trees, Heap trees.
-----------------------------------------------------------------------------------------
Tree: A Tree is a Non-linear data structure. It stores the data in
hierarchical manner. Suppose we want to show the employees of an
organization in the hierarchical form as shown below.
Characteristics of a Tree:
• A tree data structure is defined as a collection of objects or entities
known as nodes that are linked together to represent or simulate
hierarchy.
• A tree data structure is a non-linear data structure because it does
not store in a sequential manner. It is a hierarchical structure as
elements in a Tree are arranged in multiple levels.
• In the Tree data structure, the topmost node is known as a root node.
Each node contains some data, and data can be of any type. In the
above tree structure, the node contains the name of the employee,
so the type of data would be a string.
• Each node contains some data and the link or reference of other
nodes that can be called children.
In the above tree, root node P has two successors: Q and R. Node Q has
two successors: A and B. Node A has one successor: D. Node B has two
successors: E and F. Node D has two successors: I and J. Node R has one
successor: C. Node C has two successors: G and H.
Even the leaf nodes may contain the empty left sub trees and empty
right sub trees. The nodes I,J,E,F,G and H has no successors then these
are empty sub trees.
In the above tree, we can observe that each node is either containing
zero or two children; therefore, it is a Full Binary tree.
2. Complete Binary Tree: The complete binary tree is a tree in which all
the nodes are completely filled except the last level. In the last level,
all the nodes must be as left as possible. In a complete binary tree,
the nodes should be added from the left.
The above tree is a complete binary tree because all the nodes are
completely filled, and all the nodes in the last level are added at the
left first.
The above tree is a perfect binary tree because all nodes having 2
children except the leaf nodes.
4. Degenerate Binary Tree: The degenerate binary tree is a tree in which
all the internal nodes have only one children. There are two types of
degenerate binary trees. They are Left-degenerate binary tree and
Right-degenerate binary tree.
The above tree is a degenerate binary tree because all the nodes
have only one child. It is also known as a right-degenerate tree as all
the nodes have a right child only.
The above tree is also a degenerate binary tree because all the nodes
have only one child. It is also known as a left-skewed tree as all the
nodes have a left child only.
The index 1 is holding the root, it has two children 5 and 16, they are
placed at location 2 and 3. Some children are missing, so their place is
left as blank.
In Array representation we can easily get the position of two children of
one node by using this formula −
child1=2∗parent
child2=⟮2∗parent⟯+1
To get parent index from child we can use below formula
parent=child/2
In this representation, if one node knows the address of the any other
node then from there, we can access any node.
Pre-Order Traversal for the example of the above given binary tree is
A-B-D-I-J-F-C-G-K–H
In-Order Traversal: In In-Order traversal, the root node is visited
between the left child and right child. In this traversal, the left child node
is visited first, then the root node is visited and later we go for visiting
the right child node. This in-order traversal is applicable for every root
node of all subtrees in the tree. This is performed recursively for all
nodes in the tree. Consider the below binary tree
The node to be deleted has only one child: In this , replace the node
with its child and delete the child node, which now contains the value
which is to be deleted. Simply replace it with the NULL and free the
allocated space.
In the following image, the node 12 is to be deleted. It has only one
child. The node will be replaced with its child node and the replaced
node 12 (which is now leaf node) will simply be deleted.
For fully threaded binary tree, each node has five fields. Three fields like
normal binary tree node, another two fields to store Boolean value to
denote whether link of that side is actual link or thread.
Q) Heap Trees:
A heap is a specialized binary tree, and the binary tree is a tree in which
the node can have utmost two children. The heap tree is a special
balanced binary tree data structure where the root node is compared
with its children and arrange accordingly.
We can represent the Heap trees in two ways
1. Max-Heap tree
2. Min-Heap Tree
1. Min-Heap Trees:
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]
In the above figure, 11 is the root node, and the value of the root node
is less than the value of all the other nodes (left child or a right child).
2. Max-Heap Trees:
The value of the parent node is greater than or equal to its children. 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]
In the above figure, 44 is the root node, and the value of the root node
is greater than the value of all the other nodes (left child or a right child).
***********END******************