public bool RotateLeft(AbstractNode <T> node) { /* * z y * / \ / \ * T1 y Left Rotate(z) z x * / \ - - - - - - - -> / \ / \ * p x T1 p T3 T4 * / \ * T3 T4 */ AvlTreeNode <T> z = (AvlTreeNode <T>)node; AvlTreeNode <T> y = (AvlTreeNode <T>)node.Right; AbstractNode <T> p = y.Left; y.Ancestor = z.Ancestor; z.Ancestor = y; z.Right = p; if (p != null) { p.Ancestor = z; } y.Left = z; SetAncestor(y); AdjustHeightToNode(z); AdjustBalancefactorToNode(z); AdjustHeightToNode(y); AdjustBalancefactorToNode(y); return(true); }
private void AdjustHeights(AbstractNode <T> node, bool deletion) { AvlTreeNode <T> current = (AvlTreeNode <T>)node; AvlTreeNode <T> leftNode = (AvlTreeNode <T>)current.Left; AvlTreeNode <T> rightNode = (AvlTreeNode <T>)current.Right; while (true) { int left = leftNode?.Height ?? 0; int right = rightNode?.Height ?? 0; current.Height = 1 + Math.Max(left, right); current.BalanceFactor = left - right; if (deletion && (current.BalanceFactor > 1 || current.BalanceFactor < -1)) { Rebalance(current); } if (current.Ancestor == null) { return; } current = (AvlTreeNode <T>)current.Ancestor; leftNode = (AvlTreeNode <T>)current.Left; rightNode = (AvlTreeNode <T>)current.Right; } }
private static void CheckResultAfterDeletion(AvlTreeNode root) { AssertTrue(() => root.Value == 1); AssertTrue(() => root.Left.Value == 0); AssertTrue(() => root.Left.Right == null); AssertTrue(() => root.Left.Left.Value == -1); AssertTrue(() => root.Left.Left.Right == null); AssertTrue(() => root.Left.Left.Left == null); AssertTrue(() => root.Right.Value == 9); AssertTrue(() => root.Right.Left.Value == 5); AssertTrue(() => root.Right.Right.Value == 11); AssertTrue(() => root.Right.Right.Left == null); AssertTrue(() => root.Right.Right.Right == null); AssertTrue(() => root.Right.Left.Right.Value == 6); AssertTrue(() => root.Right.Left.Right.Left == null); AssertTrue(() => root.Right.Left.Right.Right == null); AssertTrue(() => root.Right.Left.Left.Value == 2); AssertTrue(() => root.Right.Left.Left.Left == null); AssertTrue(() => root.Right.Left.Left.Right == null); }
private int ValidateHeight(AvlTreeNode <T> node) { if (node == null) { return(0); } return(node.GetHeight()); }
private AvlTreeNode <T> FindMin(AvlTreeNode <T> node) { if (node.GetLeft() == null) { return(node); } return(FindMin(node.GetLeft())); }
private static int Height <T>(AvlTreeNode <T> node, int deep) { var leftHeight = node.Left != null?Height(node.Left, deep + 1) : 0; var rightHeight = node.Right != null?Height(node.Right, deep + 1) : 0; return((leftHeight > rightHeight ? leftHeight : rightHeight) + 1); }
public void Key_Setter_StoresTheValue() { var instance = new AvlTreeNode <int, string> { Key = 100 }; Assert.AreEqual(100, instance.Key); }
public void Balance_Setter_StoresTheValue() { var instance = new AvlTreeNode <int, string> { Balance = -1 }; Assert.AreEqual(-1, instance.Balance); }
public void Value_Setter_StoresTheValue() { var instance = new AvlTreeNode <int, string> { Value = "Hello" }; Assert.AreEqual("Hello", instance.Value); }
public void MaxHeightTest_RootLeft() { var t = new AvlTreeNode(5) { Left = new AvlTreeNode(4) }; Assert.AreEqual(1, t.MaxHeight); }
public void MaxHeightTest_RootRight() { var t = new AvlTreeNode(5) { Right = new AvlTreeNode(6) }; Assert.AreEqual(1, t.MaxHeight); }
public void MaxHeightTest_RootTwoChildren() { var t = new AvlTreeNode(5) { Left = new AvlTreeNode(4), Right = new AvlTreeNode(6) }; Assert.AreEqual(1, t.MaxHeight); }
public void BalanceTest_RootLeft() { var t = new AvlTreeNode(5) { Left = new AvlTreeNode(4) }; Assert.AreEqual(-1, t.Balance); }
public void BalanceTest_RootRight() { var t = new AvlTreeNode(5) { Right = new AvlTreeNode(6) }; Assert.AreEqual(1, t.Balance); }
public void BalanceTestTest_RootTwoChildren() { var t = new AvlTreeNode(5) { Left = new AvlTreeNode(4), Right = new AvlTreeNode(6) }; Assert.AreEqual(0, t.Balance); }
public void RightChild_Setter_StoresTheValue() { var r = new AvlTreeNode <int, string>(); var instance = new AvlTreeNode <int, string> { RightChild = r }; Assert.AreSame(r, instance.RightChild); }
private static int Count <T>(AvlTreeNode <T>?node) { if (node == null) { return(0); } var count = 1 + Count(node.Left) + Count(node.Right); return(count); }
private void AdjustBalancefactorToNode(AvlTreeNode <T> node) { AvlTreeNode <T> current = (AvlTreeNode <T>)node; AvlTreeNode <T> leftNode = (AvlTreeNode <T>)current.Left; AvlTreeNode <T> rightNode = (AvlTreeNode <T>)current.Right; int left = leftNode?.Height ?? 0; int right = rightNode?.Height ?? 0; current.BalanceFactor = left - right; }
private void AdjustHeightToNode(AvlTreeNode <T> node) { AvlTreeNode <T> current = (AvlTreeNode <T>)node; AvlTreeNode <T> leftNode = (AvlTreeNode <T>)current.Left; AvlTreeNode <T> rightNode = (AvlTreeNode <T>)current.Right; int left = leftNode?.Height ?? 0; int right = rightNode?.Height ?? 0; current.Height = 1 + Math.Max(left, right); }
private int GetBalanceFactor(AvlTreeNode <T> node) { AvlTreeNode <T> current = (AvlTreeNode <T>)node; AvlTreeNode <T> leftNode = (AvlTreeNode <T>)current.Left; AvlTreeNode <T> rightNode = (AvlTreeNode <T>)current.Right; int left = leftNode?.Height ?? 0; int right = rightNode?.Height ?? 0; return(left - right); }
public string[] ShowAllBooksArray(string libraryName) { Library l = new Library(libraryName); AvlTreeNode <Library> lib = (AvlTreeNode <Library>)LibraryModel.Libraries.SearchNode(l, LibraryModel.Libraries.Root); if (lib != null && lib.Data.AllBooksByName != null) { return(lib.Data.AllBooksByName.InorderTraversalToStringArray(lib.Data.AllBooksByName.Root)); } return(new string[] {}); }
public void BalanceFactorTest() { AvlTree <int> actual = new AvlTree <int> { 10, 20 }; AvlTreeNode <int> root = actual.FindNode(10); AvlTreeNode <int> leaf = actual.FindNode(20); AvlTreeNode <int> emptyNode = default(AvlTreeNode <int>); Assert.AreEqual(actual.GetBalanceFactor(root), -1); Assert.AreEqual(actual.GetBalanceFactor(leaf), 0); Assert.AreEqual(actual.GetBalanceFactor(emptyNode), 0); }
public bool ArchiveBook(string isbn, int bookId, string libraryName) { Book b = new Book(isbn, bookId, ""); AvlTreeNode <Library> lib = (AvlTreeNode <Library>)LibraryModel.Libraries.SearchNode(new Library(libraryName), LibraryModel.Libraries.Root); AbstractNode <Book> foundB = lib.Data.AllBooksByIsbn.SearchNode(b, lib.Data.AllBooksByIsbn.Root); if (foundB != null) { foundB.Data.IsArchived = true; return(foundB.Data.IsArchived); } return(false); }
private AvlTreeNode <T> Balance(AvlTreeNode <T> node) { if (node == null) { return(null); } int balance = ValidateHeight(node.GetLeft()) - ValidateHeight(node.GetRight()); if (balance == 2) { if (node.GetLeft().GetLeft() != null) { if (node == _root) { _root = node.GetLeft(); } node = node.RotateLeftChild(); } else { if (node == _root) { _root = node.GetLeft().GetRight(); } node = node.RotateDoubleLeft(); } } else if (balance == -2) { if (node.GetRight().GetRight() != null) { if (node == _root) { _root = node.GetRight(); } node = node.RotateRightChild(); } else { if (node == _root) { _root = node.GetRight().GetLeft(); } node = node.RotateDoubleRight(); } } node.CalculateHeight(); return(node); }
private bool Rebalance(AvlTreeNode <T> nodeToRebalance) { AvlTreeNode <T> node = (AvlTreeNode <T>)nodeToRebalance; AvlTreeNode <T> right = (AvlTreeNode <T>)node.Right; AvlTreeNode <T> left = (AvlTreeNode <T>)node.Left; bool loop = true; while (loop) { // Left left case || Left right case if (node.BalanceFactor == 2) { // left left case if (left.BalanceFactor >= 0) { return(RotateRight(node)); } // left right case if (left.BalanceFactor < 0) { return(RotateLeftRight(node)); } } // Right right case || right left case if (node.BalanceFactor == -2) { // Right right case if (right.BalanceFactor <= 0) { return(RotateLeft(node)); } // Right left case if (right.BalanceFactor > 0) { return(RotateRightLeft(node)); } } if (node.Ancestor == null) { loop = false; } else { node = (AvlTreeNode <T>)node.Ancestor; right = (AvlTreeNode <T>)node.Right; left = (AvlTreeNode <T>)node.Left; } } return(true); }
public bool Add(AvlTreeNode <T> node) { if (Root == null) { Root = node; } else { InsertNode(Root, node.Data, node); } AdjustHeightToNode(Root); AdjustBalancefactorToNode(Root); return(true); }
public void MaxHeightTest() { var t = new AvlTreeNode(5) { Left = new AvlTreeNode(3) { Left = new AvlTreeNode(2), Right = new AvlTreeNode(4) }, Right = new AvlTreeNode(6) }; Assert.AreEqual(2, t.MaxHeight); }
public void BalanceTest() { var t = new AvlTreeNode(5) { Left = new AvlTreeNode(3) { Left = new AvlTreeNode(2), Right = new AvlTreeNode(4) }, Right = new AvlTreeNode(6) }; Assert.AreEqual(-1, t.Balance); }
private bool InsertNode(AbstractNode <T> current, T data, AvlTreeNode <T> node) { AvlTreeNode <T> inserted = node; while (true) { //if (inserted.Data.CompareTo(current.Data) == 0) if (Comparer.Compare(data, current.Data) == 0) { return(false); } else if (Comparer.Compare(inserted.Data, current.Data) == -1) { if (current.Left == null) { current.Left = inserted; inserted.Ancestor = current; // upravit vysky AdjustHeights(inserted, false); // upravit balance factor //AdjustBalanceFactor(inserted); Rebalance((AvlTreeNode <T>)inserted.Ancestor); return(true); } else { current = current.Left; } } else { if (current.Right == null) { current.Right = inserted; inserted.Ancestor = current; // upravit vysky AdjustHeights(inserted, false); // upravit balance factor //AdjustBalanceFactor(inserted); Rebalance((AvlTreeNode <T>)inserted.Ancestor); return(true); } else { current = current.Right; } } } }
private AvlTreeNode <T> SearchRecursive(T value, AvlTreeNode <T> node) { if (node == null) { return(null); } if (node.GetValue().CompareTo(value) > 0) { return(SearchRecursive(value, node.GetLeft())); } if (node.GetValue().CompareTo(value) < 0) { return(SearchRecursive(value, node.GetRight())); } return(node); }
public void HeightTest() { AvlTreeNode<int> actual = new AvlTreeNode<int>(20); Assert.AreEqual(1, actual.Height); }
public void HeightPUT(int value) { AvlTreeNode<int> actual = new AvlTreeNode<int>(value); PexAssert.AreEqual(1, actual.Height); }