-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_tree.h
67 lines (55 loc) · 1.42 KB
/
binary_tree.h
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
#include<iostream>
#include<string.h>
using namespace std;
class binary_tree{
struct node{
int value;
struct node * left ;
struct node * right;
};
struct node * root;
struct node * newnode(int value){
struct node * temp = new struct node;
temp->value = value;
temp->left = temp->right = NULL;
return temp;
}
int nodedepth(struct node * node){
if (node == NULL) return 0;
else {
int ldepth = nodedepth(node->left) + 1;
int rdepth = nodedepth(node->right) + 1;
if (ldepth > rdepth) return ldepth;
else return rdepth;
}
}
int nodediameter(struct node * node){
int crnt_diameter = 0;
crnt_diameter = nodedepth(node->left) + nodedepth(node->right) + 1;
if (crnt_diameter < nodediameter(node->left)) return nodediameter(node->left);
else if (crnt_diameter < nodediameter(node->right)) return nodediameter(node->right);
else return crnt_diameter;
}
public:
void addroot(int value){
root = newnode(value);
}
struct node * addnode(int value,char path[]){
struct node * temp ;
struct node * p;
temp = newnode(value);
p = root;
for (int i=0;i<strlen(path);++i){
if (path[i] == 'L' &&p != NULL) p = p->left;
else if (path[i] == 'R' &&p != NULL) p = p->right;
else cout << "Invalid Input" << endl ;
}
p = temp;
}
int maxdepth(){
return nodedepth(root);
}
int maxdiameter(){
return nodediameter(root);
}
};