Ejemplo n.º 1
0
        //删除二叉排序树中指定节点
        static void DeleteBST(ref BSTree bsTree, int key)
        {
            if (bsTree == null)
                return;

            if (bsTree.data == key)
            {
                //第一种情况:叶子节点
                if (bsTree.left == null && bsTree.right == null)
                {
                    bsTree = null;
                    return;
                }
                //第二种情况:左子树不为空
                if (bsTree.left != null && bsTree.right == null)
                {
                    bsTree = bsTree.left;
                    return;
                }
                //第三种情况:右子树不为空
                if (bsTree.left == null && bsTree.right == null)
                {
                    bsTree = bsTree.right;
                    return;

                }
                //第四种情况:左右子树都不为空
                if (bsTree.left != null && bsTree.right != null)
                {
                    var node = bsTree.right;   //
                    //找到右子树中的最左节点
                    while (node.left != null)
                    {
                        //遍历左子树
                        node = node.left;

                    }
                    //交换左右孩子
                    node.left = bsTree.left;
                    //判断是真正的叶子节点还是空左孩子的父节点
                    if (node.right == null)
                    {
                        //删掉右子树最左节点
                        DeleteBST(ref bsTree, node.data);
                        node.right = bsTree.right;
                    }
                    bsTree = node;
                }
            }
            if (bsTree.data > key)
            {
                DeleteBST(ref bsTree.left, key);
            }
            else
            {
                DeleteBST(ref bsTree.right, key);
            }
        }
Ejemplo n.º 2
0
        public void InsertTest()
        {
            var values = new int[] { 8, 2, 1, 9, 2, 5 };
            var bstree = new BSTree <int>();

            bstree.BuildBSTree(values);

            bstree.Delete(5);
        }
Ejemplo n.º 3
0
 public HashTable(int m)
 {
     this.prime_number = m;
     table             = new BSTree[m];
     for (int i = 0; i < table.Length; i++)
     {
         table[i] = new BSTree();
     }
 }
        public static BSTNode ToBSTree(this int[] arr)
        {
            BSTNode node = null;
            BSTree  tree = new BSTree();

            foreach (var a in arr)
            {
                node = tree.Insert(node, a);
            }
            return(node);
        }
Ejemplo n.º 5
0
 public void BSTDelete(BSTree <int> tree, int value)
 {
     if (tree.root == null)
     {
         return;
     }
     else
     {
         BSTDeleteStartingFrom(tree.root, value);
     }
 }
Ejemplo n.º 6
0
        public RestTemplate BuildTree([FromBody] int[] a)
        {
            if (a == null || a.Length == 0)
            {
                return(new RestTemplate((int)HttpStatusCode.InternalServerError, null, "An array of integers is required to build a tree"));
            }
            BSTree  tree = new BSTree(a, true);
            NodeDto dto  = toDto(tree.root, 1, 0);

            return(new RestTemplate((int)HttpStatusCode.OK, dto, ""));
        }
Ejemplo n.º 7
0
 public override void OnInspectorGUI()
 {
     if (GUILayout.Button("生成树"))
     {
         List <int> list = new List <int> {
             50, 30, 70, 10, 40, 90, 80
         };
         BSTree bsTree         = BSTreeBLL.Create(list);
         BinaryTree <string> k = new BinaryTree <string>();
     }
 }
Ejemplo n.º 8
0
        public void Find_StudentDataIsNull_ShouldReturnNullTreeNodeException()
        {
            //arrange
            Student          A      = new Student("", "", "", 50);
            Student          B      = new Student("", "", "", 30);
            BSTree <Student> bSTree = new BSTree <Student>(A);

            bSTree.Insert(B);

            //act
            bSTree.FindNode(null);
        }
Ejemplo n.º 9
0
    public static void Main(string[] args)
    {
        BSTree tree = new BSTree();

        tree.Add(4);
        tree.Add(7);
        tree.Add(3);
        tree.Add(6);
        tree.Add(8);
        tree.Add(5);
        tree.Preorder(tree.root);
    }
