Tree
Tree
Trees
Tree Data Structure-
Tree is a non-linear data structure which organizes data in a hierarchical
structure and this is a recursive definition.
OR
A tree is a connected graph without any circuits.
OR
If in a graph, there is one and only one path between every pair of vertices,
then graph is called as a tree.
Introduction to trees
4
5
Tree
• A tree is an abstract model of a
hierarchical structure that consists of
nodes with a parent-child relationship.
9
Main applications of trees include:
1. Manipulate hierarchical data.
2. Make information easy to search (see tree traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for visual
effects.
5. Form of a multi-stage decision-making
Tree Terminologies
Tree Terminologies
Node:
In tree data structure, every individual element is called as Node.
Node in a tree data structure stores the actual data of that particular
element and link to next element in hierarchical structure.
1. Root
• In a tree data structure, the first node is called as Root Node.
• Every tree must have a root node.
• We can say that the root node is the origin of the tree data
structure. In any tree, there must be only one root node. We
never have multiple root nodes in a tree.
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.
3. Parent
• In a tree data structure, the node which is a predecessor of any node is
called as PARENT NODE.
• In simple words, the node which has a branch from it to any other
node is called a parent node. Parent node can also be defined as "The
node which has child / children".
4. Child
• In a tree data structure, the node which is descendant of any node is
called as CHILD Node.
• In simple words, the node which has a link from its parent node is
called as child node.
• In a tree, any parent node can have any number of child nodes. In a tree,
all the nodes except root are child nodes.
5. Siblings
• 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.
6. 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.
7. Internal Nodes
• In a tree data structure, the node which has atleast one child is called
as INTERNAL Node. In simple words, an internal node is a node with atleast
one child.
• In a tree data structure, nodes other than leaf nodes are called as Internal
Nodes. The root node is also said to be Internal Node if the tree has more
than one node. Internal nodes are also called as 'Non-Terminal' nodes.
8. 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'
9. Level
• 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 bottom is called as
a Level and the Level count starts with '0' and incremented by one
at each level (Step).
10. Height
• In a tree data structure, the total number of edges from leaf node to
a particular 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'.
11. Depth
• In a tree data structure, the total number of egdes from root node to
a particular node is called as DEPTH of that Node.
• In a tree, the total number of edges from root node to a leaf node in
the longest path is said to be Depth of the tree. In simple words, the
highest depth of any leaf node in a tree is said to be depth of that
tree. In a tree, depth of the root node is '0'.
12. Path
• 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.
The kids are known as the left kid and right kid.
This is more popular than most other trees. When certain constraints
and characteristics are applied in a Binary tree, a number of others such
as AVL tree, BST (Binary Search Tree), RBT tree, etc. are also used.
When we move forward, we will explain all these styles in detail.
Binary Tree
A binary tree is a special case of the more general tree data structure,
but it is the most useful and most widely used kind of tree.
32
Types of BinaryTree
• Complete binary tree
• Strictly binary tree
• Almost complete binary tree
33
Strictly binary tree
• If every non-leaf node in a binary tree has nonempty leftand right sub-trees, then
such a tree is called a strictly binary tree.
• Or, to put it another way, all of the nodes in a strictly binary tree are of degree zero
or two, never degreeone.
34
Complete binary tree
• Acomplete binary tree is a binary tree in which every level, except possibly the last, is completely
filled, and all nodes are as far left as possible.
• Acomplete binary tree of depth d is called strictly binary tree if all of whose leaves are at level d.
• Acomplete binary tree has 2d nodes at every depthd and 2d -1 non leaf nodes
35
Almost complete binary tree
• An almost complete binary tree is a tree where for a right child, thereis always a
left child, but for a left child there may not be a right child.
36
Here’s an example:
The first has an empty right subtree, while the second has an empty
left subtree. However, as trees, they are the same.
The following are examples of binary trees.
Here are binary trees with all left subtrees empty and all right subtrees empty:
Here is a binary tree where each node, except the leaves, has
exactly two subtrees; this is called a complete binary tree:
To represent a binary tree of depth 'n' using array representation, we need one
dimensional array with a maximum size of 2n + 1.
2. Linked List Representation of Binary Tree
Lchild
data
rchild
Class node
{
Char data;
Node *lchild;
Node * rchild;
};
Consider a binary tree and its linked list representation is shown in
the figure.
A
B C
D E G
F
B C
D E F G
Operations on Binary trees
The basic operations to be performed on a binary tree are listed below.
Lchild: it returns a pointer to the left child of the node. If the node has no left child,
it returns a NULL pointer.
Rchild: it returns a pointer to the right child of the node. If the node has no right
child, it returns a NULL pointer.
Let us consider that we have a tree T. let our tree T is a binary tree that us
complete binary tree.
A
A
B C
B C
D E G D E F G
F
struct node
{
char data;
char father;
struct node *lchild;
struct node *rchild;
};
typedef struct node BTNODE;
Array Representation
• An array can be used to store the nodes of a binary tree.
• The nodes stored in an array are accessible sequentially.
• In c, arrays start with index 0 to (MAXSIZE-1).
BT [O] A
BT [1] B
BT [2] C
A
0
1 B
2 C
3 D
4 E
5 F
6 G
Array Representation
Number the nodes using the numbering scheme for a full binary tree. The
node that is numbered i is stored in tree[i].
a1
2 3
b c
4 5 6 7
d e f g
8 9 10
h i j
tree[] a b c d e f g h i j
0 5 10
left-Skewed Binary Tree
a1
3
b 7
C
0 5 10 15
tree[] a b - c - - - - - - - - - -
tree[] a - b - - - c - - - - - - - d
0 5 10 15
Root Level 0
Level 1
Leaf
Leaf Level 2
Leaf
Binary Tree
Binary Tree
Binary Tree is a rooted tree in which root can have
maximum two children such that each of them is
again a binary tree. That means, there can be 0,1, or 2
children of any node.
Strict Binary Tree
Strict Binary Tree
Strict Binary Tree is a Binary tree in
which root can have exactly two or no
children children at all. That
means, there can be 0 or 2 children of any node.
Complete Binary Tree
Complete Binary Tree
Complete Binary Tree is a Strict Binary tree in which
every leaf node is at same level. That means, there
are equal number of children in right and left subtree
for every node.
Extended Binary Tree
A binary tree with special nodes replacing everynull subtree. Every
regular node has two children, and every special node has nochildren.
Extended Binary Tree
An extended binary tree is a transformation of any binary tree into a
complete binary tree.
The nodes from the original tree are then called as internal nodes, while
the “special nodes” are called as external nodes.
Apart from these primitive operations, other operations that can be
applied to the binary tree are
1. Tree traversal
2. Insertion of a node
3. Deletion of a node
4. Searching for a node
5. Copying the binary tree.
Traversal of a binary tree
• The traversal is one of the most common operations performed on tree data structure.
• It is a way in which each node in the tree is visited exactly once in a systematic manner.
• There are 3 popular ways of binary tree traversal. They are
Preorder traversal
Inorder traversal
Postorder traversal
Preorder traversal
• The preorder traversal of a non-empty binary tree is defined as follows.
In a preorder traversal the root node is visited before traversing its left and right
subtrees. After visiting the root node, the left subtree is taken up and it is traversed
recursively, then the right subtree is traversed recursively.
Tree Traversal : Pre order
Pre order (N L R)
Tree Traversal : Pre order
Pre order (N L R)
A
Tree Traversal : Pre order
Pre order (N L R)
A, B
Tree Traversal : Pre order
Pre order (N L R)
A, B, D
Tree Traversal : Pre order
Pre order (N L R)
A, B, D, E
Tree Traversal : Pre order
Pre order (N L R)
A, B, D, E, C
Tree Traversal : Pre order
Pre order (N L R)
A, B, D, E, C, F
Tree Traversal : Pre order
Pre order (N L R)
A, B, D, E, C, F, G
Algorithm for preorder order traversal
Procedure Preorder(root)
1.If (root !=NULL)
Begin A
1.Print root→info B C
2.Preorder(root→left) D
E F G
3.Preorder(root→right)
E E
4.End
5.finished
The preorder traversal of the above binary tree is
ABDEICFGJ
Tree Traversal : In order
In order (L N R)
Tree Traversal : In order
In order (L N R)
D
Tree Traversal : In order
In order (L N R)
D, B
Tree Traversal : In order
In order (L N R)
D, B, E
Tree Traversal : In order
In order (L N R)
D, B, E, A
Tree Traversal : In order
In order (L N R)
D, B, E, A, F
Tree Traversal : In order
In order (L N R)
D, B, E, A, F, C
Tree Traversal : In order
In order (L N R)
D, B, E, A, F, C, G
Tree Traversal : Post order
Post order (L R N)
Tree Traversal : Post order
Post order (L R N)
D
Tree Traversal : Post order
Post order (L R N)
D, E
Tree Traversal : Post order
Post order (L R N)
D, E, B
Tree Traversal : Post order
Post order (L R N)
D, E, B, F
Tree Traversal : Post order
Post order (L R N)
D, E, B, F, G
Tree Traversal : Post order
Post order (L R N)
D, E, B, F, G, C
Tree Traversal : Post order
Post order (L R N)
D, E, B, F, G, C, A