Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
A tree data structure is a hierarchical structure that is used to represent and organize
data in a way that is easy to navigate and search. It is a collection of nodes that are
connected by edges and has a hierarchical relationship between the nodes.
The topmost node of the tree is called the root, and the nodes below it are called the child
nodes. Each node can have multiple child nodes, and these child nodes can also have their
own child nodes, forming a recursive structure.
This data structure is a specialized method to organize and store data in the computer to
be used more effectively. It consists of a central node, structural nodes, and sub-nodes,
1
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
which are connected via edges. We can also say that tree data structure has roots,
branches, and leaves connected with one another.
2
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
Parent Node: The node which is a predecessor of a node is called the parent node of
that node. {B} is the parent node of {D, E}.
Child Node: The node which is the immediate successor of a node is called the child
node of that node. Examples: {D, E} are the child nodes of {B}.
Root Node: The topmost node of a tree or the node which does not have any parent
node is called the root node. {A} is the root node of the tree. A non-empty tree must
contain exactly one root node and exactly one path from the root to all other nodes of
the tree.
Leaf Node or External Node: The nodes which do not have any child nodes are called
leaf nodes. {K, L, M, N, O, P, G} are the leaf nodes of the tree.
Ancestor of a Node: Any predecessor nodes on the path of the root to that node are
called Ancestors of that node. {A,B} are the ancestor nodes of the node {E}
Descendant: Any successor node on the path from the leaf node to that node. {E,I} are
the descendants of the node {B}.
Sibling: Children of the same parent node are called siblings. {D,E} are called siblings.
Level of a node: The count of edges on the path from the root node to that node. The
root node has level 0.
Internal node: A node with at least one child is called Internal Node.
3
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
Neighbour of a Node: Parent or child nodes of that node are called neighbors of that
node.
Subtree: Any node of the tree along with its descendant.
Depth. In a tree, many edges from the root node to the particular node are called the
depth of the tree. In the tree, the total number of edges from the root node to the leaf
node in the longest path is known as "Depth of Tree". In the tree data structures, the
depth of the root node is 0.
A tree consists of a root, and zero or more subtrees T1, T2, … , Tk such that there is an
edge from the root of the tree to the root of each subtree.
Here,
Node 1 is the root node
5
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
Binary tree: In a binary tree, each node can have a maximum of two children linked to
it. Some common types of binary trees include full binary trees, complete binary trees,
balanced binary trees, and degenerate or pathological binary trees.
Ternary Tree: A Ternary Tree is a tree data structure in which each node has at most
three child nodes, usually distinguished as “left”, “mid” and “right”.
6
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
N-ary Tree or Generic Tree: Generic trees are a collection of nodes where each node is
a data structure that consists of records and a list of references to its children(duplicate
references are not allowed). Unlike the linked list, each node stores the address of
multiple nodes.
Traversal:
data structure.
In order Traversal – perform Traveling a tree in an in-order manner.
7
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
tree has N nodes then it will have (N-1) edges. There is only one path from each node to
any other node of the tree.
Depth of a node: The depth of a node is defined as the length of the path from the root
to that node. Each edge adds 1 unit of length to the path. So, it can also be defined as
the number of edges in the path from the root of the tree to the node.
Height of a node: The height of a node can be defined as the length of the longest path
of the node. The degree of a leaf node must be 0. The degree of a tree is the maximum
degree of a node among all the nodes in the tree.
8
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
File System
2. Trees (with some ordering e.g., BST) provide moderate access/search (quicker than
Linked List and slower than arrays).
3. Trees provide moderate insertion/deletion (quicker than Arrays and slower than
Unordered Linked Lists).
4. Like Linked Lists and unlike Arrays, Trees don’t have an upper limit on the number of
nodes as nodes are linked using pointers.
9
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
Data Compression: Huffman coding is a popular technique for data compression that
involves constructing a binary tree where the leaves represent characters and their
frequency of occurrence. The resulting tree is used to encode the data in a way that
minimizes the amount of storage required.
Compiler Design: In compiler design, a syntax tree is used to represent the structure of
a program.
Database Indexing: B-trees and other tree structures are used in database indexing to
10
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
6. K-D Tree: A space partitioning tree used to organize points in K dimensional space.
7. Trie : Used to implement dictionaries with prefix lookup.
8. Suffix Tree : For quick pattern searching in a fixed text.
9. Spanning Trees and shortest path trees are used in routers and bridges respectively in
computer networks
10. As a workflow for compositing digital images for visual effects.
11. Decision trees.
12. Organization chart of a large organization.
13. In XML parser.
14. Machine learning algorithm.
15. For indexing in database.
16. IN server like DNS (Domain Name Server)
17. In Computer Graphics.
18. To evaluate an expression.
19. In chess game to store defense moves of player.
20. In java virtual machine.
21. Tree data structures are used to organize and manage files and directories in a file
system. Each file and directory is represented as a node in the tree, with parent-child
relationships indicating the hierarchical structure of the file system.
22. Tree data structures are often used in parsing, such as in compilers and interpreters,
to represent the structure of a program or a document.
11
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
23. Tree data structures, such as binary search trees, are commonly used to implement
efficient searching and sorting algorithms.
24. Graphics and UI design
25. Tree data structures are commonly used in decision-making algorithms in artificial
intelligence, such as game-playing algorithms, expert systems, and decision trees.
26. Tree data structures can be used to represent the topology of a network and to
calculate routing tables for efficient data transmission.
recursive algorithms.
Disadvantages of Tree Data Structure:
Unbalanced Trees, meaning that the height of the tree is skewed towards one side,
12
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
The implementation and manipulation of trees can be complex and require a good
understanding of the algorithms.
Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only
one logical way to traverse them, trees can be traversed in different ways.
A Tree Data Structure can be traversed in following ways:
1. Depth First Search or DFS
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
2. Level Order Traversal or Breadth First Search or BFS
3. Boundary Traversal
4. Diagonal Traversal
13
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
Tree Traversal
14
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
Inorder Traversal:
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left->subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right->subtree)
In the case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing
order. To get nodes of BST in non-increasing order, a variation of Inorder traversal where
Inorder traversal is reversed can be used.
Code implementation of Inorder traversal. (Exp.4.a)
#include <stdio.h>
15
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
#include <stdlib.h>
struct node {
int data;
};
16
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
node->data = data;
node->left = NULL;
node->right = NULL;
17
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
return (node);
if (node == NULL)
return;
18
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
printInorder(node->left);
printInorder(node->right);
19
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
// Driver code
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
20
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
// Function call
printInorder(root);
getchar();
return 0;
Output
Inorder traversal of binary tree is
42513
21
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
Preorder Traversal:
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left->subtree)
3. Traverse the right subtree, i.e., call Preorder(right->subtree)
Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get
prefix expressions on an expression tree.
Code implementation of Preorder traversal:
#include <stdio.h>
22
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
#include <stdlib.h>
struct node {
int data;
};
23
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
node->data = data;
node->left = NULL;
24
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
node->right = NULL;
return (node);
if (node == NULL)
return;
25
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
printPreorder(node->left);
printPreorder(node->right);
26
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
// Driver code
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
27
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
// Function call
printPreorder(root);
getchar();
return 0;
Output
Preorder traversal of binary tree is
12453
28
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
Postorder Traversal:
Postorder traversal is used to delete the tree. Please see the question for the deletion of a
tree for details. Postorder traversal is also useful to get the postfix expression of an
expression tree
29
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
30
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
};
31
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
32
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
if (node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
33
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
// Driver code
int main()
root->left = newNode(2);
34
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
// Function call
printPostorder(root);
getchar();
return 0;
35
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
Output
Postorder traversal of binary tree is
45231
Some other Tree Traversals Techniques:
Some of the other tree traversals are:
36
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
Boundary Traversal
The Boundary Traversal of a Tree includes:
1. left boundary (nodes on left excluding leaf nodes)
2. leaves (consist of only the leaf nodes)
3. right boundary (nodes on right excluding leaf nodes)
37
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
Diagonal Traversal
In the Diagonal Traversal of a Tree, all the nodes in a single diagonal will be printed one
by one.
Input :
38
Unit III – Topic 1 – TREE ADT & TREE TRAVERSAL
39