Ejemplo n.º 10
0
        public RestTemplate DeleteX([FromBody] NodeDto root, int x)
        {
            BSTree tree = new BSTree(toEntity(root, NodeFactoryImpl.getInstance()));

            if (tree.findX(x) == null)
            {
                return(new RestTemplate((int)HttpStatusCode.Conflict, null, x + " is not found"));
            }

            tree.delete(x);
            return(new RestTemplate((int)HttpStatusCode.OK, toDto(tree.root, 1, 0), ""));
        }
Ejemplo n.º 11
0
 public void BSTAdd(BSTree <int> tree, int value)
 {
     if (tree.root == null)
     {
         tree.root = new TreeNode <int> (value, tree.root, null, null);
         return;
     }
     else
     {
         BSTInsertStartingFrom(tree.root, value);
     }
 }
Ejemplo n.º 12
0
    // Use this for initialization
    void Start()
    {
        BSTree <int, int> orderBst = new BSTree <int, int> ();
        int n = 1000;

        for (int i = 0; i != n; i++)
        {
            orderBst.Insert(i, i);
        }
        print("---------从小到大打印二叉搜索树");
        string str = "";

        while (orderBst.Size() != 0)
        {
            Node <int, int> node = orderBst.ExtraMinNode();
            str += node.value + " ";
        }
        print("从小到大 :" + str);

        for (int i = 0; i != n; i++)
        {
            orderBst.Insert(i, i);
        }

        print("---------从大到小打印二叉搜索树");
        str = "";
        while (orderBst.Size() != 0)
        {
            Node <int, int> node = orderBst.ExtraMaxNode();
            str += node.value + " ";
        }
        print("从大到小 :" + str);

        for (int i = 0; i != n; i++)
        {
            orderBst.Insert(i, i);
        }
        print("---------修改任意节点的值,删除1,3,4,5,6,8,90,999");
        int[] changeArr = { 1, 3, 4, 5, 6, 8, 90, 999 };
        for (int i = 0; i != changeArr.Length; i++)
        {
            orderBst.DeleteNode(changeArr [i]);
        }

        str = "";
        while (orderBst.Size() != 0)
        {
            Node <int, int> node = orderBst.ExtraMinNode();
            str += node.value + " ";
        }
        print("修改后的数值从小到大 :" + str);
    }
Ejemplo n.º 13
0
        public void Clear_ClearTreeFilledWithStudent_ShouldReturnNullRoot()
        {
            //arrange
            Student          A      = new Student("", "", "", 50);
            BSTree <Student> bSTree = new BSTree <Student>(A);

            //act
            bSTree.Clear();
            var res = bSTree.Root;

            //assert
            Assert.AreEqual(null, res);
        }
Ejemplo n.º 14
0
    // Получить средне-арифметическое значение всех внутренних узлов
    public static int GetArithmeticalMean(this BSTree bTree)
    {
        if (bTree.Root == null)
        {
            return(0);
        }

        int countNodes = 0;
        int summ       = calcAvg(bTree.Root, ref countNodes);

        // System.Console.WriteLine("countNodes:{0} - summ:{1}", countNodes, summ);
        return(summ / countNodes);
    }
Ejemplo n.º 15
0
        public RestTemplate GetPathLengthToX([FromBody] NodeDto root, int x)
        {
            BSTree tree    = new BSTree(toEntity(root, NodeFactoryImpl.getInstance()));
            int    length  = tree.findPathLengthToX(x);
            string message = "";

            if (length == -1)
            {
                message = x + " doesn't exist";
            }

            return(new RestTemplate((int)HttpStatusCode.OK, length, message));
        }
Ejemplo n.º 16
0
        public void TestEnumerator(int[] ini, int[] expected)
        {
            BSTree tree = new BSTree();

            tree.Init(ini);
            int[] actual   = new int[ini.Length];
            int   iterator = 0;

            foreach (var item in tree)
            {
                actual[iterator++] = (int)item;
            }
            CollectionAssert.AreEqual(actual, expected);
        }
