Tree Data Structure
Tree Data Structure
We have all watched trees from our childhood. It has roots, stems,
branches and leaves.
Hence tree structure was used to explain hierarchical relationships,
e.g. family tree, animal kingdom classification, etc.
The tree is a nonlinear hierarchical data structure and comprises a
collection of entities known as nodes.
General tree: In the general tree, each node has either 0 or n number
of nodes. In this tree, there is no limitation to the number of nodes. It
starts with a root node and the children of the parent node make
another general sub tree.
Binary Tree: In a binary tree, each node can have at most 2 children
(i.e. either 0, 1, 2). There is no restriction as to what data will be in the
left child or right child.
Binary Search Tree: A binary search tree just like a binary tree can
have at most 2 children. It can have n nodes and also each node can be
defined as a data part that holds the data, left child and the right node.
Binary Trees
A binary tree is a tree-type non-linear data structure with a
maximum of two children for each parent.
A tree in which every root has either one or two nodes is called a
binary tree. The root component of a binary tree is having two
nodes. One is the right tree and the other is the left tree. It is called a
sub-tree.
Properties of Tree
● A binary tree is a special case of tree as defined in the preceding
section, in which no node of a tree can have a degree of more
than 2.
● Therefore, a binary tree is a set of zero or more nodes T such that:
○ (i) There is a specially designated node called the root of the
tree
○ (ii) The remaining nodes are partitioned into two disjoint
sets, T1, and T2, each of which is a binary tree.
● T1 is called the left sub tree and T2 is called the right subtree, or
vice-versa. A binary tree is shown in Figure
● Sequential representation
Using total number of nodes by array
● Linked representation
In this representation, each node requires three fields, one
for the link of the left child, second field for representing the
information associated with the node and the third field
representing the link of the right child.
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit the root node.
Step 3 − Recursively traverse the right subtree.
Pre-order Traversal
● In this traversal method, the root node is visited first, then the left
subtree and finally the right subtree.
Algorithm
Until all nodes are traversed −
Step 1 − Visit the root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse the right subtree.
Post-order Traversal
● In this traversal method, the root node is visited last, hence the
name. First we traverse the left subtree, then the right subtree
and finally the root node.
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse the right subtree.
Step 3 − Visit the root node.
Example
#include <stdio.h>
#include <stdlib.h>
struct node
{
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root)
{
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// Preorder traversal
void preorderTraversal(struct node* root)
{
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// Postorder traversal
void postorderTraversal(struct node* root)
{
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
int main()
{
struct node* root = createNode(1);
clrscr();
insertLeft(root, 2);
insertRight(root, 3);
insertLeft(root->left, 4);