private bool AddChild(Node <T> childNode) { if (childNode.Key.CompareTo(this.Key) > 0) { //child node key is greater than this key if (RightChildNode is null) { RightChildNode = childNode; return(true); } return(RightChildNode.AddChild(childNode)); } else if (childNode.Key.CompareTo(this.Key) < 0) { //child node key is lesser than this key if (LeftChildNode is null) { LeftChildNode = childNode; return(true); } return(LeftChildNode.AddChild(childNode)); } else { //child node key is equal to this key //do nothing return(false); } }
public int Balance() { var leftWeight = LeftChildNode.Balance(); var rightWeight = RightChildNode.Balance(); var ratio = (double)leftWeight / (leftWeight + rightWeight); Position = Length * (1 - ratio); return(leftWeight + rightWeight); }
public Node <T> GetNodeByKey(T key) { if (key.CompareTo(this.Key) > 0 && !(RightChildNode is null)) { //child node is greater than this key return(RightChildNode.GetNodeByKey(key)); } else if (key.CompareTo(this.Key) < 0 && !(LeftChildNode is null)) { //child node is smaller than this key return(LeftChildNode.GetNodeByKey(key)); } return(key.CompareTo(this.Key) == 0 ? this : null); }
public void Print(int depth = 0) { var msg = ""; for (var i = 0; i < depth; i++) { msg += "\t"; } msg += ToString(); Console.WriteLine(msg); LeftChildNode.Print(depth + 1); RightChildNode.Print(depth + 1); }
//needs fixing public int NumberOfChildren() { if (NumberOfDirectChilren() == 0) { return(0); } var result = this.NumberOfDirectChilren(); if (!(this.LeftChildNode is null)) { result += LeftChildNode.NumberOfChildren(); } if (!(this.RightChildNode is null)) { result += RightChildNode.NumberOfChildren(); } return(result); }