Ejemplo n.º 17
0
        ///<summary>
        /// 中序遍历二叉排序树
        ///</summary>
        ///<param name="bsTree"></param>
        ///<returns></returns>
        static void LdrBST(BSTree bsTree)
        {
            if (bsTree != null)
            {
                //遍历左子树
                LdrBST(bsTree.Left);

                //输入节点数据
                Console.Write(bsTree.Data + "");

                //遍历右子树
                LdrBST(bsTree.Right);
            }
        }
Ejemplo n.º 18
0
        static void BSTAdd(BSTree <int> tree, int value)
        {
            if (tree.root == null)
            {
                // TODO: EX4.1; tree.root = ?

                return;
            }
            else
            {
                // TODO: Ex4.2
                return; // PLACEHOLDER; REMOVE AND REPLACE WITH YOUR CODE
            }
        }
Ejemplo n.º 19
0
        public void TestMethod3()
        {
            BSTree bt = new BSTree();

            int[] arr1 = { 22, 5, 91, 2, 47, 13, 32, 45, 95 };

            for (int i = 0; i < arr1.Length; i++)
            {
                bt.insert(arr1[i]);
            }

            Node parent = bt.findParent(32);

            Assert.AreEqual(47, parent.value);
        }
Ejemplo n.º 20
0
        public void Remove_RemovingNodeHavingNoChildren_ShouldRemoveNode()
        {
            //arrange
            Student          A      = new Student("", "", "", 50);
            Student          B      = new Student("", "", "", 60);
            BSTree <Student> bSTree = new BSTree <Student>(A);

            bSTree.Insert(B);
            //act
            bSTree.Remove(B);
            var res = bSTree.FindNode(B);

            //assert
            Assert.AreEqual(res, null);
        }
Ejemplo n.º 21
0
        public void Find_StudentIsAbsent_ShouldReturnNodeWithNullValue()
        {
            //arrange
            Student          A      = new Student("", "", "", 50);
            Student          B      = new Student("", "", "", 30);
            Student          C      = new Student("", "", "", 70);
            BSTree <Student> bSTree = new BSTree <Student>(A);

            bSTree.Insert(B);
            //act
            var res = bSTree.FindNode(C);

            //assert
            Assert.AreEqual(res, null);
        }
Ejemplo n.º 22
0
        public void Insert_InsertStudentsWithSameScoreButDiferentSurnames_BStudentShouldBeLeftChild()
        {
            //arrange
            Student          A      = new Student("", "Chumak", "", 50);
            Student          B      = new Student("", "Adamenko", "", 50);
            BSTree <Student> bSTree = new BSTree <Student>(A);

            //act
            bSTree.Insert(B);
            BSTreeNode <Student> nodeRes = bSTree.Root.Left;
            Student inserted             = nodeRes.Data;

            //assert
            Assert.AreEqual(50, inserted.TestResult);
        }
Ejemplo n.º 23
0
        public void TestMethod2()
        {
            BSTree bt = new BSTree();

            int[] arr1 = { 11, 3, 54, 6, 42, 95, 2, 45, 24, 23, 34 };

            for (int i = 0; i < arr1.Length; i++)
            {
                bt.insert(arr1[i]);
            }

            Node parent = bt.findParent(34);

            Assert.AreEqual(24, parent.value);
        }
Ejemplo n.º 24
0
        public void Insert_InsertStudentBAsLeftChildOfStudentA_ShouldReturnRootLeftInsertedNodeWithResult40()
        {
            //arrange
            Student          A      = new Student("", "", "", 50);
            Student          B      = new Student("", "", "", 40);
            BSTree <Student> bSTree = new BSTree <Student>(A);

            //act
            bSTree.Insert(B);
            BSTreeNode <Student> nodeRes = bSTree.Root.Left;
            Student inserted             = nodeRes.Data;

            //assert
            Assert.AreEqual(40, inserted.TestResult);
        }
