-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree2.cpp
112 lines (110 loc) · 1.39 KB
/
tree2.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<stdio.h>
#include<stddef.h>
#include<stdlib.h>
int top=0,size=100;
typedef struct node
{
char data;
struct node *lchild,*rchild;
}node;
node *t,*root=NULL,*stack[100];
void push(node *el)
{
stack[0]=NULL;
if(top>=size-1)
printf("overflow");
else
{
top++;
stack[top]=el;
}
}
node * pop()
{
node *s;
if(top==0)
printf("underflow");
else
{
s=stack[top];
top--;
return s;
}
}
void btcreate(node *parent)
{
int i,l;
if(parent!=NULL)
{
printf("enter the left node 1/0");
scanf(" %d",&i);
if(i==1)
{
t=(node *)malloc(sizeof(node));
scanf(" %c",&t->data);
parent->lchild=t;
btcreate(t);
}
else
{
parent->lchild=NULL;
}
printf("enter the right child 1/0");
scanf(" %d",&i);
if(i==1)
{
t=(node *)malloc(sizeof(node));
scanf(" %c",&t->data);
parent->rchild=t;
btcreate(t);
}
else
{
parent->rchild=NULL;
}
}
}
void preorder(node *parent)
{
while(parent!=NULL)
{
printf(" %c",parent->data);
if(parent->rchild!=NULL)
push(parent->rchild);
if(parent->lchild!=NULL)
parent=parent->lchild;
else
parent=pop();
}
}
/*void inorder(node *parent)
{
while(parent!=NULL)
{
if(parent->rchild!=NULL)
push(parent->rchild);
else if(parent->lchild!=NULL)
{
push(parent);
parent=parent->lchild;
}
else
{
printf(" %c",parent);
parent=pop();
}
if(parent!=NULL)
{
printf(" %c",parent);
parent=pop();
}
}
} */
int main()
{
printf("enter the root");
root=(node *)malloc(sizeof(node));
scanf(" %c",&root->data);
btcreate(root);
preorder(root);
}