/// <summary> /// Check, if element is in the tree. /// </summary> /// <param name="keyToCheck"></param> /// <param name="nodeToCheck"></param> /// <returns></returns> private bool ExistsInTree(T keyToCheck, BSTreeNode nodeToCheck) { if (nodeToCheck == null) { return(false); } else { if (nodeToCheck.Key.CompareTo(keyToCheck) == 0) { return(true); } else { if (nodeToCheck.Key.CompareTo(keyToCheck) > 0) { return(ExistsInTree(keyToCheck, nodeToCheck.LeftChild)); } else { return(ExistsInTree(keyToCheck, nodeToCheck.RightChild)); } } } }
public BSTreeNode <T> MinimalNode(BSTreeNode <T> node) { while (node.Left != null) { node = node.Left; } return(node); }
private void Remove(BSTreeNode <T> node) { if (node == null) { throw new NullTreeNodeException("Node is null! Not aible to remove"); } var side = node.NodeSide; if (node.Left == null && node.Right == null) { if (side == Sides.Right) { node.Parent.Right = null; } else { node.Parent.Left = null; } } else if (node.Left == null) { if (side == Sides.Left) { node.Parent.Left = node.Right; } else { node.Parent.Right = node.Right; } node.Right.Parent = node.Parent; } else if (node.Right == null) { if (side == Sides.Right) { node.Parent.Right = node.Left; } else { node.Parent.Left = node.Left; } node.Left.Parent = node.Parent; } else { var minimalNode = MinimalNode(node.Right); node.Data = minimalNode.Data; Remove(minimalNode); } }
public IEnumerable <BSTreeNode <T> > preOrderTraversal(BSTreeNode <T> node) { if (node != null) { yield return(node); foreach (var item in preOrderTraversal(node.Left)) { yield return(item); } foreach (var item in preOrderTraversal(node.Right)) { yield return(item); } } }
private void Insert(BSTreeNode <T> node, BSTreeNode <T> curNode = null) { if (node == null) { throw new NullTreeNodeException(); } if (root == null) { node.Parent = null; root = node; } else { curNode = curNode ?? root; node.Parent = curNode; int comparisson = node.Data.CompareTo(curNode.Data); if (comparisson < 0) { if (curNode.Left == null) { curNode.Left = node; } else { Insert(node, curNode.Left); } } else if (comparisson == 0) { curNode.Data = node.Data; } else { if (curNode.Right == null) { curNode.Right = node; } else { Insert(node, curNode.Right); } } } }
/// <summary> /// Insert key to tree. /// </summary> /// <param name="keyToInsert"></param> public void Insert(T keyToInsert) { if (IsEmpty()) { root = new BSTreeNode(keyToInsert); } else { var nodeToCompare = root; while (true) { if (nodeToCompare.Key.CompareTo(keyToInsert) > 0) { if (nodeToCompare.LeftChild == null) { nodeToCompare.LeftChild = new BSTreeNode(keyToInsert); return; } nodeToCompare = nodeToCompare.LeftChild; } else { if (nodeToCompare.Key.CompareTo(keyToInsert) == 0) { return; } else { if (nodeToCompare.RightChild == null) { nodeToCompare.RightChild = new BSTreeNode(keyToInsert); return; } nodeToCompare = nodeToCompare.RightChild; } } } } }
/// <summary> /// Add tree to list ascending. /// </summary> /// <param name="elementToPaste"></param> private void treeToList(BSTreeNode elementToPaste) { if (tree.root == null) { return; } if (elementToPaste != null) { listForTree.Add(elementToPaste.Key); } if (elementToPaste.LeftChild != null) { treeToList(elementToPaste.LeftChild); } if (elementToPaste.RightChild != null) { treeToList(elementToPaste.RightChild); } }
public BSTreeNode <T> FindNode(T data, BSTreeNode <T> startNode = null) { if (data == null) { throw new NullTreeNodeException(); } startNode = startNode ?? root; int comparisson = data.CompareTo(startNode.Data); if (comparisson == 0) { return(startNode); } else if (comparisson < 0) { if (startNode.Left == null) { return(null); } else { return(FindNode(data, startNode.Left)); } } else { if (startNode.Right == null) { return(null); } else { return(FindNode(data, startNode.Right)); } } }
/// <summary> /// Removes element from tree by user's order. /// </summary> /// <param name="keyToRemove"></param> public void RemoveKey(T keyToRemove) { root = Remove(keyToRemove, root); }
/// <summary> /// Removes element from tree. /// </summary> /// <param name="keyToRemove"></param> /// <param name="root"></param> /// <returns></returns> private BSTreeNode Remove(T keyToRemove, BSTreeNode root) { if (root == null) { return(null); } else { if (root.Key.CompareTo(keyToRemove) > 0) { root.LeftChild = Remove(keyToRemove, root.LeftChild); } else { if (root.Key.CompareTo(keyToRemove) < 0) { root.RightChild = Remove(keyToRemove, root.RightChild); } else { if (root.RightChild == null && root.LeftChild == null) { return(null); } else { if (root.LeftChild == null) { return(root.RightChild); } else { if (root.RightChild == null) { return(root.LeftChild); } else { if (root.RightChild.LeftChild == null) { var temp = root.LeftChild; root = root.RightChild; root.LeftChild = temp; return(root); } else { var cur = root.RightChild; while (cur.LeftChild.LeftChild != null) { cur = cur.LeftChild; } var temp = root.LeftChild; root = root.RightChild; cur.LeftChild.LeftChild = temp; return(root); } } } } } } return(root); } }
public BSTree(T root) { this.root = new BSTreeNode <T>(root); }
public void Clear() { root = null; clearTree?.Invoke(this, new EventArgs()); }