private IEnumerable <BinaryNode <T> > Traversal(BinaryNode <T> Node) { if (Node != null) { if (Node.GetLeft() != null) { foreach (BinaryNode <T> LeftNode in Traversal(Node.GetLeft())) { yield return(LeftNode); } } yield return(Node); if (Node.GetRight() != null) { foreach (BinaryNode <T> RightNode in Traversal(Node.GetRight())) { yield return(RightNode); } } } else { yield return(null); } }
private BinaryNode <T> SearchNode <E>(BinaryNode <T> node, Compare <E> compare, E element) { if (compare(node.Value, element) == 0) { return(node); } else { if (compare(node.Value, element) > 0) { if (node.GetLeft() == null) { return(null); } else { return(SearchNode(node.GetLeft(), compare, element)); } } else { if (node.GetRight() == null) { return(null); } else { return(SearchNode(node.GetRight(), compare, element)); } } } }
private bool EditElement <E>(BinaryNode <T> node, Compare <E> compare, E element, T Item) { if (compare(node.Value, element) == 0) { node.Value = Item; return(true); } else { if (compare(node.Value, element) > 0) { if (node.GetLeft() == null) { return(false); } else { return(EditElement(node.GetLeft(), compare, element, Item)); } } else { if (node.GetRight() == null) { return(false); } else { return(EditElement(node.GetRight(), compare, element, Item)); } } } }
private bool Eliminate2(BinaryNode <T> node) { // left BinaryNode <T> TempLeft = node.GetPadre().GetLeft(); // right BinaryNode <T> TempRight = node.GetPadre().GetRight(); BinaryNode <T> actual = node.GetLeft() != null?node.GetLeft() : node.GetRight(); if (TempLeft == node) { node.GetPadre().SetLeft(actual); actual.SetPadre(node.GetPadre()); node.SetRight(null); node.SetLeft(null); count--; return(true); } if (TempRight == node) { node.GetPadre().SetRight(actual); /* Eliminando todas las referencias hacia el nodo */ actual.SetPadre(node.GetPadre()); node.SetRight(null); node.SetLeft(null); count--; return(true); } return(false); }
public bool Eliminate(BinaryNode <T> node) { bool NodeRight = node.GetRight() != null ? true : false; bool NodeLeft = node.GetLeft() != null ? true : false; //If node does not exist. if (node == null) { return(false); } if (!NodeRight && !NodeLeft) { return(Eliminate1(node)); } if (NodeRight && !NodeLeft) { return(Eliminate2(node)); } if (!NodeRight && NodeLeft) { return(Eliminate2(node)); } if (NodeRight && NodeLeft) { return(Eliminate3(node)); } return(false); }
private BinaryNode <T> RotateRight(BinaryNode <T> binaryNode) { BinaryNode <T> left = binaryNode.GetLeft(); BinaryNode <T> leftRight = left.GetRight(); BinaryNode <T> parent = binaryNode.GetPadre(); left.SetPadre(parent); left.SetRight(binaryNode); binaryNode.SetLeft(leftRight); binaryNode.SetPadre(left); if (leftRight != null) { leftRight.SetPadre(binaryNode); } if (binaryNode == this.root) { this.root = left; } else if (parent.GetLeft() == binaryNode) { parent.SetLeft(left); } else { parent.SetRight(left); } left.Balance--; binaryNode.Balance = -left.Balance; return(left); }
public bool Eliminate(BinaryNode <T> binaryNode) { bool NodeRight = binaryNode.GetRight() != null ? true : false; bool NodeLeft = binaryNode.GetLeft() != null ? true : false; //If note is empty. if (binaryNode == null) { return(false); } if (!NodeRight && !NodeLeft) { return(Delete1(binaryNode)); } if (NodeRight && !NodeLeft) { return(Delete2(binaryNode)); } if (!NodeRight && NodeLeft) { return(Delete2(binaryNode)); } if (NodeRight && NodeLeft) { return(Delete3(binaryNode)); } return(false); }
private BinaryNode <T> RotateLeft(BinaryNode <T> binaryNode) { BinaryNode <T> right = binaryNode.GetRight(); BinaryNode <T> rightLeft = right.GetLeft(); BinaryNode <T> parent = binaryNode.GetPadre(); right.SetPadre(parent); right.SetLeft(binaryNode); binaryNode.SetRight(rightLeft); binaryNode.SetPadre(right); if (rightLeft != null) { rightLeft.SetPadre(binaryNode); } if (binaryNode == this.root) { this.root = right; } else if (parent.GetRight() == binaryNode) { parent.SetRight(right); } else { parent.SetLeft(right); } right.Balance++; binaryNode.Balance = -right.Balance; return(right); }
private BinaryNode <T> RotateLeftRight(BinaryNode <T> binaryNode) { BinaryNode <T> left = binaryNode.GetLeft(); BinaryNode <T> leftRight = left.GetRight(); BinaryNode <T> parent = binaryNode.GetPadre(); BinaryNode <T> leftRightRight = leftRight.GetRight(); BinaryNode <T> leftRightLeft = leftRight.GetLeft(); leftRight.SetPadre(parent); binaryNode.SetLeft(leftRightRight); left.SetRight(leftRightLeft); leftRight.SetLeft(left); leftRight.SetRight(binaryNode); left.SetPadre(leftRight); binaryNode.SetPadre(leftRight); if (leftRightRight != null) { leftRightRight.SetPadre(binaryNode); } if (leftRightLeft != null) { leftRightLeft.SetPadre(left); } if (binaryNode == this.root) { SetRoot(leftRight); } else if (parent.GetLeft() == binaryNode) { parent.SetLeft(leftRight); } else { parent.SetRight(leftRight); } if (leftRight.Balance == -1) { binaryNode.Balance = 0; left.Balance = 1; } else if (leftRight.Balance == 0) { binaryNode.Balance = 0; left.Balance = 0; } else { binaryNode.Balance = -1; left.Balance = 0; } leftRight.Balance = 0; return(leftRight); }
private BinaryNode <T> RotateRightLeft(BinaryNode <T> binaryNode) { BinaryNode <T> right = binaryNode.GetRight(); BinaryNode <T> rightLeft = right.GetLeft(); BinaryNode <T> parent = binaryNode.GetPadre(); BinaryNode <T> rightLeftLeft = rightLeft.GetLeft(); BinaryNode <T> rightLeftRight = rightLeft.GetRight(); rightLeft.SetPadre(parent); binaryNode.SetRight(rightLeftLeft); right.SetLeft(rightLeftRight); rightLeft.SetRight(right); rightLeft.SetLeft(binaryNode); right.SetPadre(rightLeft); binaryNode.SetPadre(rightLeft); if (rightLeftLeft != null) { rightLeftLeft.SetPadre(binaryNode); } if (rightLeftRight != null) { rightLeftRight.SetPadre(right); } if (binaryNode == this.root) { SetRoot(rightLeft); } else if (parent.GetRight() == binaryNode) { parent.SetRight(rightLeft); } else { parent.SetLeft(rightLeft); } if (rightLeft.Balance == 1) { binaryNode.Balance = 0; right.Balance = -1; } else if (rightLeft.Balance == 0) { binaryNode.Balance = 0; right.Balance = 0; } else { binaryNode.Balance = 1; right.Balance = 0; } rightLeft.Balance = 0; return(rightLeft); }
private void PostOrder(BinaryNode <T> node, ref List <T> list) { if (node != null) { PostOrder(node.GetLeft(), ref list); PostOrder(node.GetRight(), ref list); list.Add(node.Value); } }
private void InOrden(BinaryNode <T> node, ref List <T> list) { if (node != null) { InOrden(node.GetLeft(), ref list); list.Add(node.Value); InOrden(node.GetRight(), ref list); } }
private void PreOrder(BinaryNode <T> binaryNode, ref List <T> list) { if (binaryNode != null) { list.Add(binaryNode.Value); PreOrder(binaryNode.GetLeft(), ref list); PreOrder(binaryNode.GetRight(), ref list); } }
private void Add(BinaryNode <T> value, BinaryNode <T> root) { if (root == null) { //Insert first element. this.myRoot(value); this.root.SetPadre(null); count++; } else { if (value.Value.CompareTo(root.Value) == 1) { if (root.GetRight() == null) { root.SetRight(value); root.GetRight().SetPadre(root); count++; } else { Add(value, root.GetRight()); } } else { if (root.GetLeft() == null) { root.SetLeft(value); root.GetLeft().SetPadre(root); count++; } else { Add(value, root.GetLeft()); } } } }
private bool Delete3(BinaryNode <T> binaryNode) { BinaryNode <T> binaryNodeMoreLeft = RouteLeft(binaryNode.GetRight()); if (binaryNodeMoreLeft != null) { binaryNode.Value = binaryNodeMoreLeft.Value; Eliminate(binaryNodeMoreLeft); return(true); } return(false); }
private bool Eliminate3(BinaryNode <T> node) { BinaryNode <T> LeftNode = SearchLeft(node.GetRight()); if (LeftNode != null) { node.Value = LeftNode.Value; Eliminate(LeftNode); return(true); } return(false); }
private void InsertBalance(BinaryNode <T> binaryNode, int balance) { while (binaryNode != null) { balance = (binaryNode.Balance += balance); if (balance == 0) { return; } else if (balance == 2) { if (binaryNode.GetLeft().Balance == 1) { RotateRight(binaryNode); } else { RotateLeftRight(binaryNode); } return; } else if (balance == -2) { if (binaryNode.GetRight().Balance == -1) { RotateLeft(binaryNode); } else { RotateRightLeft(binaryNode); } return; } BinaryNode <T> parent = binaryNode.GetPadre(); if (parent != null) { balance = parent.GetLeft() == binaryNode ? 1 : -1; } binaryNode = parent; } }
private void Add(BinaryNode <T> newElement, BinaryNode <T> root) { if (root == null) { this.SetRoot(newElement); count++; this.root.SetPadre(null); } else { BinaryNode <T> binaryNode = this.root; while (binaryNode != null) { int compare = newElement.Value.CompareTo(binaryNode.Value); if (compare < 0) { BinaryNode <T> left = binaryNode.GetLeft(); if (left == null) { binaryNode.SetLeft(newElement); binaryNode.GetLeft().SetPadre(binaryNode); count++; InsertBalance(binaryNode, 1); return; } else { binaryNode = left; } } else { if (compare > 0) { BinaryNode <T> right = binaryNode.GetRight(); if (right == null) { binaryNode.SetRight(newElement); binaryNode.GetRight().SetPadre(binaryNode); count++; InsertBalance(binaryNode, -1); return; } else { binaryNode = right; } } else { binaryNode.Value = newElement.Value; return; } } } } }
private bool Delete2(BinaryNode <T> binaryNode) { BinaryNode <T> TempLeft = binaryNode.GetPadre().GetLeft(); BinaryNode <T> TempRight = binaryNode.GetPadre().GetRight(); BinaryNode <T> current = binaryNode.GetLeft() != null?binaryNode.GetLeft() : binaryNode.GetRight(); if (TempLeft == binaryNode) { binaryNode.GetPadre().SetLeft(current); current.SetPadre(binaryNode.GetPadre()); binaryNode.SetRight(null); binaryNode.SetLeft(null); count--; return(true); } if (TempRight == binaryNode) { binaryNode.GetPadre().SetRight(current); current.SetPadre(binaryNode.GetPadre()); binaryNode.SetRight(null); binaryNode.SetLeft(null); count--; return(true); } return(false); }