File tree Expand file tree Collapse file tree 1 file changed +78
-0
lines changed Expand file tree Collapse file tree 1 file changed +78
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ struct node
5
+ {
6
+ int data;
7
+ node *lc;
8
+ node *rc;
9
+ };
10
+
11
+ struct que
12
+ {
13
+ int status;
14
+ node *ptr;
15
+ que *next;
16
+ };
17
+ struct que *first=NULL ;
18
+ struct que *last=NULL ;
19
+
20
+ node *insert (int x,node *root)
21
+ {
22
+ node *temp=new node;
23
+ que *q=new que;
24
+ temp->data =x;
25
+ temp->lc =NULL ;
26
+ temp->rc =NULL ;
27
+ q->status =0 ;
28
+ q->next =NULL ;
29
+
30
+ if (root==NULL )
31
+ {
32
+ q->ptr =temp;
33
+ first=last=q;
34
+ return temp;
35
+ }
36
+ else
37
+ {
38
+ if (first->status ==0 )
39
+ {
40
+ first->ptr ->lc =temp;
41
+ first->status =1 ;
42
+ }
43
+ else
44
+ {
45
+ first->ptr ->rc =temp;
46
+ first=first->next ;
47
+ }
48
+ last->next = q;
49
+ q->ptr = temp;
50
+ last = q;
51
+ }
52
+ return root;
53
+ }
54
+ void preorder (node *root)
55
+ {
56
+ if (root == NULL )
57
+ return ;
58
+ else
59
+ {
60
+ preorder (root->lc );
61
+ cout<<root->data <<" " ;
62
+ preorder (root->rc );
63
+ }
64
+ }
65
+ int main ()
66
+ {
67
+ node *root = NULL ;
68
+ int ht;
69
+ cout<<" Tree Elements in Array Form : " ;
70
+ for (int i = 0 ; i < 10 ; i++)
71
+ {
72
+ cout<<i<<" " ;
73
+ root = insert (i,root);
74
+ }
75
+ cout<<" \n Preorder Traversal : " ;
76
+ preorder (root);
77
+ return 0 ;
78
+ }
You can’t perform that action at this time.
0 commit comments