0% found this document useful (0 votes)
53 views22 pages

Understanding Tree Data Structures

A tree is a non-linear data structure that organizes data hierarchically, consisting of nodes where each node can have multiple children but only one parent. Key concepts include root, edges, leaves, internal nodes, and various types of binary trees such as full, complete, and perfect binary trees, each with specific properties and applications. Tree traversal methods include depth-first search (inorder, preorder, postorder) and breadth-first search (level order), each with distinct algorithms and complexities.

Uploaded by

copaf78219
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views22 pages

Understanding Tree Data Structures

A tree is a non-linear data structure that organizes data hierarchically, consisting of nodes where each node can have multiple children but only one parent. Key concepts include root, edges, leaves, internal nodes, and various types of binary trees such as full, complete, and perfect binary trees, each with specific properties and applications. Tree traversal methods include depth-first search (inorder, preorder, postorder) and breadth-first search (level order), each with distinct algorithms and complexities.

Uploaded by

copaf78219
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tree

• In linear data structure data is organized in sequential order and in non-linear data structure
data is organized in random order. A tree is a very popular non-linear data structure used
in a wide range of applications.
• Tree is a non-linear data structure which organizes data in hierarchical structure. Tree data
structure is a collection of data (Node) which is organized in hierarchical structure
recursively.
• 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.
• In a tree data structure, if we have N number of nodes then we can have a maximum of (N-
1) number of links or edges.
• A Tree is a recursive data structure containing the set of one or more data nodes where one
node is designated as the root of the tree while the remaining nodes are called as the children
of the root.
• The nodes other than the root node are partitioned into the non-empty sets where each one
of them is to be called sub-tree.
• In a general tree, a node can have any number of children nodes but it can have only a
single parent.

Example

Root - The root node is the topmost node in the tree hierarchy. In other words, the root node is
the one which doesn't have any parent. Every tree must have a root node.
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.

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".

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.
Any parent node can have any number of child nodes. All the nodes except root are child nodes.
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.

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. Leaf nodes are also called as external nodes or
terminal nodes. Leaf node is the bottom most node of the tree.

Internal Nodes - In a tree data structure, the node which has at least one child is called
as internal node. 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.
Degree - In a tree data structure, the total number of children of a node is called as degree of
that node. Degree of a leaf node is always 0.

Level - Each node of the tree is assigned a level number in such a way that each node is present
at one level higher than its parent. Root node of the tree is always present at level 0.

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. Height of all leaf nodes is '0'.
Depth - In a tree data structure, the total number of edges 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. Depth of the root node is '0'.

Path - The sequence of consecutive edges is called path.

Sub Tree - In a tree data structure, each child from a node forms a subtree recursively. Every
child node will form a subtree on its parent node.
Binary Tree

In a normal tree, every node can have any number of children. A binary tree is a special type
of tree data structure in which each node can have at most 2 children. One is known as left
child and the other is known as right child.

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.

Example

Properties of Binary Tree

• At each level of i, the maximum number of nodes is 2i.


• The height of the tree is defined as the longest path from the root node to the leaf node.
The tree which is shown above has a height equal to 2. Therefore, the maximum number
of nodes at height 2 is equal to (1+2+4) = 7. In general, the maximum number of nodes
possible at height h is (20 + 21 + 22+….2h) = 2h+1 -1.

• The minimum number of nodes possible at height h is equal to h+1.


• If the number of nodes is minimum, then the height of the tree would be maximum.
Conversely, if the number of nodes is maximum, then the height of the tree would be
minimum.

If there are 'n' number of nodes in the binary tree.

Minimum height = h = log2(n+1) – 1

Maximum height = h= n-1

There are different types of binary trees and they are as follows:

Full / Strict Binary Tree

Full binary tree is also known as a strict binary tree. In strictly binary tree, every node should
have exactly two children or none. That means every internal node must have exactly two
children.

A binary tree in which every node has either two or zero number of children is called strictly
binary tree. Strictly binary tree is also called as full binary tree or proper binary tree or 2-tree.
A strictly binary tree with n leaves, will have (2n - 1) nodes.

Strictly binary tree data structure is used to represent mathematical expressions.

Example
Properties of Full Binary Tree
Let, i = Number of internal nodes
n = Total number of nodes
l = Number of leaves
λ = Number of levels
h = Height of the tree

• Number of leaves is l = i + 1.
• Total number of nodes is n = 2i + 1.
• Maximum number of nodes is 2h+1 -1.
• Minimum number of nodes in the full binary tree is 2h + 1.
• Minimum height of the full binary tree is log2(n+1) - 1.
• Maximum height of the full binary tree is (n-1)/2.
• Number of internal nodes is i = (n – 1) / 2.
• Number of leaves is l = (n + 1) / 2.
• Total number of nodes is n = 2l – 1.
• Number of internal nodes is i = l – 1.
• Number of leaves is at most 2λ - 1.
Complete Binary Tree

