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); }
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); }
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> 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); }
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 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 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; } } } } }