public int[] ToArray() { int[] newArr = new int[Count]; NodeBT current = Root; newArr[0] = Root.Data; int i = 1; do { if (current.Data != Root.Data) { newArr[i] = current.Data; } i++; if (current.Left != null) { current = current.Left; } else if (current.Right != null) { current = current.Right; } else if (current.Left == null && current.Right == null) { current = Root; } }while (i < Count); return(newArr); }
public void Add(int data) { if (Root == null) { NodeBT newNode = new NodeBT(data); Root = newNode; Count++; return; } NodeBT currentNode = Root; bool addData = false; do { if (data < currentNode.Data) { if (currentNode.Left == null) { NodeBT newNode = new NodeBT(data); currentNode.Left = newNode; addData = true; Count++; } else { currentNode = currentNode.Left; } } if (data > currentNode.Data) { if (currentNode.Right == null) { NodeBT newNode = new NodeBT(data); currentNode.Right = newNode; addData = true; Count++; } else { currentNode = currentNode.Right; } } }while (!addData); }
public bool Contains(int data) { NodeBT currentNode = Root; bool containsData = false; do { if (currentNode.Data == data) { containsData = true; return(containsData); } if (data < currentNode.Data) { currentNode = currentNode.Left; } if (data > currentNode.Data) { currentNode = currentNode.Right; } }while (!containsData); return(containsData); }
public BinaryTree(int data) { Root = new NodeBT(data); }
public NodeBT(int data) { Data = data; Left = null; Right = null; }