forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree.cpp
80 lines (66 loc) · 2.13 KB
/
binary_tree.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
//Constructing a binary tree using structure and implementing various operations
//OPERATIONS: preorder, postorder, inorder traversal by creating structures
#include<iostream>
#include<iomanip>
using namespace std;
struct binaryTree{
int val;
struct binaryTree* l; //left branch
struct binaryTree* r; //right branch
};
struct binaryTree* createtree(int value){
struct binaryTree* n= new struct binaryTree[1];
n->l=NULL;
n->r=NULL;
n->val= value;
return n;
}
void preorder(struct binaryTree* n){ // root -> left -> right
if(n!=NULL){
cout<<n->val<<" "; //printing root element
preorder(n->l); //moving to left branch
preorder(n->r); //moving to right after transversing left branch
}
}
void postorder(struct binaryTree* n){ // left -> right -> root
if(n!=NULL){
postorder(n->l); //moving to left branch
postorder(n->r); //moving to right after transversing left branch
cout<<n->val<<" "; //printing root element
}
}
void inorder(struct binaryTree* n){ // left -> root -> right
if(n!=NULL){
inorder(n->l); //moving to left branch
cout<<n->val<<" "; //printing root element
inorder(n->r); //moving to right after transversing left branch and root branch
}
}
int main(){
//EXAMPLE
//constructing a binaryTree structure using createtree function
struct binaryTree* p = createtree(4);
struct binaryTree* p1 = createtree(2);
struct binaryTree* p2 = createtree(8);
struct binaryTree* p3 = createtree(5);
struct binaryTree* p4 = createtree(6); //creating the branches of binary tree
// BINARY TREE LOOKS LIKE:
// 4
// / \
// 2 8
// / \
// 5 6
p->l = p1; //linking the branches of binary tree
p->r = p2;
p1->l = p3;
p1->r = p4;
cout<<"PreOrder Traversal: ";
preorder(p); //preorder traversal
cout<<endl;
cout<<"PostOrder Traversal: ";
postorder(p); //postorder traversal
cout<<endl;
cout<<"InOrder Traversal: ";
inorder(p); //inorder traversal
return 0;
}