-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.c
81 lines (72 loc) · 1.59 KB
/
tree.c
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
#include<stdlib.h>
#include<stdio.h>
struct tree_el
{
int val;
struct tree_el * right, * left;
};
typedef struct tree_el node;
void insert(node ** tree, node * item)
{
if(!(*tree)) {
*tree = item;
return;
}
if(item->val<(*tree)->val)
insert(&(*tree)->left, item);
else if(item->val>(*tree)->val)
insert(&(*tree)->right, item);
}
void printout(node * tree)
{
if(tree->left) printout(tree->left);
printf("%d\n",tree->val);
if(tree->right) printout(tree->right);
}
void printout_preorder(node * tree)
{
printf("%d\n",tree->val);
if(tree->left) printout(tree->left);
if(tree->right) printout(tree->right);
}
int getmax(node *tree)
{
if(tree->right) return getmax(tree->right);
return tree->val;
}
int getmin(node *tree)
{
if(tree->left) return getmin(tree->left);
return tree->val;
}
int common_ancestor(node *root,int v1, int v2)
{
int val=root->val;
printf("Entered with val=%d v1=%d v2=%d\n",val,v1,v2);
if(v1 > val && v2 > val)
return common_ancestor(root->right,v1,v2);
if(v1 < val && v2 < val)
return common_ancestor(root->left,v1,v2);
return val;
}
main()
{
node * curr, * root;
int i;
int a[]={10,4,3,7,30,1,8,2,200,17};
root = NULL;
for(i=0;i<10;i++) {
curr = (node *)malloc(sizeof(node));
curr->left = curr->right = NULL;
curr->val = a[i];
insert(&root, curr);
}
printout(root);
printout_preorder(root);
printf("Max=%d\n",getmax(root));
printf("Min=%d\n",getmin(root));
printf("CA=%d\n",common_ancestor(root,3,7));
printf("CA=%d\n",common_ancestor(root,3,1));
printf("CA=%d\n",common_ancestor(root,3,9));
printf("CA=%d\n",common_ancestor(root,2,200));
}