-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path110-old_binary_tree_is_bst.c
121 lines (113 loc) · 3.03 KB
/
110-old_binary_tree_is_bst.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "binary_trees.h"
#include "limits.h"
int is_bst_helper(const binary_tree_t *tree, int *largest);
#define VERBOSE 0
/**
* binary_tree_is_bst - checks if a binary tree is a valid Binary Search Tree.
* @tree: A pointer to the root node of the tree to check.
*
* Return: 1 if tree is a valid BST, and 0 otherwise
*/
int binary_tree_is_bst(const binary_tree_t *tree)
{
int tracker = INT_MIN;
if (tree == NULL)
return (0);
return (is_bst_helper(tree, &tracker));
}
/**
* is_left - checks if a node is a left child of some other node
* @node: A pointer to the node to be checked.
* Return: 1 if is a left child, otherwise 0
*/
int is_left(const binary_tree_t *node)
{
if (node && node->parent)
return (node->parent->left == node);
return (0);
}
/**
* is_right - checks if a node is a right child of some other node
* @node: A pointer to the node to be checked.
* Return: 1 if is a right child, otherwise 0
*/
int is_right(const binary_tree_t *node)
{
if (node && node->parent)
return (node->parent->right == node);
return (0);
}
#if !VERBOSE
/**
* is_bst_helper - checks if a binary tree is a valid Binary Search Tree.
* @tree: A pointer to the root node of the tree to check.
* @largest: Value of largest node visited so far.
*
* Return: 1 if tree is a valid BST, and 0 otherwise
*/
int is_bst_helper(const binary_tree_t *tree, int *largest)
{
int ret = 1;
if (tree != NULL)
{
ret *= is_bst_helper(tree->left, largest);
if (tree->n < *largest)
return (0);
*largest = tree->n;
if (is_left(tree) && !(tree->n < tree->parent->n))
return (0);
if (is_right(tree) && !(tree->n > tree->parent->n))
return (0);
ret *= is_bst_helper(tree->right, largest);
}
return (ret);
}
#else
/**
* is_bst_helper - checks if a binary tree is a valid Binary Search Tree.
* @tree: A pointer to the root node of the tree to check.
* @largest: Value of largest node visited so far.
*
* Return: 1 if tree is a valid BST, and 0 otherwise
*/
int is_bst_helper(const binary_tree_t *tree, int *largest)
{
int ret = 1;
if (tree)
{
printf("Moving to %d\n", tree->n);
ret *= is_bst_helper(tree->left, largest);
printf("done with left tree for %d: %d\n", tree->n, ret);
printf("largest = %d\n", *largest);
if (tree->n < *largest)
return (0);
*largest = tree->n;
printf("largest = %d\n", *largest);
if (is_left(tree))
printf("%d is %s than %d\n",
tree->n,
(tree->n < tree->parent->n) ? "smaller" : "larger",
tree->parent->n);
if (is_left(tree) && !(tree->n < tree->parent->n))
{
printf("%d is left child\n", tree->n);
return (0);
}
if (is_right(tree))
printf("%d is %s than %d\n",
tree->n,
(tree->n < tree->parent->n) ? "smaller" : "larger",
tree->parent->n);
if (is_right(tree) && !(tree->n > tree->parent->n))
{
printf("%d is right child\n", tree->n);
return (0);
}
ret *= is_bst_helper(tree->right, largest);
printf("done with right tree for %d: %d\n", tree->n, ret);
if (tree->parent)
printf("Moving back to %d\n", tree->parent->n);
}
return (ret);
}
#endif /* VERBOSE */