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 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 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 Eliminate1(BinaryNode <T> node) { //left BinaryNode <T> TempLeft = node.GetPadre().GetLeft(); //right BinaryNode <T> TempRight = node.GetPadre().GetRight(); if (TempLeft == node) { node.GetPadre().SetLeft(null); count--; return(true); } if (TempRight == node) { node.GetPadre().SetRight(null); count--; return(true); } return(false); }
private bool Delete1(BinaryNode <T> binaryNode) { //left son BinaryNode <T> TempLeft = binaryNode.GetPadre().GetLeft(); //right son BinaryNode <T> TempRight = binaryNode.GetPadre().GetRight(); if (TempLeft == binaryNode) { binaryNode.GetPadre().SetLeft(null); count--; return(true); } if (TempRight == binaryNode) { binaryNode.GetPadre().SetRight(null); count--; 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; } }