-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathInorderTraversal.cpp
More file actions
82 lines (76 loc) · 1.44 KB
/
Copy pathInorderTraversal.cpp
File metadata and controls
82 lines (76 loc) · 1.44 KB
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
#include <iostream>
using namespace std;
// Uses of Inorder Traversal:
// In the case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order.
// To get nodes of BST in non-increasing order, a variation of Inorder traversal where Inorder traversal is reversed can be used.
struct node
{
int data;
node *lc;
node *rc;
};
struct que
{
int status;
node *ptr;
que *next;
};
struct que *first=NULL;
struct que *last=NULL;
node *insert(int x,node *root)
{
node *temp=new node;
que *q=new que;
temp->data=x;
temp->lc=NULL;
temp->rc=NULL;
q->status=0;
q->next=NULL;
if(root==NULL)
{
q->ptr=temp;
first=last=q;
return temp;
}
else
{
if(first->status==0)
{
first->ptr->lc=temp;
first->status=1;
}
else
{
first->ptr->rc=temp;
first=first->next;
}
last->next = q;
q->ptr = temp;
last = q;
}
return root;
}
void inorder(node *root)
{
if(root == NULL)
return;
else
{
cout<<root->data<<" ";
inorder(root->lc);
inorder(root->rc);
}
}
int main()
{
node *root = NULL;
cout<<"Tree Elements in Array Form : ";
for(int i = 0; i < 10; i++)
{
cout<<i<<" ";
root = insert(i,root);
}
cout<<"\nInorder Traversal : ";
inorder(root);
return 0;
}