-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.cpp
99 lines (85 loc) · 1.93 KB
/
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<bits/stdc++.h>
// Tree concepts and algorithms
using namespace std;
// create a tree
class tree{
public:
int val;
tree* left = NULL;
tree* right = NULL;
tree(int data){ val = data; left = NULL; right = NULL;}
};
//inorder traversal
void inorder(tree* root){
if(root == NULL) return;
inorder(root->left);
cout<<root->val<<" ";
inorder(root->right);
}
// pre-order traversal
void preorder(tree* root){
if(root == NULL) return;
cout<<root->val<<" ";
inorder(root->left);
inorder(root->right);
}
// post-order traversal
void postorder(tree* root){
if(root == NULL) return;
inorder(root->left);
inorder(root->right);
cout<<root->val<<" ";
}
int height(tree* root){
if(root == NULL) return 0;
return 1 + max(height(root->left), height(root->right));
}
void printKthLevel(tree* root, int k){
if(root == NULL){
return;
}
if(k == 1){
cout<<root->val<<" ";
return;
}
printKthLevel(root->left, k-1);
printKthLevel(root->right, k-1);
return;
}
void printAllLevels(tree* root){
if(root == NULL) return;
int h = height(root);
for(int i = 0; i < h; i++){
printKthLevel(root, i+1);
cout<<endl;
}
}
int main(){
tree* root = new tree(1);
tree* left = new tree(2);
tree* right = new tree(3);
tree* left1 = new tree(4);
tree* right1 = new tree(5);
root->left = left;
root->right = right;
left->left = left1;
left->right = right1;
tree* left2 = new tree(6);
tree* right2 = new tree(7);
right->right = right2;
right->left = left2;
//preorder(root);
/*cout<<"Inorder traversal: ";
inorder(root);
cout<<endl;
cout<<"Preorder traversal: ";
preorder(root);
cout<<"\n";
cout<<"Postorder traversal: ";
postorder(root);
cout<<"\n";*/
//cout<<endl<<height(root)<<endl;
printAllLevels(root);
cout<<endl;
return 0;
}