-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_binary_tree.c
More file actions
130 lines (100 loc) · 2.22 KB
/
Copy pathsimple_binary_tree.c
File metadata and controls
130 lines (100 loc) · 2.22 KB
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
128
129
/** BY @OscarScrooge*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "treechart.h"
NODE *newNode();
NODE *addNode(NODE* root);
void preorder(NODE *root);
void inorder(NODE *root);
void posorder(NODE *root);
int heigh(NODE *root);
int main() {
NODE *root=NULL;
int i =0;
while(i<2){
root = addNode(root);
i++;
}
printf("\nTree heigh: ");
printf("%d ",heigh(root));
printf("\nPreorder: ");
preorder(root);
printf("\nPosorder: ");
posorder(root);
printf("\nInorder: ");
inorder(root);
chart(root);
return 0;
}
NODE *addNode(NODE* root){
int resp;
if(root==NULL){
printf("Add a root \n");
root = newNode();
}else{
printf("Add left node? 1 YES -0 NO: ");
scanf("%d",&resp);
if(resp==1){
root->leftChild=newNode();
root->leftChild = addNode(root->leftChild);
} else{
root->leftChild=NULL;
}
printf("Add right node? 1 YES -0 NO: ");
scanf("%d",&resp);
if(resp==1){
root->rightChild=newNode();
root->rightChild = addNode(root->rightChild);
} else{
root->rightChild=NULL;
}
}
return root;
}
NODE *newNode(){
NODE *newNode = NULL;
newNode = malloc(sizeof(NODE));
printf("ID: ");
scanf("%d",&newNode->id);
newNode->leftChild=NULL;
newNode->rightChild=NULL;
return newNode;
}
void preorder(NODE *root){
if(root!=NULL){
printf("%d ",root->id);
preorder(root->leftChild);
preorder(root->rightChild);
}
}
void inorder(NODE *root){
if(root!=NULL){
inorder(root->leftChild);
printf("%d ",root->id);
inorder(root->rightChild);
}
}
void posorder(NODE *root){
if(root!=NULL){
posorder(root->leftChild);
posorder(root->rightChild);
printf("%d ",root->id);
}
}
int heigh(NODE *root){
int h=0;
if(root!=NULL){
int hi=0,hd=0;
hi++;
hi+=heigh(root->leftChild);
hd++;
hd+=heigh(root->rightChild);
if(hi>hd){
h = hi;
}else{
h= hd;
}
}
return h;
}