Unit8 Tree
Unit8 Tree
ers.com
Engaging Peers, Inspiring Careers!
Left
Sub
Tree
Right
Sub
Tree
Binary Tree
-- A Tree is said to be a binary tree that every
element in a binary tree has at the most two sub
trees. ( means zero or one or two )
Types of binary trees
-- Strictly binary tree
-- Complete binary tree
-- Extended binary tree
Tree Terminology.
Root : The basic node of all nodes in the tree. All operations on the tree are performed with passing root
node to the functions.( A is the root node in above example.)
Child : a successor node connected to a node is called child. A node in binary tree may have at most two
children. ( B and C are child nodes to the node A, Like that D and E are child nodes to the node B. )
Parent : a node is said to be parent node to all its child nodes. ( A is parent node to B,C and B is parent
node to the nodes D and F).
Leaf : a node that has no child nodes. ( D, E and F are Leaf nodes )
Siblings : Two nodes are siblings if they are children to the same parent node.
Ancestor : a node which is parent of parent node ( A is ancestor node to D,E and F ).
Descendent : a node which is child of child node ( D, E and F are descendent nodes of node A )
Level : The distance of a node from the root node, The root is at level 0,( B and C are at Level 1 and D,
E, F have Level 2 ( highest level of tree is called height of tree )
Degree : The number of nodes connected to a particular parent node.
C
D
Root
D
C
A
C
Tree Created :
D
H
Output :
Preorder Traversal : A B D E H C F G
Inorder Traversal : D B H E A F C G
Postorder Traversal : D H E B F G C A
preorder(t->lchild);
preorder(t->rchild);
}
}
void postorder(struct node *t) {
if(t!=NULL) {
postorder(t->lchild);
postorder(t->rchild);
printf("%5d",t->data);
}
}
void search(struct node *r,int key){
struct node *q;
, fnode=NULL; q=r;
while(q!=NULL) {
if(q->data == key) {
fnode = q; return;
}
parent=q;
if(q->data > key) q=q->lchild;
else q=q->rchild;
}
}
void delnode ( struct node **r,int key) {
struct node *succ,*fnode,*parent;
if(*r==NULL) {
printf("Tree is empty"); return;
}
parent=fnode=NULL;
*
2
3
1
2*(a1)+(3*b)
Graphs
A Tree is in fact a special type of graph. Graph is a data structure having a group of nodes
connecting with cyclic paths.
A Graph contains two finite sets, one is set of nodes is called vertices and other is set of edges.
A Graph is defined as G = ( V, E ) where.
i) V is set of elements called nodes or vertices.
ii) E is set of edges, identified with a unique pair ( v1, v2 ) of nodes. Here ( v1, v2 ) pair denotes that there
is an edge from node v1 to node v2.
Un-directed Graph
Directed Graph
D
D
Set of vertices = { A, B, C, D, E }
Set of edges = { ( A, B ) , ( A, C ) , ( B, D ),
( C, D ),( C, E ) , ( D, E ) }
A graph in which every edge is undirected is
known as Un directed graph.
The set of edges are unordered pairs.
Edge ( A, B ) and ( B, A ) are treated as same
edge.
Set of vertices = { A, B, C, D, E }
Set of edges = { ( A, B ) , ( A, C ) , ( B, B ), ( B, D ),
( C, A ), ( C, D ), ( C, E ) , ( E, D ) }
A graph in which every edge is directed is known as
Directed graph.
The set of edges are ordered pairs.
Edge ( A, B ) and ( B, A ) are treated as different edges.
The edge connected to same vertex ( B, B ) is called loop.
Out degree : no of edges originating at a given node.
In degree : no of edges terminating at a given node.
For node C - In degree is 1 and out degree is 3 and
total degree is 4.
Representation of Graph
Set of vertices = { A, B, C, D, E }
Set of edges = { ( A, B ) , ( A, C ) , ( A, D ), ( B, D ), ( B, D ),
( C, D ),( C, E ) , ( D, E ) }
There are two ways of representing a graph in memory.
i)
Sequential Representation by means of Adjacency Matrix.
ii)
Linked Representation by means of Linked List.
A
C
B
D
Linked List
Adjacency Matrix
A
B
C
D
E
A B
0 1
1 0
1 1
1 1
0 0
C
1
1
0
1
1
D
1
1
1
0
1
E
0
0
1
1
0
D NULL
D NULL
NULL
NULL
D NULL
FaaDoOEngine
ers.com
Engaging Peers, Inspiring Careers!