Trees of mumbai
Trees of mumbai
Tree Forest
E B
D C F H G
Figure 1
1
Example: Nodes = {A,B,C,D,E,F,G,H}
Edges = {(A,B),(A,E),(B,F),(B,G),(B,H),(E,C),(E,D)}
A tree satisfies the following properties:
1. It has one designated node, called the root, that has no parent.
2. Every node, except the root, has exactly one parent.
3. A node may have zero or more children.
4. There is a unique directed path from the root to each node.
5 5
3 2 3 2
4 1 6 4 1 6
Fig-2 Tree Fig-3 not a tree
Tree Terminology:
B C
D E F G H
Fig- 3
I
Root: Only node with no parent , Example: A
Parent of x: The node directly above node x in the tree (A,B,C,H)
2
Child of x: A node directly below node x in the tree Ex. (B,C,D,E,F,G,H,I)
Siblings : Two or more nodes which have common parent. Ex. (B,C),(D,E),(F,G,H)
Degree of a node: Number of children (or subtrees) of a given node is termed as degree of a
node.
Leaf / external nodes : Nodes which have degree zero are called leaxf nodes. (or A node with
no children). Ex. (D,E,F,G,I)
Non leaf nodes: (B,C,H)
Level of a node : The distance of a node from root is called as the level of the node
(Or) The number of edges used / processed to reach a node.
Generally, the root has a zero distance from itself, the root is at level 0. The, children
of the root are at level 1, their children are at level at 2, and so on.
Height of a tree : The number of nodes processed to reach the leaf node at the last level is
termed as height of the tree.
(or) Height of a tree = Max. level + 1
Level 0
Level 1
Level 2
Fig - 4
3
TERM DESCRIPTION EXAMPLE
Depth Length of the path to that node from the root 2 for D
Degree of a Number of children connected from that node 3 for A, 1 for B,D, 2 for
C and 0 for leaves
Node
Degree of a Degree of a node which has maximum degree 3 (since A has Max.
degree)
Tree
Binary Tree: A tree in which each node has at most 2 children (either 0,1 or 2 sub trees) is called
as binary tree. A tree whose elements have at most 2 children is called a binary tree. Since each
element in a binary tree can have only 2 children, we typically name them the left and right child.
4
Types of Binary Tree:
1) Strictly Binary tree : A binary tree with each node having either 0 or 2 children.
A A
B C B C
D F
2) Complete Binary tree: It is a binary tree where in at ith level, it should have 2 nodes
And at last level, the nodes should be left filled.
A
A
B C
B C
Fig-A Fig-B
D E F G
B C
F G
Fig-C
5
Note: Fig-A, Fig-B are complete binary trees and Fig-C is not a complete binary tree.
A A
B B
C
C
a b c d
BST: Binary Search tree exhibits a special behavior. A node's left child must have a value less than
its parent's value and the node's right child must have a value greater than its parent value.
Example ) Refer below fig-E
A full binary tree: is a tree in which every node other than the leaves has two children.
Note: All leaves are at same level and all other nodes each have two children.
A full binary tree of height h has at most 2h-1 nodes.
Example ) Refer below fig-E
6
Fig-E
A A
B C B C
D E
A B C
1 2 3
A B C D E
1 2 3 4 5 6 7
3) Linked representation:
7
2. Pointer to left child
3. Pointer to right child
Example)
struct node
{
int data;
struct node *left;
struct node *right;
};
8
• Organizational structure of an organization
• Class inheritance tree.
– Problem representation, e.g.,
• Expression tree.
• Decision tree.
– Efficient algorithmic solutions, e.g.,
• Search trees.
• Efficient priority queues via heaps.
9
Traverse a binary tree in left-right-root
if(root is not null)
1. postorder (leftsubtree)
2. postorder (rightsubtree)
3. process (root)
Example)
Preorder: A B D E H I C F J K G
Inorder: D B H E I A F K J C G
Postorder: D H I E B K J F G C A
#include<stdio.h>
#include<stdlib.h>
struct list
{
int data;
struct list *left;
struct list *right;
};
10
typedef struct list *TREE;
TREE createlocation()
{
TREE temp;
temp=(TREE)malloc(sizeof(struct list));
return(temp);
}
TREE insert(TREE root,int item) // recursive function
{
TREE temp;
temp=createlocation();
temp->data = item;
if(root == NULL) // binary tree is completely empty
{
temp->left = NULL;
temp->right = NULL;
root = temp;
}
else
{
if(root->data>item)
root->left = insert(root->left,item);
else
root->right = insert(root->right,item);
}
return (root);
}
void inorder(TREE root)
{
if(root!=NULL)
{
inorder(root->left);
printf(",%d",root->data);
inorder(root->right);
}
}
void preorder(TREE root)
{
if(root!=NULL)
{
printf(" ,%d",root->data);
preorder(root->left);
11
preorder(root->right);
}
}
12