private static void Add(BinarySearchNode <T> parent, T childValue) { if (parent.Key.CompareTo(childValue) >= 0) { if (parent.LeftChild != null) { Add(parent.LeftChild, childValue); } else { parent.LeftChild = new BinarySearchNode <T>(childValue) { Parent = parent }; } } else { if (parent.RigthChild != null) { Add(parent.RigthChild, childValue); } else { parent.RigthChild = new BinarySearchNode <T>(childValue) { Parent = parent }; } } }
public static BinarySearchNode <T> GetMinimum <T>(BinarySearchNode <T> node) where T : IComparable { BinarySearchNode <T> result = node; while (result.LeftChild != null) { result = result.LeftChild; } return(result); }
private void GetPostorderTreeWalk(BinarySearchNode <T> node) { if (node.LeftChild != null) { GetPostorderTreeWalk(node.LeftChild); } if (node.RigthChild != null) { GetPostorderTreeWalk(node.RigthChild); } treeWalk.Add(node.Key); }
public BinarySearchTree(T[] values) { if (values == null) { return; } this.count = values.Length; this.Parent = new BinarySearchNode <T>(values[0]); for (int i = 1; i < values.Length; i++) { Add(this.Parent, values[i]); } }
public static BinarySearchNode <T> LookFor <T>(BinarySearchNode <T> rootNode, T value) where T : IComparable { if (rootNode.Key.Equals(value)) { return(rootNode); } else if (rootNode.Key.CompareTo(value) > 0 && rootNode.LeftChild != null) { return(LookFor(rootNode.LeftChild, value)); } else if (rootNode.Key.CompareTo(value) < 0 && rootNode.RigthChild != null) { return(LookFor(rootNode.RigthChild, value)); } return(null); }
public static BinarySearchNode <T> LookFor <T>(BinarySearchNode <T> rootNode, BinarySearchNode <T> node) where T : IComparable { //Check the difference with rootNode.Equal(node) if (object.ReferenceEquals(rootNode, node)) { return(rootNode); } else if (rootNode.Key.CompareTo(node.Key) > 0 && rootNode.LeftChild != null) { return(LookFor(rootNode.LeftChild, node)); } else if (rootNode.Key.CompareTo(node.Key) < 0 && rootNode.RigthChild != null) { return(LookFor(rootNode.RigthChild, node)); } return(null); }
public static BinarySearchNode <T> GetPredecessor <T>(BinarySearchNode <T> node) where T : IComparable { if (node.LeftChild != null) { return(TreeOperation.GetMaximum(node.LeftChild)); } BinarySearchNode <T> result = node.Parent; while (result != null && result.LeftChild == node) { node = result; result = node.Parent; } return(result); }
public virtual void Delete(BinarySearchNode <T> newNode) { }
public BinarySearchNode <T> GetPredecessor(BinarySearchNode <T> node) { return(TreeOperation.GetPredecessor(node)); }