-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeIsABST.cs
More file actions
114 lines (92 loc) · 4.08 KB
/
Copy pathBinaryTreeIsABST.cs
File metadata and controls
114 lines (92 loc) · 4.08 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
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
using DataStructures.Libraries.Trees;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms.Problem.Trees
{
[TestClass]
public class BinaryTreeIsABST
{
public class BinaryTreeChecker
{
public BinaryTreeNode BinaryTree { get; set; }
public double lowerBound { get; set; }
public double upperBound { get; set; }
public BinaryTreeChecker(BinaryTreeNode binaryTreeNode, double lowerBound, double upperBound)
{
this.lowerBound = lowerBound;
this.upperBound = upperBound;
this.BinaryTree = binaryTreeNode;
}
}
public bool IsBinaryTreeABSTRecursion(BinaryTreeNode root)
{
return this.IsBinaryTreeABSTRecursionHelper(new BinaryTreeChecker(root, int.MinValue, int.MaxValue));
}
public bool IsBinaryTreeABSTRecursionHelper(BinaryTreeChecker root)
{
if (root == null || root.BinaryTree == null) return true;
if (root != null
&& root.BinaryTree != null
&& (root.BinaryTree.Value < root.lowerBound || root.BinaryTree.Value > root.upperBound))
{
return false;
}
bool isLeftTree = this.IsBinaryTreeABSTRecursionHelper(new BinaryTreeChecker(root.BinaryTree.Left, root.lowerBound, root.BinaryTree.Value));
bool isRightTree = this.IsBinaryTreeABSTRecursionHelper(new BinaryTreeChecker(root.BinaryTree.Right, root.BinaryTree.Value, root.lowerBound));
return isLeftTree && isRightTree;
}
public bool IsBinaryTreeABST(BinaryTreeNode root)
{
Stack<BinaryTreeChecker> bstChecker = new Stack<BinaryTreeChecker>();
bstChecker.Push(new BinaryTreeChecker(root, int.MinValue, int.MaxValue));
while(bstChecker.Any())
{
BinaryTreeChecker rangeChecker = bstChecker.Pop();
if (rangeChecker.BinaryTree != null
&& (rangeChecker.BinaryTree.Value < rangeChecker.lowerBound || rangeChecker.BinaryTree.Value > rangeChecker.upperBound))
{
return false;
}
if (rangeChecker.BinaryTree != null && rangeChecker.BinaryTree.Right != null)
{
BinaryTreeChecker rightRangeNode = new BinaryTreeChecker(rangeChecker.BinaryTree.Right, rangeChecker.BinaryTree.Value, rangeChecker.upperBound);
bstChecker.Push(rightRangeNode);
}
if (rangeChecker.BinaryTree != null && rangeChecker.BinaryTree.Left != null)
{
BinaryTreeChecker leftRangeNode = new BinaryTreeChecker(rangeChecker.BinaryTree.Left, rangeChecker.lowerBound, rangeChecker.BinaryTree.Value);
bstChecker.Push(leftRangeNode);
}
}
return true;
}
[TestMethod]
public void TestIsBinaryTreeABST()
{
BinaryTreeNode root = new BinaryTreeNode(10);
root.Left = new BinaryTreeNode(0);
root.Right = new BinaryTreeNode(25);
root.Left.Left = new BinaryTreeNode(-1);
root.Left.Right = new BinaryTreeNode(9);
root.Right.Left = new BinaryTreeNode(16);
root.Right.Right = new BinaryTreeNode(32);
bool result = this.IsBinaryTreeABST(root);
}
[TestMethod]
public void TestIsBinaryTreeABSTUsingRecursion()
{
BinaryTreeNode root = new BinaryTreeNode(10);
root.Left = new BinaryTreeNode(0);
root.Right = new BinaryTreeNode(25);
root.Left.Left = new BinaryTreeNode(-1);
root.Left.Right = new BinaryTreeNode(9);
root.Right.Left = new BinaryTreeNode(16);
root.Right.Right = new BinaryTreeNode(32);
bool result = this.IsBinaryTreeABSTRecursion(root);
}
}
}