public int CalcSize(Link node, int size) { size++; if (node.Left != null) { CalcSize(node.Left, size); } if (node.Right != null) { CalcSize(node.Right, size); } return size; }
public void Add(int val) { if (rootNode == null) { rootNode = new Link(); } else { Link newNode = Search(rootNode, val); if (val < newNode.Value) { newNode.Left = new Link(val, null, null); } else if (val > newNode.Value) { newNode.Right = new Link(val, null, null); } } }
public int FindHeight(Link node, int count) { if (node == null) return 0; int heightLeft = FindHeight(node.Left, count); int heightRight = FindHeight(node.Right, count); if (heightLeft > heightRight) count = heightLeft + 1; else count = heightRight + 1; return count; }
public void Clear() { rootNode = new Link(); }
public Node(int val, Link left, Link right) { Value = val; Left = left; Right = right; }
public void SortElements(Link node, List<int> arr) { if (node.Left != null) { SortElements(node.Left, arr); arr.Add(node.Value); } else { arr.Add(node.Value); } if (node.Right != null) { SortElements(node.Right, arr); } }
public Link Search(Link node, int val) { if (rootNode == null) { node = new Link(val, null, null); rootNode = node; } if (node.Value != val) { if (val < node.Value && node.Left != null) { node = Search(node.Left, val); } else { if (val > node.Value && node.Right != null) node = Search(node.Right, val); } } return node; }
public void Reverse(Link node) { if (node != null) { Link tempNode = node.Left; node.Left = node.Right; node.Right = tempNode; if (node.Left != null) { Reverse(node.Left); } if (node.Right != null) { Reverse(node.Right); } } }
public void Init(int[] arr) { if (arr.Length > 0) { rootNode = new Link(arr[0], null, null); for (int i = 1; i < arr.Length; i++) { Add(arr[i]); } } }
public int FindWidth(Link node, int level) { if (node == null) return 0; if (level == 1) return 1; return FindWidth(node.Left, level - 1) + FindWidth(node.Right, level - 1); }
public Link FindParent(Link node, Link currentNode) { if (currentNode.Left != null && node != currentNode.Left && node.Value < currentNode.Value) { currentNode = FindParent(node, currentNode.Left); } if (currentNode.Right != null && node != currentNode.Right && node.Value > currentNode.Value) { currentNode = FindParent(node, currentNode.Right); } return currentNode; }
public int FindLeaves(Link node, int count) { if (node.Left == null && node.Right == null) { count++; } if (node.Left != null) { FindLeaves(node.Left, count); } if (node.Right != null) { FindLeaves(node.Right, count); } return count; }