using System using System Collections Generic using System Linq using

  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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Tree: IEnumerable
{
private int[] l;
private int[] r;
private int[] x;
private int head = -1;
private int root = -1;
public Tree(int capacity = 10)
{
this.Capacity = capacity;
Count = 0;
}
public int Capacity
{
get
{
return ((l == null)? 0 : l.Count());
}
private set
{
int size = Capacity;
Array.Resize(ref l, value);
Array.Resize(ref r, value);
Array.Resize(ref x, value);
for (int i = value - 1; i >= size; --i)
{
l[i] = r[i] = -1;
x[i] = head;
head = i;
}
}
}
public int Count
{
get;
private set;
}
private int getRoom()
{
if (head == -1) Capacity *= 2;
int freeplace = head;
head = x[head];
return freeplace;
}
public bool addVertex(int value)
{
if (Count == 0) //root = -1;
{
root = getRoom();
x[root] = value;
return true;
}
int cur = root;
while (true)
{
if (x[cur] == value)
{
return false;
}
if (x[cur] > value)
{
if (l[cur] != -1)
{
cur = l[cur];
}
else
{
l[cur] = getRoom();
x[l[cur]] = value;
return true;
}
}
else
{
if (r[cur] != -1)
{
cur = r[cur];
}
else
{
r[cur] = getRoom();
x[r[cur]] = value;
return true;
}
}
}
}
public void deleteVertex(int value)
{
int cur = root;
while (cur != -1)
{
}
}
public bool containsVertex(int val)
{
int cur = root;
while (cur != -1 && x[cur] != val)
{
cur = ((val < x[cur]) ? l[cur] : r[cur]);
}
return cur != -1;
}
public IEnumerator GetEnumerator()
{
if (root == -1) yield break;
Stack<int> s = new Stack<int>();
s.Push(root);
while (s.Count != 0)
{
int v = s.Peek();
if (l[v] == -1)
{
yield return x[v];
s.Pop();
if (r[v] != -1) s.Push(r[v]);
}
else //l[v] != -1
{
s.Push(l[v]);
}
}
}
}
class Program
{
static void Main(string[] args)
{
}
}
}