Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Trees
(Part – 2)
8.1 Introduction
struct bin_tree
{
int dat;
struct bin_tree *left, *right;
}
When these flags have value 1, then we can say that the
children are present. When lflag is 1 and rflag is 0, the
node has leftson but it does not have the right son.
When lflag is 1, we can say that the node has left child
and its address will be stored at the left. But when lflag is
0, in the left we will get the address of some other node in
the tree that is not the child of the original node. Actually
lflag = 0 has pointed out that left child is absent. These
addresses, which we store in left or right links form the
edges, which are not edges of the tree. These are known as
‘threads’. The tree thus formed is called as Threaded Binary
Tree.
1. You can create a simple binary tree and then thread it.
2. During creation only, form these threads.
30 20 28 27 42 10 36 38 39 32 25 37 , i.e.
3
20 0 42
10 28 36
27 32 38
25 37 39
Header
30
21 42
10 28 36
27 32 38
25 37 39
30 30
21
Thus
Header Header
30 30
21 21 42
28 28
27 27
# include <stdio.h>
# include <alloc.h>
# include<conio.h>
TR que[20], s, header;
TR pop();
void push(TR node);
int quempty();
int leftmost (TR temp);
int rightmost (TR temp);
TR getnode (int val);
void insert (TR new);
void display();
main()
{
TR t, new;
char c;
int val;
if (val != -99)
{
new = getnode(val);
insert (new);
}
}while (val != -99);
display ();
}
void display()
{
int i=0,p,q;
TR t;
t=s;
push(t);
push (&head);
print (“the breadth first search is ….\n”);
printf(“LEVEL %d: “,i);
While (!quempty())
{
t = pop ();
if (t == &head)
{
i ++;
printf(“\n########### LEVEL IS OVER
#####\n”);
getch();
printf(“\n\n LEVEL %d:
“,i);
printf(\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n”);
push (&head);
}
else
{
printf(“value is ….% d\n”, t->dat);
if (t->lflag)
p=rightmost(t->left);
else
p = t->left->dat;
if (t->rflag)
q=leftmost (ft->right);
else
q=t->right ->dat;
printf(“Predecessor is %d Successor is …%d\n”,p,q);
print(“\n***********************************\n”);
}
if(t->lflag)
push(t->left);
if(t->rflag)
push(t->right);
}
}
int qempty()
{
if (front = = end-1)
return(1);
return(0);
}
Output
LEVEL 2 :
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Value is …..3
Predecessor is …………10000 Successor is …..4
***********************************************************
value is ……9
Predecessor is ………7 Successor is …..12
***************************************
###################### LEVEL IS OVER #####################
LEVEL 3 :
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11
value is ……34
Predecessor is ………12 Successor is …..10000
***************************************
################ LEVEL IS OVER #####################
LEVEL 4 :
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
value is ……12
Predecessor is ………9 Successor is …..34
*****************************************************
30
21 42
10 28 36
27 32 38
25 37 39
Temp Print
STOP
Header
30
21 42
10 28 36
27 32 38
25 37 39
1. temp = leftmost(root)
2. if (temp has rightson)
then temp = leftmost(rightson)
else temp = temp right
3. repeat step 2 while temp ! = header