summaryrefslogtreecommitdiff
path: root/Sources/DataStructure.cpp
blob: e9332c438af1ba88480d30c5b97d8065163c2294 (plain)
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
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
//#include <gtest/gtest.h>

using std::vector;
using std::ios_base;
using std::cin;
using std::cout;
using std::endl;

class TreeOrders {
public:
	int n;
	vector<long> key;
	vector<int> left;
	vector<int> right;

	void read() {
		cin >> n;
		key.resize(n);
		left.resize(n);
		right.resize(n);
		for (int i = 0; i < n; i++) {
			cin >> key[i] >> left[i] >> right[i];
		}
	}

	bool is_Bst() {
		if (key.size()==0) return true;
		std::stack<int> keyIndexStack;
		vector<long> result;
		bool walkLeft = true;
		int currentIndex = 0;
		keyIndexStack.push(currentIndex);
		while (!keyIndexStack.empty() || right[currentIndex] != -1) {
			if (walkLeft) {
				if (left[currentIndex] != -1) {
                    if (key[left[currentIndex]] >= key[currentIndex]) return false;
					currentIndex = left[currentIndex];
					keyIndexStack.push(currentIndex);
				} else {
                    int i = keyIndexStack.top();
                    keyIndexStack.pop();
                    if (result.size() > 0) {
                        if (key[i] < result[(result.size()-1)]) return false;
                    }
                    result.push_back(key[i]);
                    walkLeft = false;
				}
			} else {
				if (right[currentIndex] != -1) {
                    if (key[right[currentIndex]] < key[currentIndex]) return false;
					currentIndex = right[currentIndex];
					keyIndexStack.push(currentIndex);
					walkLeft = true;
				} else {
					if (!keyIndexStack.empty()) {
						currentIndex = keyIndexStack.top();
						keyIndexStack.pop();
                        if (result.size() > 0) {
                            if (key[currentIndex] <= result[(result.size()-1)]) return false;
                        }
						result.push_back(key[currentIndex]);
					}
				}
			}
		}
		return true;
	}

};

void print(vector<int> a) {
	for (size_t i = 0; i < a.size(); i++) {
		if (i > 0) {
			cout << ' ';
		}
		cout << a[i];
	}
	cout << '\n';
}

//TEST(TreeTraversal, t) {
//	TreeOrders t;
//	t.key = {4, 2, 5, 1, 3};
//	t.left = {1, 3, -1, -1, -1};
//	t.right = {2, 4, -1, -1, -1};
//
//	vector<int> results = t.in_order();
//	ASSERT_EQ(5, results.size());
//	ASSERT_EQ(1, results[0]);
//	ASSERT_EQ(2, results[1]);
//	ASSERT_EQ(3, results[2]);
//	ASSERT_EQ(4, results[3]);
//	ASSERT_EQ(5, results[4]);
//
//	results = t.pre_order();
//	ASSERT_EQ(5, results.size());
//	ASSERT_EQ(4, results[0]);
//	ASSERT_EQ(2, results[1]);
//	ASSERT_EQ(1, results[2]);
//	ASSERT_EQ(3, results[3]);
//	ASSERT_EQ(5, results[4]);
//
//	results = t.post_order();
//	ASSERT_EQ(5, results.size());
//	ASSERT_EQ(1, results[0]);
//	ASSERT_EQ(3, results[1]);
//	ASSERT_EQ(2, results[2]);
//	ASSERT_EQ(5, results[3]);
//	ASSERT_EQ(4, results[4]);
//}

int main_with_large_stack_space() {
	ios_base::sync_with_stdio(0);
	TreeOrders t;
	t.read();
	if (t.is_Bst())
		cout << "CORRECT" << endl;
	else
		cout << "INCORRECT" << endl;
	return 0;
}

int main (int argc, char **argv)
{
  return main_with_large_stack_space();
}