public void ThreadedBinaryTreeNodeRemoveRightGuardTest() { ThreadedBinaryTree <int> threadedTree = BuildTestBinaryTree().BuildThreadedTree(); var n7 = threadedTree.Root.Right.Right; n7.RemoveRight(); }
public void BuildThreadedTreeSimpleTest() { ThreadedBinaryTree <int> threadedTree = BuildTestBinaryTree().BuildThreadedTree(); var root = threadedTree.Root; Assert.IsFalse(root.IsThreaded); var n2 = root.Left; Assert.IsFalse(n2.IsLeftThreaded); Assert.IsTrue(n2.IsRightThreaded); var n3 = root.Right; Assert.IsFalse(n3.IsThreaded); var n4 = n2.Left; Assert.IsTrue(n4.IsLeftThreaded); Assert.IsTrue(n4.IsRightThreaded); var n6 = n3.Left; Assert.IsTrue(n6.IsLeftThreaded); Assert.IsTrue(n6.IsRightThreaded); var n7 = n3.Right; Assert.IsTrue(n7.IsLeftThreaded); Assert.IsTrue(n7.IsRightThreaded); }
public void ThreadedBinaryTreeNodeRemoveLeftGuardTest() { ThreadedBinaryTree <int> threadedTree = BuildTestBinaryTree().BuildThreadedTree(); var n4 = threadedTree.Root.Left.Left; n4.RemoveLeft(); }
public void InOrderTraverseTest() { List <int> list = new List <int>(); ThreadedBinaryTree <int> threadedTree = BuildTestBinaryTree().BuildThreadedTree(); threadedTree.InOrderTraverse(x => list.Add(x.Value)); CollectionAssert.AreEqual(new int[] { 4, 2, 1, 6, 3, 7 }, list); }
public void BigThreadedBinaryTreeSimpleTest() { List <int> list = new List <int>(); ThreadedBinaryTree <int> tree = BuildBigTestBinaryTree().BuildThreadedTree(); tree.PreOrderTraverse(x => list.Add(x.Value)); CollectionAssert.AreEqual(new int[] { 1, 2, 4, 8, 9, 5, 10, 3, 6, 7, 11, 12 }, list); list.Clear(); tree.InOrderTraverse(x => list.Add(x.Value)); CollectionAssert.AreEqual(new int[] { 8, 4, 9, 2, 5, 10, 1, 6, 3, 11, 7, 12 }, list); }
public void ThreadedBinaryTreeNodeRemoveRightTest() { ThreadedBinaryTree <int> threadedTree = BuildTestBinaryTree().BuildThreadedTree(); var root = threadedTree.Root; var dummy = root.Right.Right.Right; var result = root.RemoveRight(); Assert.AreEqual(3, result.Value); Assert.IsFalse(root.IsLeftThreaded); Assert.IsTrue(root.IsRightThreaded); Assert.AreSame(root.Right, dummy); }
// возвращаем узел родитель элемента public static ThreadedBinaryTree inorderSuccessor(ThreadedBinaryTree ptr) { // если элемент справа, то выведем его if (ptr.rthread == true) { return(ptr.right); } // иначе выдаём самый левый результат ptr = ptr.right; while (ptr.lthread == false) { ptr = ptr.left; } return(ptr); }
public void ThreadedBinaryTreeNodeInsertLeftSimpleTest() { ThreadedBinaryTree <int> threadedTree = BuildTestBinaryTree().BuildThreadedTree(); var n2 = threadedTree.Root.Left; var n4 = n2.Left; var result = n2.InsertLeft(11); Assert.AreEqual(11, result.Value); Assert.AreSame(n2.Left, result); Assert.IsFalse(result.IsLeftThreaded); Assert.IsTrue(result.IsRightThreaded); Assert.AreSame(result.Left, n4); Assert.AreSame(result.Right, n2); Assert.AreSame(n4.Right, result); List <int> list = new List <int>(); threadedTree.InOrderTraverse(x => list.Add(x.Value)); CollectionAssert.AreEqual(new int[] { 4, 11, 2, 1, 6, 3, 7 }, list); }
public void BigThreadedBinaryTreeRemoveTest() { List <int> list = new List <int>(); ThreadedBinaryTree <int> tree = BuildBigTestBinaryTree().BuildThreadedTree(); var n3 = tree.Root.Right; var n12 = n3.Right.Right; var dummy = n12.Right; Assert.IsFalse(n3.IsLeftThreaded); Assert.IsFalse(n3.IsRightThreaded); n3.RemoveRight(); Assert.IsFalse(n3.IsLeftThreaded); Assert.IsTrue(n3.IsRightThreaded); Assert.AreSame(n3.Right, dummy); tree.PreOrderTraverse(x => list.Add(x.Value)); CollectionAssert.AreEqual(new int[] { 1, 2, 4, 8, 9, 5, 10, 3, 6 }, list); list.Clear(); tree.InOrderTraverse(x => list.Add(x.Value)); CollectionAssert.AreEqual(new int[] { 8, 4, 9, 2, 5, 10, 1, 6, 3 }, list); }
public void ThreadedBinaryTreeNodeInsertRightTest() { ThreadedBinaryTree <int> threadedTree = BuildTestBinaryTree().BuildThreadedTree(); var n7 = threadedTree.Root.Right.Right; var dummy = n7.Right; var result = n7.InsertRight(11); Assert.AreEqual(11, result.Value); Assert.IsTrue(n7.IsLeftThreaded); Assert.IsFalse(n7.IsRightThreaded); Assert.AreSame(n7.Right, result); Assert.IsTrue(result.IsLeftThreaded); Assert.IsTrue(result.IsRightThreaded); Assert.AreSame(result.Left, n7); Assert.AreSame(result.Right, dummy); List <int> list = new List <int>(); threadedTree.InOrderTraverse(x => list.Add(x.Value)); CollectionAssert.AreEqual(new int[] { 4, 2, 1, 6, 3, 7, 11 }, list); }
public void BuildThreadedTreeTest() { ThreadedBinaryTree <int> threadedTree = BuildTestBinaryTree().BuildThreadedTree(); var n2 = threadedTree.Root.Left; var n3 = threadedTree.Root.Right; Assert.AreSame(n2.Right, threadedTree.Root); var n4 = n2.Left; Assert.IsNotNull(n4.Left); Assert.AreSame(n4.Right, n2); var n6 = n3.Left; Assert.AreSame(n6.Left, threadedTree.Root); Assert.AreSame(n6.Right, n3); var n7 = n3.Right; Assert.AreSame(n7.Left, n3); Assert.IsNotNull(n7.Right); }
public static void Main() { // создаём экземпляр прошитого дерева ThreadedBinaryTree threadedBinaryTree = null; Random random = new Random(); for (int n = 1; n < 16; n++) { threadedBinaryTree = insert(threadedBinaryTree, random.Next(0, 100)); } // вызов функции обход дерева Console.WriteLine("Обход прошитого дерева:"); printInOrder(threadedBinaryTree); Console.ReadLine(); }
public void BigThreadedBinaryTreeInsertTest() { List <int> list = new List <int>(); ThreadedBinaryTree <int> tree = BuildBigTestBinaryTree().BuildThreadedTree(); var n3 = tree.Root.Right; var n11 = n3.Right.Left; Assert.IsTrue(n11.IsLeftThreaded); Assert.IsTrue(n11.IsRightThreaded); Assert.AreEqual(3, n11.Left.Value); Assert.AreEqual(7, n11.Right.Value); n3.InsertRight(15); Assert.IsTrue(n11.IsLeftThreaded); Assert.IsTrue(n11.IsRightThreaded); Assert.AreEqual(15, n11.Left.Value); Assert.AreEqual(7, n11.Right.Value); tree.PreOrderTraverse(x => list.Add(x.Value)); CollectionAssert.AreEqual(new int[] { 1, 2, 4, 8, 9, 5, 10, 3, 6, 15, 7, 11, 12 }, list); list.Clear(); tree.InOrderTraverse(x => list.Add(x.Value)); CollectionAssert.AreEqual(new int[] { 8, 4, 9, 2, 5, 10, 1, 6, 3, 15, 11, 7, 12 }, list); }
// функция обхода прошитого дерева public static void printInOrder(ThreadedBinaryTree root) { if (root == null) { Console.Write("Дерево пустое!"); } // проходим до самого левого элемента. ThreadedBinaryTree ptr = root; while (ptr.lthread == false) { ptr = ptr.left; } // выводим на экран предков узла один за одним while (ptr != null) { Console.Write("{0:D} ", ptr.info); ptr = inorderSuccessor(ptr); } }
// добавление узла в прошитое дерево public static ThreadedBinaryTree insert(ThreadedBinaryTree root, int ikey) { // поиск нужного узла ThreadedBinaryTree ptr = root; // ключ родительского элемента ThreadedBinaryTree par = null; while (ptr != null) { // если ключи дублируются if (ikey == (ptr.info)) { Console.Write("Ключ дублируется!\n"); return(root); } // обновление указателя родительского элемента par = ptr; // проходим левую часть дерева if (ikey < ptr.info) { if (ptr.lthread == false) { ptr = ptr.left; } else { break; } } // проходим правую часть дерева else { if (ptr.rthread == false) { ptr = ptr.right; } else { break; } } } // создаём новое дерево ThreadedBinaryTree tmp = new ThreadedBinaryTree(); tmp.info = ikey; tmp.lthread = true; tmp.rthread = true; if (par == null) { root = tmp; tmp.left = null; tmp.right = null; } else if (ikey < (par.info)) { tmp.left = par.left; tmp.right = par; par.lthread = false; par.left = tmp; } else { tmp.left = par; tmp.right = par.right; par.rthread = false; par.right = tmp; } return(root); }