-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.cs
164 lines (139 loc) · 4.36 KB
/
BinaryTree.cs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections;
using System.Collections.Generic;
namespace BinaryTrees
{
public class BinaryTree<T> : IEnumerable<T> where T : IComparable
{
private BinaryTree<T> _head;
private BinaryTree<T> _left;
private BinaryTree<T> _right;
private T _value;
private int _weight;
private BinaryTree(T key)
{
_value = key;
_weight = 1;
}
public BinaryTree()
{
}
public void Add(T key)
{
if (_weight == 0)
{
_weight++;
_value = key;
return;
}
_weight++;
if (key.CompareTo(_value) < 0)
{
if (_left == null)
{
_left = new BinaryTree<T>(key)
{
_head = this
};
return;
}
_left.Add(key);
return;
}
if (_right == null)
{
_right = new BinaryTree<T>(key)
{
_head = this
};
return;
}
_right.Add(key);
}
public bool Contains(T key)
{
if (_weight == 0)
return false;
var stack = new Stack<BinaryTree<T>>();
stack.Push(this);
while (stack.Count != 0)
{
var current = stack.Pop();
if (current._value.CompareTo(key) == 0)
return true;
if (current._left != null)
stack.Push(current._left);
if (current._right != null)
stack.Push(current._right);
}
return false;
}
private static int GetIndexByTree(BinaryTree<T> requiredTree)
{
// если искомое дерево это корень
if (requiredTree._head == null)
{
return requiredTree._left?._weight ?? 0;
}
// если искомое дерево является левым для родительского
if (requiredTree == requiredTree._head._left)
{
return requiredTree._left == null
? GetIndexByTree(requiredTree._head) - requiredTree._weight
: GetIndexByTree(requiredTree._head) - requiredTree._weight + requiredTree._left._weight;
}
// если искомое дерево является правым для родительского
return requiredTree._left == null
? GetIndexByTree(requiredTree._head) + 1
: GetIndexByTree(requiredTree._head) + requiredTree._left._weight + 1;
}
private BinaryTree<T> GetTreeByIndex(int index)
{
var currentTree = this;
var currentIndex = GetIndexByTree(currentTree);
while (true)
{
if (index == currentIndex)
return currentTree;
if (index < currentIndex)
{
currentTree = currentTree._left;
currentIndex = GetIndexByTree(currentTree);
}
if (index > currentIndex)
{
currentTree = currentTree._right;
currentIndex = GetIndexByTree(currentTree);
}
}
}
public T this[int index]
{
get
{
var result = GetTreeByIndex(index);
return result != null ? result._value : throw new IndexOutOfRangeException();
}
}
public IEnumerator<T> GetEnumerator()
{
if (_weight == 0)
yield break;
if (_left != null)
{
foreach (var item in _left)
yield return item;
}
yield return _value;
if (_right != null)
{
foreach (var item in _right)
yield return item;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}