-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.c
More file actions
78 lines (72 loc) · 2 KB
/
tree.c
File metadata and controls
78 lines (72 loc) · 2 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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left;
struct node *right;
};
void preorder(struct node *root){
if(root==NULL) return;
printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
}
void postorder(struct node *root){
if(root==NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ",root->data);
}
void inorder(struct node *root){
if(root==NULL) return;
inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
}
struct node *creatnode(int value){
struct node *newnode=malloc(sizeof(struct node));
newnode->data=value;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
struct node *inserleft(struct node *root,int value){
root->left=creatnode(value);
return root->left;
}
struct node *inserright(struct node *root,int value){
root->right=creatnode(value);
return root->right;
}
int tree_sum(struct node *root){
if(root==NULL) return 0;
return tree_sum(root->left)+tree_sum(root->right)+root->data;
}
int tree_height(struct node *root){
if(root == NULL) return 0;
int leftheight=tree_height(root->left);
int rightheight=tree_height(root->right);
return (leftheight>rightheight? leftheight: rightheight)+1;
}
int main(){
struct node *root=creatnode(1);
inserleft(root,7);
inserright(root,10);
inserleft(root->left,17);
inserright(root->left,5);
inserright(root->right,100);
inserleft(root->left->left,117);
inserright(root->right->right,1000);
printf("\ninorder traversal : \n");
inorder(root);
printf("\nreorder traversal : \n");
preorder(root);
printf("\npostorder traversal : \n");
postorder(root);
printf("\nsum of tree : ");
int sum=tree_sum(root);
printf("%d\n",sum);
printf("total height in tree : ");
int treeheight=tree_height(root);
printf("%d\n",treeheight);
}