-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.cpp
89 lines (86 loc) · 1.45 KB
/
make.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
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct node {
int data;
struct node *left;
struct node *right;
}node;
void create(node ** root,int n)
{
node *temp;
if(*(root) == NULL) {
temp = (node *)malloc(sizeof(node));
temp->data = n;
temp->left = NULL;
temp->right = NULL;
(*root) = temp;
}else {
if(n <= (*root)->data) {
cout << "root->data is " << (*root)->data << endl;
create(&((*root)->left),n);
}else if(n > (*root)->data) {
cout << "root->data is " << (*root)->data << endl;
create(&((*root)->right),n);
}
}
}
void inorder(node *root)
{
if(root == NULL) {
return;
}else {
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}
int height(node *root)
{
if(root == NULL) {
return 0;
}else {
int l = height(root->left);
int r = height(root->right);
if(l > r) {
return l+1;
}else {
return r+1;
}
}
}
void level1(node *root,int level)
{
if(root == NULL) {
return;
}else if(level == 1) {
cout << root->data << " ";
}else if(level > 1) {
level1(root->left,level-1);
level1(root->right,level-1);
}
}
void levelwise(node *root,int height)
{
for(int i = 1;i <= height;i++) {
level1(root,i);
cout << endl;
}
}
int main()
{
node *root = NULL;
while(1) {
int n;
cin >> n;
if(n == 999) {
break;
}else {
create(&root,n);
}
}
inorder(root);
cout << "height of tree is " << height(root) << endl;
levelwise(root,height(root));
return 0;
}