forked from fineanmol/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_search_tree.c
72 lines (66 loc) · 1.19 KB
/
Binary_search_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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *creatNode(int data)
{
struct node *n;
n = (struct node *)malloc(sizeof(struct node));
n->data = data;
n->left = NULL;
n->right = NULL;
return n;
}
void InorderTraversal(struct node *root)
{
if (root != NULL)
{
InorderTraversal(root->left);
printf("%d\t", root->data);
InorderTraversal(root->right);
}
}
int isBST(struct node *root)
{
static struct node *prev = NULL;
if (root != NULL)
{
if (!isBST(root->left))
{
return 0;
}
prev = root;
return isBST(root->right);
}
else
{
return 1;
}
}
int main()
{
struct node *p = creatNode(5);
struct node *p1 = creatNode(3);
struct node *p2 = creatNode(6);
struct node *p3 = creatNode(1);
struct node *p4 = creatNode(4);
p->left = p1;
p->right = p2;
p1->left = p3;
p1->right = p4;
InorderTraversal(p);
printf("\n");
if (isBST(p))
{
printf("this is BST");
}
else
{
printf("this is not BST");
}
return 0;
}