0% found this document useful (0 votes)
31 views

Trees of mumbai

The document provides a comprehensive overview of tree data structures, detailing their properties, terminology, and types, including binary trees and their specific categories. It explains tree representation methods, such as array and linked representations, and discusses various applications and traversal techniques. Additionally, it includes a C program for creating and traversing a binary tree.

Uploaded by

saichandnara2102
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)
31 views

Trees of mumbai

The document provides a comprehensive overview of tree data structures, detailing their properties, terminology, and types, including binary trees and their specific categories. It explains tree representation methods, such as array and linked representations, and discusses various applications and traversal techniques. Additionally, it includes a C program for creating and traversing a binary tree.

Uploaded by

saichandnara2102
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
You are on page 1/ 12

TREES

A tree is a non-linear data structure in which data is stored in a hierarchical fashion.


A tree is a finite set of one or more nodes that represents / exhibits “parent - child” relationship such
that
i) There is a special designated node called root
ii) The remaining nodes are portioned into disjoint subsets of nodes like T1, T2,… Tn termed as sub-
trees.

Tree Forest

Computer Programmer / Software Engineer View


Root Branches Leaves

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

Note : The below table is explained with Fig-4


Note: Group of trees known as forest.

3
TERM DESCRIPTION EXAMPLE

Node An item or single element represented in a tree A,B,C…,H

Root Node that does not have any ancestors (parent A


or Grandparent)

Sub tree Internal nodes in a tree which has both B,C,D


ancestor (parent) and descendant (child)

Leaf External nodes that does not have any E,F,G,H


descendant(child)

Edge The line depicts the connectivity between two (A-B),(A-C)…


Nodes

Path Sequence of nodes connected A-B-E for E from root

Height Length of the longest path from the root 3

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.

Skewed Tree : If a binary tree is built only on one side.

A A

B B

C
C

Left Skewed tree Right skewed tree


Expression Tree:
All internal nodes are operators
*
All leaf nodes are operands
Example : (a+b)*(c+d)
+ -

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

Representation of a Binary Tree:


Binary tree can be represented in two ways
1) Array representation:
In array representation root is stored at starting subscript / index of the array, its left child is
stored at the index 2*i (where i is the index of parent) and its right child is stored at 2*i+1
index.
(Assume that array subscript/ index starts from 1)
Note: 1) The major disadvantage with this type of representation is wastage of memory.
Example: In the skewed tree, half of the array is unutilized.
2) Allows only static representation. There is no possible way to enhance the size of the tree.

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:

In linked representation of a Binary Tree node contains following parts.


1. Data

7
2. Pointer to left child
3. Pointer to right child

Example)
struct node
{
int data;
struct node *left;
struct node *right;
};

Advantages of linked representation


 This representation is superior to the array representation as there is no wastage of memory.
 There is no need to have prior knowledge of depth of the tree. Using dynamic memory allocation
concept one can create as much memory (node) as required.
 Insertion and deletion which are the most common operations can be done without moving the
other nodes.
Applications of Trees
Trees are very important data structures in computing.
They are suitable for:
– Hierarchical structure representation, e.g.,
• File directory.

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.

Binary Tree Traversal Techniques


There are three techniques / methods for binary tree traversal.
1. Preorder Traversal
2. Inorder Traversal
3. Postorder Traversal
1. Preorder Traversal :
Traverse a binary tree in root-left-right
if(root is not null)
1. process(root)
2. preorder (left subtree)
3. preorder (right subtree)
2. Inorder Traversal
Traverse a binary tree in left-root-right
if(root is not null)
1. inorder (leftsubtree)
2. process (root)
3. inorder (rightsubtree)
3. Postorder Traversal

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

Write a C program that uses functions to perform the following:


i) Creating a Binary Tree of integers.
ii) Traversing the above binary tree in preorder, inorder and postorder.

#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);
}
}

void postorder(TREE root)


{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d\t",root->data);
}
}
void main()
{
TREE root=NULL;
int choice,item;
for(;;)
{
printf("Enter the Chocie\n");
printf("1:Insert \t 2: inorder \t 3: preorder \t 4: postorder \t 5: Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the item\n");
scanf("%d",&item);
root=insert(root,item);
break;
case 2: inorder(root);
break;
case 3: preorder(root);
break;
case 4: postorder(root);
break;
case 5: exit(0);
default: printf("invalid option \n");
}
}
}

12

You might also like