-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFStrvsl.cpp
64 lines (63 loc) · 1.14 KB
/
DFStrvsl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
#include<stdlib.h>
struct btnode{
int data;
struct btnode* l;
struct btnode* r;
};
typedef struct btnode bnode;
struct stacknode{
bnode* bnd;
struct stacknode* next;
};
typedef struct stacknode snode;
snode* top;
void push(btnode* bnode)
{
snode* newnode=(snode*)malloc(sizeof(snode));
newnode->bnd=bnode;
newnode->next=NULL;
if(top==NULL)
top=newnode;
else
{
newnode->next=top;
top=newnode;
}
}
bnode* pop()
{ snode* temp=top;
bnode* tempnd=top->bnd;
if(top==NULL)
{printf("Underflow\n");
return NULL;
}
top=temp->next;
free(temp);
return tempnd;
}
bnode* insert(int ndata,bnode* rtref)
{ bnode* newnode=(bnode*)malloc(sizeof(bnode));
newnode->data=ndata;
newnode->l=newnode->r=NULL;
if(rtref==NULL)
rtref=newnode;
else if(rtref->data<=ndata)
rtref->l=insert(ndata,rtref->l);
else rtref->r=insert(ndata,rtref->r);
return rtref;
}
void DFStrvsl(bnode* root)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
push(root);
printf("DFS traversal of tree is\n");
while(top)
{bnode* current=top->bnd;
if(current)
}
}