Ejemplo n.º 1
0
 private void add(TreeNode node, TreeNode current)
 {
     if (node.value < current.value)
     {
         if (current.nodesOnLeft == 0)
         {
             current.nodesOnLeft++;
             current.AddLeft(node);
             return;
         }
         else
         {
             current.nodesOnLeft++;
             add(node, current.left);
         }
     }
     else
     {
         if (current.nodesOnRight == 0)
         {
             current.nodesOnRight++;
             current.AddRight(node);
             return;
         }
         else
         {
             current.nodesOnRight++;
             add(node, current.right);
         }
     }
 }
Ejemplo n.º 2
0
        public static void CheckBinarySearchTreeMain()
        {
            TreeNode tree = new TreeNode(20);

            tree.AddLeft(new TreeNode(10));
            tree.left.AddRight(new TreeNode(15));
            tree.AddRight(new TreeNode(30));
            bool isBinarySearchTree = IsBinarySearchTree(tree);
        }
Ejemplo n.º 3
0
        public static void CheckSubtreeMain()
        {
            TreeNode treeNode  = TreeNode.GenerateBinaryTreeWithDepth(5);
            TreeNode treeNode2 = new TreeNode();

            TreeNode.CloneTreeFromNode(treeNode.right.left.right, treeNode2);
            treeNode2.AddLeft(new TreeNode(40));
            bool isSubTreePresent = IsSubtree(treeNode, treeNode2);
        }
Ejemplo n.º 4
0
        /*
         * Returns
         *      10
         * 15              50
         * 20      25      35      45
         */
        private TreeNode <int> BuildBunchOfIntNodes()
        {
            var root = new TreeNode <int>(10);
            var left = root.AddLeft(15);

            left.AddLeft(20);
            left.AddRight(25);
            var right = root.AddRight(50);

            right.AddLeft(35);
            right.AddRight(45);

            return(root);
        }
Ejemplo n.º 5
0
        public void NodeInsertion()
        {
            TreeNode root  = new TreeNode(0, values, "A");
            TreeNode node1 = new TreeNode(1, values1, "B");
            TreeNode node2 = new TreeNode(1, values2, "C");
            TreeNode node3 = new TreeNode(1, values3, "D");
            TreeNode node4 = new TreeNode(1, values4, "E");

            root.AddLeft(node1);
            node1.AddLeft(node2);
            node1.AddRight(node3);
            root.AddRight(node4);

            Assert.AreEqual(root.left.GetValues()[0], 2);
            Assert.AreEqual(root.left.left.GetValues()[0], 4);
            Assert.AreEqual(root.right.GetValues()[1], 5);
            Assert.AreEqual(root.left.right.GetValues()[0], 0);
        }
Ejemplo n.º 6
0
    /* CutAxis
     * calculates random value in [X,Y,Z] axis to cut section(Cube)
     *  creating 2 sections(Cube s) and call GenerateCutRec
     *
     * parameters:
     *  Cube section: section(Cube) being considered for cut
     *  Axis axis: axis in which we are performing a cut
     *  TreeNode<Cube>: reference to BSP tree
     *
     * return:
     *  void
     */
    void CutAxis(Cube section, Enum.Axis axis, TreeNode <Cube> node)
    {
        int  value;
        Cube sectionLeft  = null;
        Cube sectionRight = null;

        if (axis == Enum.Axis.x)
        {
            value = Random.Range(section.x + minSectionX, section.disX - minSectionX);
            //DrawCut(Enum.Axis.x, value, section.y, section.z, section.disY, section.disZ, Color.red);

            sectionLeft  = new Cube(section.x, section.y, section.z, value, section.disY, section.disZ);
            sectionRight = new Cube(value, section.y, section.z, section.disX, section.disY, section.disZ);
        }
        else if (axis == Enum.Axis.y)
        {
            value = Random.Range(section.y + minSectionY, section.disY - minSectionY);
            //DrawCut(Enum.Axis.y, value, section.x, section.z, section.disX, section.disZ, Color.green);

            sectionLeft  = new Cube(section.x, section.y, section.z, section.disX, value, section.disZ);
            sectionRight = new Cube(section.x, value, section.z, section.disX, section.disY, section.disZ);
        }
        else if (axis == Enum.Axis.z)
        {
            value = Random.Range(section.z + minSectionZ, section.disZ - minSectionZ);
            //DrawCut(Enum.Axis.z, value, section.x, section.y, section.disX, section.disY, Color.blue);

            sectionLeft  = new Cube(section.x, section.y, section.z, section.disX, section.disY, value);
            sectionRight = new Cube(section.x, section.y, value, section.disX, section.disY, section.disZ);
        }
        else
        {
            Debug.LogError("Invalid axis number repesentation");
        }

        node.AddLeft(sectionLeft);
        node.AddRight(sectionRight);
        GenerateCutRec(sectionLeft, node.LeftNode);
        GenerateCutRec(sectionRight, node.RightNode);
    }
Ejemplo n.º 7
0
		public TreeTest(TraversalType traversalType)
		{
			Tree<string> tree;
			TreeNode<string> root;
			TreeNode<string> node;
			
			_FullTree = new Tree<string>();
			
			tree = _FullTree;
			tree.TraversalOrder = traversalType;
			root = new TreeNode<string>();
			root.Data = "A";
			tree.Root = root;
						
			node = root.AddLeft("B");
			node.AddLeft("C");
			node.AddRight("D");
			
			node = root.AddRight("E");
			node.AddLeft("F");
			node.AddRight("G");
			
			_LeftOnlyTree = new Tree<string>();
			
			tree = _LeftOnlyTree;
			tree.TraversalOrder = traversalType;
			root = new TreeNode<string>();
			root.Data = "A";
			tree.Root = root;
			
			node = root.AddLeft("B");
			node.AddLeft("C");
			
			_RightOnlyTree = new Tree<string>();
			
			tree = _RightOnlyTree;
			tree.TraversalOrder = traversalType;
			root = new TreeNode<string>();
			root.Data = "A";
			tree.Root = root;
			
			node = root.AddRight("B");
			node.AddRight("C");
			
			
			_MissingFinalRightTree = new Tree<string>();
			
			tree = _MissingFinalRightTree;
			tree.TraversalOrder = traversalType;
			root = new TreeNode<string>();
			root.Data = "A";
			tree.Root = root;
			
			node = root.AddLeft("B");
			node.AddLeft("C");
			
			node = root.AddRight("D");
			node.AddLeft("E");
			
			
			_MissingFinalLeftTree = new Tree<string>();
			
			tree = _MissingFinalLeftTree;
			tree.TraversalOrder = traversalType;
			root = new TreeNode<string>();
			root.Data = "A";
			tree.Root = root;
			
			node = root.AddLeft("B");
			node.AddRight("C");
			
			node = root.AddRight("D");
			node.AddRight("E");
		}