Data Struture Unit 4
Data Struture Unit 4
Data Struture Unit 4
NON-LINEAR
DATA
STRUCTURES
TREES STRUCTURE
Definition:
Tree is a non-linear data structure. It organized the data in
hierarchical manner. A tree is a finite set of one or more nodes
such that there is a specially designated node called the root
node and root node can have zero or more sub trees T1,T2,T3,…
,Tn. Each of whose roots are connected by a
directed edge from root R.
Tree is collection of nodes in which the first node is called
root and root has many number of sub tree T1, T2, T3… Tn.
Terms:
1.Root
A node which does not have a parent is called as root node.
2.Node
Each data element in the tree is called as node.
3.Leaf node
A node which does not have any children is called leaf node.
4.Siblings
A child of same parent is called sibling.
5.Path
A path from node n1 to nk is defined has sequence of nodes
n1, n2, n3…..nk. Such that ni is a parent
of ni +1.Example: A->B->E->J
6.Length for a path
Number of edges in the path.
Example: Consider path from A to J is 3
7.Degree
Number of sub trees of the node is called degree.
8.Level
Root is at level 1 then i+s children are at
level 2+1 Example: level
9.Depth
For any node n, the depth n is length of unique path from root to n.
10.Height
For any node n, the height of node n is the length of longest path from
n to left.
11.Forest
Collection of tree node is known as forest.
BINARY TREE ADT
Definition: -
0 1 2 3 4 5 6
Array representation of Binary Tree
2. Linked representation
The elements are represented using pointers. Each node in
linked representation has three fields, namely,
* Data field
3. Postorder Traversal
Inorder Traversal
The inorder traversal of a binary tree is performed as
* Traverse the left subtree in inorder
* Visit the root
Traverse the right subtree in inorder.
* Recursive Routine for Inorder
Traversal void Inorder (Tree T)
{
if (T!=NULL)
{
Inorder (T->left);
printf(“%d”,T->data);
Inorder (T->right);
}
}
EXPRESSION TREE
An expression tree is tree in which left nodes have the
operands and interior node have the operators.
Like binary tree, expression tree can also be travesed
by inorder, preorder and postorder traversal. Example:
+ -
A B C D
Constructing an Expression Tree
following steps:
3.Repeated the above steps until reach the end of the expression.
Definition: -
Operations of BST.
•Insertion
•Deletion
•Find
•Find min
•Find max
•Retrieve
Notes: when you’re constructing the binary tree the
given elements are read from first. Example:
struct treenode
{
int data;
struct treenode *left; struct
treenode *right;
};
Routine for perform find.
struct treenode * find(struct treenode *T,int x)
{
if(T==NULL)
return NULL; else if(x<T-
>data)
return find(T->left,x); else
if(x>T->data)
return find(T->right,x);
Else
}
return T;
Routine for perform insertion.
struct treenode* insert(struct treenode *t,int x)
{
if(t==NULL
{
t=(struct treenode *)malloc(sizeof(struct
treenode)); t->data=x;
t->left=t->right=NULL;
return t;
}
else if(x<t->data)
t->left=insert(t->left,x); else
if(x>t->data)
t->right=insert(t->right,x);
return t;
}
Routine for perform deletion
struct treenode * findmin(struct treenode *t)
{
if(t==NULL)
return NULL; /* There is no element in the tree */
return findmin(t->left);
}
struct treenode* deletion(struct treenode *t,int x)
{
struct treenode *temp;
if(t==NULL)
printf("Element not found\n");
else if(x < t->data )
t->left = deletion (t->left,x);
else if(x > t->data )
t->right = deletion (t->right,x);
else if(T->right && T->left)
{
/* Here we will replace with minimum element in the right
sub tree */ temp = findmin(t->right);
t->data = temp->data ;
Else
{
/* If there is only one or zero children then we can directly
remove it from the tree and connect its parent to its child */
temp = T;
if(t->left==NULL)
t = t->right;
elseif(T-> right==NULL)
t = t->left;
free(temp); /* temp is longer required */
}
return T;
}
void inorder(struct treenode *t)
{
if(t!=NULL)
{
inorder(t->left); printf("%d \t",t->data); inorder(t->right);
}
}
Applications of Tree
1. Manipulate hierarchical data.
5. Router algorithms
DIFFERENCE BETWEEN BINARY AND BINARY SEARCH
TREES:
It is a tree with only two It is also a tree with only two children.
children
Tree Representation
A tree data structure can be used to represent a disjoint set ADT.
Each set is represented by a tree. The elements in the tree have
the same root and hence the root is used to name the set.
Operations
1. Find
2. Union