Ejemplo n.º 25
0
        public void Find_StudentDataInTheTree_ShouldReturnNodeWithStudentDataAndTestResult70()
        {
            //arrange
            Student          A      = new Student("", "", "", 50);
            Student          B      = new Student("", "", "", 30);
            Student          C      = new Student("", "", "", 70);
            BSTree <Student> bSTree = new BSTree <Student>(A);

            bSTree.Insert(B);
            bSTree.Insert(C);
            //act
            Student res = bSTree.FindNode(C).Data;

            //assert
            Assert.AreEqual(res, C);
        }
Ejemplo n.º 26
0
        public void TestMethod1()
        {
            BSTree bt = new BSTree();

            // Added the values in an array so it would be easier to create the tests
            int[] arr1 = { 11, 3, 54, 6, 42, 95, 2, 45, 24, 23, 34 };

            for (int i = 0; i < arr1.Length; i++)
            {
                bt.insert(arr1[i]);
            }

            Node parent = bt.findParent(45);

            Assert.AreEqual(42, parent.value);
        }
Ejemplo n.º 27
0
        public void GetNode_Should_ReturnNode()
        {
            BSTree <int> tree = new BSTree <int>();

            tree.Add(10);
            tree.Add(5);
            tree.Add(7);
            tree.Add(9);
            tree.Add(20);
            tree.Add(6);
            tree.Add(1);
            tree.Add(13);
            tree.Add(53);
            tree.Add(100);
            Assert.True(tree.GetNode(13).Key == 13);
        }
Ejemplo n.º 28
0
    static public void Main()
    {
        // build a binary search tree
        IBSTree aBSTree = new BSTree();

        aBSTree.Insert('M');
        aBSTree.Insert('D');
        aBSTree.Insert('G');
        aBSTree.Insert('A');
        aBSTree.Insert('W');
        aBSTree.Insert('P');

        // pre-order traversal
        aBSTree.PreOrderTraverse();
        // in-order traversal
        aBSTree.InOrderTraverse();
        // post-order traversal
        aBSTree.PostOrderTraverse();

        // delete a leaf A
        aBSTree.Delete('A');

        // pre-order traversal
        aBSTree.PreOrderTraverse();
        // in-order traversal
        aBSTree.InOrderTraverse();
        // post-order traversal
        aBSTree.PostOrderTraverse();

        // put A back aBStree
        aBSTree.Insert('A');
        // delete a node W, which has only one child
        aBSTree.Delete('W');

        // pre-order traversal
        aBSTree.PreOrderTraverse();
        // in-order traversal
        aBSTree.InOrderTraverse();
        // post-order traversal
        aBSTree.PostOrderTraverse();

        // clear the binary tree
        aBSTree.Clear();

        // pre-order traversal
        aBSTree.PreOrderTraverse();
    }
Ejemplo n.º 29
0
        //创建二叉树
        static BSTree CreateBST(List<int> list)
        {
            //构建根节点
            BSTree bsTree = new BSTree();
            {
                bsTree.data = list[0];
                bsTree.left = null;
                bsTree.right = null;
            };

            for (int i = 1; i < list.Count; i++)
            {
                bool isExcute = false;
                InsertBST(bsTree, list[i], ref isExcute);
            }
            return bsTree;
        }
    private void Start()
    {
        BSTree tree = new BSTree();

        int[] data = { 62, 58, 88, 47, 73, 99, 35, 51, 93, 37 };
        for (int i = 0; i < data.Length; i++)
        {
            tree.Add(data[i]);
        }
        tree.MiddleTraversal();
        //Debug.Log(tree.Find(99));
        //Debug.Log(tree.Find(100));
        tree.Delete(73);
        tree.MiddleTraversal();
        tree.Delete(99);
        tree.MiddleTraversal();
    }
