-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArrayBinaryTree.cs
More file actions
66 lines (52 loc) · 1.6 KB
/
ArrayBinaryTree.cs
File metadata and controls
66 lines (52 loc) · 1.6 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
using System;
namespace AlgorithmsAndDataStructures.DataStructures.BinaryTrees;
public class ArrayBinaryTree<T>
{
private T[] tree;
public ArrayBinaryTree(int initialCapacity = 8)
{
tree = new T[initialCapacity + 1];
}
public void Root(T value)
{
tree[1] = value;
}
public void SetLeft(T parent, T value)
{
for (var i = 0; i < tree.Length; i++)
{
#pragma warning disable HAA0601 // Value type to reference type conversion causing boxing allocation
if (tree[i].Equals(parent))
#pragma warning restore HAA0601 // Value type to reference type conversion causing boxing allocation
{
var leftIndex = i * 2;
if (leftIndex >= tree.Length) ExpandTree();
tree[leftIndex] = value;
}
}
}
public void SetRight(T parent, T value)
{
for (var i = 0; i < tree.Length; i++)
{
#pragma warning disable HAA0601 // Value type to reference type conversion causing boxing allocation
if (tree[i].Equals(parent))
#pragma warning restore HAA0601 // Value type to reference type conversion causing boxing allocation
{
var rightIndex = i * 2 + 1;
if (rightIndex >= tree.Length) ExpandTree();
tree[rightIndex] = value;
}
}
}
private void ExpandTree()
{
var expandedTree = new T[tree.Length * 2];
Array.Copy(tree, 0, expandedTree, 0, tree.Length);
tree = expandedTree;
}
public T[] GetTree()
{
return tree;
}
}