public BinaryTree() { rootNode = null; leaveNode = null; }
public BstBuilder() { rootNode = null; parentNode = null; }
public BinaryNode(T data, BinaryNode <T> left, BinaryNode <T> right) { this.data = data; this.left = left; this.right = right; }
public BinaryNode(T data) { this.data = data; lChild = null; rChild = null; }
public bool Remove(T value) { var current = Root; BinaryNode <T> parent = null; while (current != null) { //Found it if (current.Value.CompareTo(value) == 0) { // No left or right child if (current.LeftChild == null && current.RightChild == null) { //are we at root if (parent == null) { Root = null; } else { if (parent > current) { parent.LeftChild = null; } else { parent.RightChild = null; } } } // Has Left but no right else if (current.LeftChild != null && current.RightChild == null) { if (parent == null) { Root = current.LeftChild; } else { if (parent > current) { parent.LeftChild = current.LeftChild; } else { parent.RightChild = current.RightChild; } } } // Has Right but that right has no left else if (current.RightChild != null && current.RightChild.LeftChild == null) { if (parent == null) { Root = current.RightChild; } else { if (parent > current) { parent.LeftChild = current.RightChild; } else { parent.RightChild = current.RightChild; } } } // Has Right and that right has left child --Count; return(true); } //Otherwise parent = current; if (current.Value.CompareTo(value) < 0) { current = current.RightChild; } else if (current.Value.CompareTo(value) > 0) { current = current.LeftChild; } } return(false); }
public BinaryNodeTree() { root = null; }
public void Insert(T value) { var binaryNode = new BinaryNode <T>(value); Insert(binaryNode); }
public virtual void Clear() { root = null; }
void Remove(BinaryNode <T> currentNode, BinaryNode <T> parentNode) { if (currentNode == null) { return; } if (currentNode.Right != null) { var leftMostNode = FindLeftMostNode(currentNode.Right); var leftMostNodeParent = GetParentNode(leftMostNode); currentNode.Value = leftMostNode.Value; if (leftMostNodeParent != null && leftMostNode.Right == null) { if (leftMostNodeParent.Equals(currentNode)) { leftMostNodeParent.Right = null; } else { leftMostNodeParent.Left = null; } } else { Remove(leftMostNode, leftMostNodeParent); } } else if (currentNode.Left != null) { var leftChildValue = currentNode.Left.Value; Remove(currentNode.Left, currentNode); currentNode.Value = leftChildValue; } else { if (currentNode.Equals(Root)) { Root = null; return; } var compareInt = currentNode.Value.CompareTo(parentNode.Value); switch (compareInt) { case (0): case (-1): parentNode.Left = null; return; case (1): parentNode.Right = null; return; default: return; } } }
BinaryNode <T> GetParentNode(BinaryNode <T> nodeToFind, BinaryNode <T> currentNode, BinaryNode <T> parentNode) { if (currentNode == null) { return(null); } else if (currentNode.Equals(nodeToFind)) { return(parentNode); } var intComparison = nodeToFind.Value.CompareTo(currentNode.Value); switch (intComparison) { case (0): case (-1): return(GetParentNode(nodeToFind, currentNode.Left, currentNode)); case (1): return(GetParentNode(nodeToFind, currentNode.Right, currentNode)); default: return(null); } }