public bool SearchNodeHere(NodeTree <T> searched) { if (this.CompareTo(searched) == 0) { return(true); } else if (this.CompareTo(searched) < 0) { if (this.Right == null) { return(false); } this.Right.SearchNodeHere(searched); } else { if (this.Left == null) { return(false); } this.Left.SearchNodeHere(searched); } return(false); }
public void AddNodeHere(NodeTree <T> forAdd) { if (this.CompareTo(forAdd) == 0) { throw new ArgumentException("Nodes are equals!"); } if (this.CompareTo(forAdd) < 0) { if (this.Right == null) { this.Right = forAdd; } else { this.Right.AddNodeHere(forAdd); } } else { if (this.Left == null) { this.Left = forAdd; } else { this.Left.AddNodeHere(forAdd); } } }
public void FindAndRemove(NodeTree <T> forRemove) { if (this.CompareTo(forRemove) < 0) { if (this.Right == null) { return; } if (this.Right.CompareTo(forRemove) == 0) { this.Right = this.Right.RemoveThisNode(); } else { this.Right.FindAndRemove(forRemove); } } else { if (this.Left == null) { return; } if (this.Left.CompareTo(forRemove) == 0) { this.Left = this.Left.RemoveThisNode(); } else { this.Left.FindAndRemove(forRemove); } } }
// need reworking still but working public NodeTree <T> RemoveThisNode() { if (this.Left == null & this.Right == null) { return(null); } else if (this.Left == null) { return(this.Right); } else if (this.Right == null) { return(this.Left); } else { T minData = this.Right.FindMin(); this.Data = minData; if (this.Right.Data.Equals(minData)) { this.Right = this.Right.RemoveThisNode(); } else { this.Right.RemoveMin(minData); } return(this); } }
public bool Search(T data) { var searched = new NodeTree <T>(data); if (this.IsEmpty()) { return(false); } else { return(this.root.SearchNodeHere(searched)); } }
public void Add(T data) { var forAdd = new NodeTree <T>(data); if (this.IsEmpty()) { this.root = forAdd; } else { root.AddNodeHere(forAdd); } }
public void Remove(T data) { var forRemove = new NodeTree <T>(data); if (this.IsEmpty()) { return; } else { if (this.root.Equals(forRemove)) { this.Root = this.Root.RemoveThisNode(); } else { this.Root.FindAndRemove(forRemove); } } }
private BinaryTree(NodeTree <T> root) { this.root = new NodeTree <T>(root); }
public int CompareTo(NodeTree <T> other) { return(this.Data.CompareTo(other.Data)); }
internal NodeTree(NodeTree <T> forCopy) { this.Data = forCopy.Data; this.Left = forCopy.Left?.DeepCopy(); this.Right = forCopy.Right?.DeepCopy(); }