forked from codeIIEST/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinorder_traversal.cpp
133 lines (118 loc) · 3.42 KB
/
inorder_traversal.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include<iostream>
#include<vector>
using namespace std;
#define nullptr 0
typedef struct elem
{
int val;
struct elem* leftchild;
struct elem* rightchild;
struct elem* parent;
}node;//This is the node for the binary tree. Each element in the binary tree contains a value, a pointer to left child,right child and parent.
class BinaryTree
{
private:
int n=0;
node* root=nullptr;
public:
void insert(int x)//Insert function first searches for the place where the node can be inserted and then it inserts it there.
{
if(n==0)
{
root=new node;
root->val=x;
root->leftchild=nullptr;
root->rightchild=nullptr;
root->parent=nullptr;
}
else
{
node* temp=root;
node* newnode=searchnode(x,temp);
if(x>newnode->val)
{
newnode->rightchild=new node;
newnode->rightchild->val=x;
newnode->rightchild->parent=newnode;
newnode->rightchild->leftchild=nullptr;
newnode->rightchild->rightchild=nullptr;
}
else
{
newnode->leftchild=new node;
newnode->leftchild->val=x;
newnode->leftchild->parent=newnode;
newnode->leftchild->leftchild=nullptr;
newnode->leftchild->rightchild=nullptr;
}
}
n++;
}
node* searchnode(int x,node* temproot)//The search function searches for the node whose value is equal to the value it is searching for.It takes O(log n) as it is proportional to the height of the binary tree.
{
if(temproot->val<x)//If value is smaller than it goes to the right child
{
if(temproot->rightchild!=nullptr)
searchnode(x,temproot->rightchild);
else
return temproot;
}
else
{
if(temproot->val>x)//If value if larger it goes to the left child
{
if(temproot->leftchild!=nullptr)
searchnode(x,temproot->leftchild);
else
return temproot;
}
else
{
if(temproot->val==x)//It returns the value when it is equal
return temproot;
}
}
}
void printtree(node* temp)
{
if(temp==nullptr)
return;
else
{
printtree(temp->leftchild);
cout<<temp->val;
printtree(temp->rightchild);
}
}//This is the function to print the tree. It prints the tree in a sorted order.
node* giveroot()
{
return root;
}//This function returns the pointer to root
void inorder_traversal(node* temp)
{
if(temp==nullptr)
return ;
else
{
inorder_traversal(temp->leftchild);
cout<<temp->val<<endl;
inorder_traversal(temp->rightchild);
}
}
};
int main()
{
int n;
cin>>n;
BinaryTree bst;
for(int i=0;i<n;i++)
{
int temp;
cin>>temp;
bst.insert(temp);
}
node* temp=bst.giveroot();
// bst.printtree(temp);
//temp = bst.giveroot();
bst.inorder_traversal(temp);
}