-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryTree.c
70 lines (57 loc) · 1.33 KB
/
binaryTree.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
#include <stdio.h>
#include <iostream>
typedef struct node
{
int data;
struct node *left;
struct node *right;
} node;
node *create()
{
node *p;
int x;
printf("Enter data(-1 for no data):");
scanf("%d",&x);
if(x==-1)
return NULL;
p=(node*)malloc(sizeof(node));
p->data=x;
printf("Enter left child of %d:\n",x);
p->left=create();
printf("Enter right child of %d:\n",x);
p->right=create();
return p;
}
void preorder(node *t) //address of root node is passed in t
{
if(t!=NULL)
{
printf("\n%d",t->data); //visit the root
preorder(t->left); //preorder traversal on left subtree
preorder(t->right); //preorder traversal om right subtree
}
}
int Height(node *root)
{
int leftHeight , rightHeight;
if(root == NULL)
return 0;
else{
leftHeight = Height(root -> left);
rightHeight = Height(root -> right);
if(leftHeight > rightHeight)
return (leftHeight+1);
else
return (rightHeight+1);
}
}
int main()
{
node *root;
root=create();
printf("\nThe preorder traversal of tree is:\n");
preorder(root);
printf("\nHeight of Binary tree is \n");
printf("%d\n",Height(root));
return 0;
}