Skip to content

Commit f27bfc2

Browse files
authored
Merge pull request #214 from Jatin86400/traversals
Traversals
2 parents ed483af + 1aeb5ce commit f27bfc2

File tree

6 files changed

+426
-0
lines changed

6 files changed

+426
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Inorder Traversal
2+
3+
![alt text](https://javabeat.net/wp-content/uploads/2013/11/BST_Inorder.jpg)
4+
5+
Algorithm Inorder(tree)
6+
* 1. Traverse the left subtree, i.e., call Inorder(left-subtree)
7+
* 2. Visit the root.
8+
* 3. Traverse the right subtree, i.e., call Inorder(right-subtree)
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include<iostream>
2+
#include<vector>
3+
using namespace std;
4+
#define nullptr 0
5+
typedef struct elem
6+
{
7+
int val;
8+
struct elem* leftchild;
9+
struct elem* rightchild;
10+
struct elem* parent;
11+
}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.
12+
class BinaryTree
13+
{
14+
private:
15+
int n=0;
16+
node* root=nullptr;
17+
public:
18+
void insert(int x)//Insert function first searches for the place where the node can be inserted and then it inserts it there.
19+
{
20+
21+
if(n==0)
22+
{
23+
root=new node;
24+
root->val=x;
25+
root->leftchild=nullptr;
26+
root->rightchild=nullptr;
27+
root->parent=nullptr;
28+
}
29+
else
30+
{
31+
node* temp=root;
32+
node* newnode=searchnode(x,temp);
33+
if(x>newnode->val)
34+
{
35+
newnode->rightchild=new node;
36+
newnode->rightchild->val=x;
37+
newnode->rightchild->parent=newnode;
38+
newnode->rightchild->leftchild=nullptr;
39+
newnode->rightchild->rightchild=nullptr;
40+
}
41+
else
42+
{
43+
newnode->leftchild=new node;
44+
newnode->leftchild->val=x;
45+
newnode->leftchild->parent=newnode;
46+
newnode->leftchild->leftchild=nullptr;
47+
newnode->leftchild->rightchild=nullptr;
48+
}
49+
50+
}
51+
n++;
52+
53+
}
54+
55+
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.
56+
{
57+
58+
if(temproot->val<x)//If value is smaller than it goes to the right child
59+
{
60+
if(temproot->rightchild!=nullptr)
61+
searchnode(x,temproot->rightchild);
62+
else
63+
return temproot;
64+
}
65+
else
66+
{
67+
if(temproot->val>x)//If value if larger it goes to the left child
68+
{
69+
if(temproot->leftchild!=nullptr)
70+
searchnode(x,temproot->leftchild);
71+
else
72+
return temproot;
73+
}
74+
else
75+
{
76+
if(temproot->val==x)//It returns the value when it is equal
77+
return temproot;
78+
}
79+
}
80+
81+
}
82+
void printtree(node* temp)
83+
{
84+
if(temp==nullptr)
85+
return;
86+
else
87+
{
88+
printtree(temp->leftchild);
89+
cout<<temp->val;
90+
printtree(temp->rightchild);
91+
}
92+
}//This is the function to print the tree. It prints the tree in a sorted order.
93+
node* giveroot()
94+
{
95+
return root;
96+
}//This function returns the pointer to root
97+
void inorder_traversal(node* temp)
98+
{
99+
if(temp==nullptr)
100+
return ;
101+
else
102+
{
103+
inorder_traversal(temp->leftchild);
104+
cout<<temp->val<<endl;
105+
inorder_traversal(temp->rightchild);
106+
}
107+
}
108+
109+
};
110+
int main()
111+
{
112+
int n;
113+
cin>>n;
114+
BinaryTree bst;
115+
for(int i=0;i<n;i++)
116+
{
117+
int temp;
118+
cin>>temp;
119+
bst.insert(temp);
120+
}
121+
node* temp=bst.giveroot();
122+
123+
// bst.printtree(temp);
124+
//temp = bst.giveroot();
125+
bst.inorder_traversal(temp);
126+
127+
}
128+
129+
130+
131+
132+
133+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include<iostream>
2+
#include<vector>
3+
using namespace std;
4+
#define nullptr 0
5+
typedef struct elem
6+
{
7+
int val;
8+
struct elem* leftchild;
9+
struct elem* rightchild;
10+
struct elem* parent;
11+
}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.
12+
class BinaryTree
13+
{
14+
private:
15+
int n=0;
16+
node* root=nullptr;
17+
public:
18+
void insert(int x)//Insert function first searches for the place where the node can be inserted and then it inserts it there.
19+
{
20+
21+
if(n==0)
22+
{
23+
root=new node;
24+
root->val=x;
25+
root->leftchild=nullptr;
26+
root->rightchild=nullptr;
27+
root->parent=nullptr;
28+
}
29+
else
30+
{
31+
node* temp=root;
32+
node* newnode=searchnode(x,temp);
33+
if(x>newnode->val)
34+
{
35+
newnode->rightchild=new node;
36+
newnode->rightchild->val=x;
37+
newnode->rightchild->parent=newnode;
38+
newnode->rightchild->leftchild=nullptr;
39+
newnode->rightchild->rightchild=nullptr;
40+
}
41+
else
42+
{
43+
newnode->leftchild=new node;
44+
newnode->leftchild->val=x;
45+
newnode->leftchild->parent=newnode;
46+
newnode->leftchild->leftchild=nullptr;
47+
newnode->leftchild->rightchild=nullptr;
48+
}
49+
50+
}
51+
n++;
52+
53+
}
54+
55+
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.
56+
{
57+
58+
if(temproot->val<x)//If value is smaller than it goes to the right child
59+
{
60+
if(temproot->rightchild!=nullptr)
61+
searchnode(x,temproot->rightchild);
62+
else
63+
return temproot;
64+
}
65+
else
66+
{
67+
if(temproot->val>x)//If value if larger it goes to the left child
68+
{
69+
if(temproot->leftchild!=nullptr)
70+
searchnode(x,temproot->leftchild);
71+
else
72+
return temproot;
73+
}
74+
else
75+
{
76+
if(temproot->val==x)//It returns the value when it is equal
77+
return temproot;
78+
}
79+
}
80+
81+
}
82+
void printtree(node* temp)
83+
{
84+
if(temp==nullptr)
85+
return;
86+
else
87+
{
88+
printtree(temp->leftchild);
89+
cout<<temp->val;
90+
printtree(temp->rightchild);
91+
}
92+
}//This is the function to print the tree. It prints the tree in a sorted order.
93+
node* giveroot()
94+
{
95+
return root;
96+
}//This function returns the pointer to root
97+
98+
void postorder_traversal(node* temp)
99+
{
100+
if(temp==nullptr)
101+
return ;
102+
else
103+
{
104+
postorder_traversal(temp->leftchild);
105+
postorder_traversal(temp->rightchild);
106+
cout<<temp->val<<endl;
107+
}
108+
}
109+
110+
111+
};
112+
int main()
113+
{
114+
int n;
115+
cin>>n;
116+
BinaryTree bst;
117+
for(int i=0;i<n;i++)
118+
{
119+
int temp;
120+
cin>>temp;
121+
bst.insert(temp);
122+
}
123+
node* temp=bst.giveroot();
124+
125+
// bst.printtree(temp);
126+
//temp = bst.giveroot();
127+
//bst.inorder_traversal(temp);
128+
bst.preorder_traversal(temp);
129+
}
130+
131+
132+
133+
134+
135+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Postorder Traversal
2+
3+
![alt text](https://www.java2blog.com/wp-content/uploads/2014/07/PostOrderTraversalBinaryTree-1.jpg)
4+
5+
Algorithm Postorder(tree)
6+
* Traverse the left subtree, i.e., call Postorder(left-subtree)
7+
* Traverse the right subtree, i.e., call Postorder(right-subtree)
8+
* Visit the root.

0 commit comments

Comments
 (0)