A complete binary tree is a binary tree in which all the levels are completely filled except
possibly the lowest one, which is filled from the left.

A complete binary tree is just like a full binary tree, but with two major differences

• All the leaf elements must lean towards the left.


• The last leaf element might not have a right sibling i.e. a complete binary tree doesn't have
to be a full binary tree.
Full Binary Tree vs Complete Binary Tree

Properties of Complete Binary Tree

• Maximum number of nodes in complete binary tree is 2h+1 - 1.


• Minimum number of nodes in complete binary tree is 2h.
• Minimum height of a complete binary tree is log2(n+1) - 1.

Relationship between array indexes and tree element

A complete binary tree has an interesting property that we can use to find the children and
parents of any node.
If the index of any element in the array is i, the element in the index 2i+1 will become the left
child and element in 2i+2 index will become the right child.

The parent of any element at index i is given by the lower bound of (i-1)/2.

Complete Binary Tree Applications

• Heap-based data structures


• Heap sort

Perfect Binary Tree


A perfect binary tree is a type of binary tree in which every internal node has exactly two child
nodes and all the leaf nodes are at the same level.

The below tree is not a perfect binary tree because all the leaf nodes are not at the same level.
• A perfect binary tree of height h has 2h + 1 – 1 node.
• A perfect binary tree with n nodes has height log2(n + 1) – 1.
• A perfect binary tree of height h has 2h leaf nodes.

Binary Tree Representation

A binary tree data structure is represented using two methods:

1. Array Representation
2. Linked List Representation

Consider the following binary tree:

Array Representation of Binary Tree (Sequential Representation)

In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent
a binary tree.

The above binary tree is represented as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

The root node of the tree will be present at the 1st index of the array. If a node is stored at ith
index then its left and right children will be stored at 2i and 2i+1 location.
To represent a binary tree of depth 'd' using array representation, we need one dimensional
array with a maximum size of 2d + 1.
This is the simplest memory allocation technique to store the tree elements but it is an
inefficient technique since it requires a lot of space to store the tree elements.
Linked List Representation of Binary Tree
In this representation, the binary tree is stored in the memory, in the form of a doubly linked
list where the nodes are stored at non-contiguous memory locations and linked together by
inheriting parent child relationship like a tree. In a double linked list, every node consists of
three fields. First field for storing left child address, second for storing actual data and third for
storing right child address.
In this linked list representation, a node has the following structure:

The above binary tree is represented as follows:

Each binary tree has a root pointer which points to the root node of the binary tree. In an empty
binary tree, the root pointer will point to null. The leaf node contains null in its left and right
pointers.
A node of a binary tree is represented by a structure containing a data part and two pointers to
other structures of the same type.
struct node
{
int data;
struct node *left, *right;
};
Traversal of Binary Tree

Tree Traversal algorithms can be classified broadly into two categories:

• Depth-First Search (DFS) Algorithms


o Inorder Traversal
o Preorder Traversal
o Postorder Traversal
• Breadth-First Search (BFS) Algorithms
o Level Order Traversal

Inorder traversal

In this type of traversal, first, we'll go to the left node, then we'll take the value of the current
node, and then we will go to the right node.

This technique follows the 'left root right' policy. It means that first left subtree is visited after
that root node is traversed, and finally, the right subtree is traversed. As the root node is
traversed between the left and right subtree, it is named inorder traversal. So, in the inorder
traversal, each node is visited in between of its subtrees.

Applications

• It is used to get the BST nodes in increasing order.


• It can also be used to get the prefix expression of an expression tree.

Algorithm
Until all nodes of the tree are not visited

Step 1 - Traverse the left subtree recursively.


Step 2 - Visit the root node.
Step 3 - Traverse the right subtree recursively.

Example

D→B→E→A→F→C→G

{15, 25, 28, 30, 35, 40, 45, 50, 55, 60, 70}

• Time complexity of inorder traversal is O(n), where 'n' is the size of binary tree.
• Space complexity of inorder traversal is O(h), where 'h' is the height of tree.

Preorder traversal

In this type of traversal, first, we will take the value of the current node(present node), then
we will go to the left node, and after that, we will go to the right node.

This technique follows the 'root left right' policy. It means that, first root node is visited after
that the left subtree is traversed recursively, and finally, right subtree is recursively traversed.
As the root node is traversed before (or pre) the left and right subtree, it is called preorder
traversal.
So, in a preorder traversal, each node is visited before both of its subtrees.

Applications

• It is used to create a copy of the tree.


• It can also be used to get the prefix expression of an expression tree.

Algorithm

Until all nodes of the tree are not visited

Step 1 - Visit the root node


Step 2 - Traverse the left subtree recursively.
Step 3 - Traverse the right subtree recursively.

Example

A→B→D→E→C→F→G
40, 30, 25, 15, 28, 35, 50, 45, 60, 55, 70