Ejemplo n.º 31
0
        ///<summary>
        /// 创建二叉排序树
        ///</summary>
        ///<param name="list"></param>
        static BSTree CreateBST(List <int> list)
        {
            //构建BST中的根节点
            BSTree bsTree = new BSTree()
            {
                Data  = list[0],
                Left  = null,
                Right = null
            };

            for (int i = 1; i < list.Count; i++)
            {
                bool isExcute = false;
                InsertBST(bsTree, list[i], ref isExcute);
            }
            return(bsTree);
        }
        public void Init()
        {
            Country[] countries;
            countries = CountryParser.GetCountries(URL);

            USA    = countries[0];
            Canada = countries[1];
            UK     = countries[8];
            Russia = countries[12];
            China  = countries[18];

            BSTCountry = new BSTree <string, Country>(USA.Name, USA);
            BSTCountry.Create(Canada.Name, Canada);
            BSTCountry.Create(UK.Name, UK);
            BSTCountry.Create(Russia.Name, Russia);
            BSTCountry.Create(China.Name, China);
        }
    public static void Execute()
    {
        BSTree<int> tree = new BSTree<int>();

        tree.Insert(new BSTNode<int>(5));
        tree.Insert(new BSTNode<int>(10));
        tree.Insert(new BSTNode<int>(8));
        tree.Insert(new BSTNode<int>(20));
        tree.Insert(new BSTNode<int>(2));
        tree.Insert(new BSTNode<int>(100));
        tree.Insert(new BSTNode<int>(1));
        tree.Insert(new BSTNode<int>(3));
        tree.Insert(new BSTNode<int>(80));
        tree.Insert(new BSTNode<int>(7));
        tree.Insert(new BSTNode<int>(19));
        tree.Insert(new BSTNode<int>(9));
        tree.Insert(new BSTNode<int>(110));

        CustomStack<BSTNode<int>> stack = new CustomStack<BSTNode<int>>();

        stack.Push(new linkedNode<BSTNode<int>>(tree.root));

        while (stack.head != null)
        {
            BSTNode<int> cursor = stack.Pop().Data;
            if (cursor == null)
                break;
            Console.WriteLine(cursor.Data);
            BSTNode<int> n = cursor.right;
            if (n != null)
                stack.Push(new linkedNode<BSTNode<int>>(n));
            n = cursor.left;
            if(n!=null)
                stack.Push(new linkedNode<BSTNode<int>>(n));

        }

        stack.Traverse();
    }
Ejemplo n.º 34
0
        //二叉树的插入操做
        //ref定义一个引用传递,与C++中引用传递类似,和实际值使用同一个地址,对它的修改就是对实际值得修改
        static void InsertBST(BSTree bsTree, int key, ref bool isExcute)
        {
            if (bsTree == null)
                return;
            //如果父节点大于key,则遍历左子树
            if(bsTree.data > key)
                InsertBST(bsTree.left, key, ref isExcute);
            else
                InsertBST(bsTree.right, key, ref isExcute);

            if (!isExcute)
            {
                //构建当前节点
                BSTree current = new BSTree();
                {
                    current.data = key;
                    current.left = null;
                    current.right = null;
                };
                if (bsTree.data > key)
                    bsTree.left = current;
                else
                    bsTree.right = current;
                isExcute = true;
            }
        }
Ejemplo n.º 35
0
        //在排序二叉树中搜索指定节点
        static bool SearchBST(BSTree bsTree, int key)
        {
            if (bsTree == null)
                return false;
            if (bsTree.data == key)
                return true;

            if(bsTree.data > key)
                return SearchBST(bsTree.left, key);
            else
                return SearchBST(bsTree.right, key);
        }
Ejemplo n.º 36
0
 //中序排列二叉树
 static void InOrderR_BST(BSTree bsTree)
 {
     if (bsTree != null)
     {
         //遍历左子树
         InOrderR_BST(bsTree.left);
         //输出节点数据
         Console.WriteLine(bsTree.data + "");
         //遍历右子书
         InOrderR_BST(bsTree.right);
     }
 }