• Time complexity of preorder traversal is O(n), where 'n' is the size of binary tree.
• Space complexity preorder traversal is O(h), where 'h' is the height of the tree.

Postorder traversal

In this type of traversal, first, we'll go to the left node, then to the right node, and at last, we
will take the current node's value.

This technique follows the 'left-right root' policy. It means that the first left subtree of the root
node is traversed, after that recursively traverses the right subtree, and finally, the root node is
traversed. As the root node is traversed after (or post) the left and right subtree, it is called
postorder traversal.

So, in a postorder traversal, each node is visited after both of its subtrees.

Application

• It is used to delete the tree.


• It can also be used to get the postfix expression of an expression tree.

Algorithm
Until all nodes of the tree are not visited

Step 1 - Traverse the left subtree recursively.


Step 2 - Traverse the right subtree recursively.
Step 3 - Visit the root node.

Example

D→E→B→F→G→C→A

{15, 28, 25, 35, 30, 45, 55, 70, 60, 50, 40}

• Time complexity of postorder traversal is O(n), where 'n' is the size of binary tree.
• Space complexity of postorder traversal is is O(h), where 'h' is the height of tree.

Level order traversal

The level order traversal of a binary tree is also known as breadth first traversal.

Visit nodes level-by-level and left-to-right fashion at the same level. The most left child has
traversed first and then the other children of the same level from left to right have traversed.

Example

1-2-3-4-5-6-7

Using recursion

// Method to display the level order traversal of a binary tree

displayLevelorder(btree)

for h = 1 to height(btree)

displayCurrentLevel(btree, h);

// Method for displaying all the nodes at the current level

displayCurrentLevel(btree, level)

if btree is NULL, then exit from the method;

if the level is 1, then

display(btree->val);
else if the level is > 1, then

displayCurrentLevel(btree->left, level - 1);

displayCurrentLevel(btree->right, level - 1);

• Time complexity of the program is O(n2) in the worst case, where n is the total
number of nodes of a binary tree.

• Space complexity of the program is O(n) in the worst case, where n is the total
number of nodes of a binary tree.

Using Queue

We can also accomplish the level order traversal of a binary tree using a queue. Using queue,
first, we put all of the children of a node in a queue. The left child is put first in the queue,
followed by the right child. As a queue works on the FIFO (First In First Out) principle, the
left child comes out first, then the right child, and thus, the level ordered traversal of the tree is
achieved.

displayLevelorder(tree)

1. Create a queue that is empty. Let's say the queue is que

2. tNode = r // starting from the root

3. Iterate until the tNode is not NULL

a) display the tNode->val.

b) enqueue the children of the tNode (First the left child and then the right child) into que
c) A node n is then dequeue from the queue (que)

• Time Complexity is O(n), where n is the total number of nodes of a binary tree.
• Space complexity of the program is O(n), where n is the total number of nodes of a
binary tree.
Construction a binary tree from inorder and preorder traversals

The idea is to start with the root node, which would be the first item in the preorder sequence,
and find the boundary of its left and right subtree in the inorder sequence. To find the boundary,
search for the index of the root node in the inorder sequence. All keys before the root node in
the inorder sequence become part of the left subtree, and all keys after the root node become
part of the right subtree. Repeat this recursively for all nodes in the tree and construct the tree
in the process.

Input

Inorder Traversal: {4, 2, 1, 7, 5, 8, 3, 6}

Preorder Traversal: {1, 2, 4, 3, 5, 7, 8, 6}

Output Below binary tree

Construction of a binary tree from inorder and postorder traversals

The idea is to start with the root node, which would be the last item in the postorder sequence,
and find the boundary of its left and right subtree in the inorder sequence. To find the boundary,
search for the index of the root node in the inorder sequence. All keys before the root node in
the inorder sequence become part of the left subtree, and all keys after the root node become
part of the right subtree. Repeat this recursively for all nodes in the tree and construct the tree
in the process.

Input

Inorder: { 4, 2, 1, 7, 5, 8, 3, 6 }
Postorder: { 4, 2, 7, 8, 5, 6, 3, 1 }
Output Below binary tree

Construction of a binary tree from preorder and postorder traversals

Input:

Preorder traversal : { 1, 2, 4, 5, 3, 6, 8, 9, 7 }
Postorder traversal: { 4, 5, 2, 8, 9, 6, 7, 3, 1 }

We know that the root is the first element in the preorder sequence and the last element in the
postorder sequence. Therefore, the root node is 1. Then locate the next element in the preorder
sequence, which must be the left child of the root node. In this case, the left child is 2. Now
since 2 is the root node of the left subtree, all nodes before 2 in the postorder sequence must be
present in the left subtree of the root node, i.e., {4, 5, 2} and all the nodes after 2 (except the
last) must be present in the right subtree, i.e., {8, 9, 6, 7, 3}. Now the problem is reduced to
building the left and right subtrees recursively and linking them to the root node.

Output

You might also like