Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有静态根节点的C#二叉树数据结构_C#_Data Structures_Tree_Binary Tree - Fatal编程技术网

具有静态根节点的C#二叉树数据结构

具有静态根节点的C#二叉树数据结构,c#,data-structures,tree,binary-tree,C#,Data Structures,Tree,Binary Tree,我尝试为一个静态二叉树创建一个数据结构,其中包含一个静态根值和两个子节点。我试图使它对任何数量的子值都是动态的。如何使用静态根节点实现这一点。如果我使用myArray={3,11,8,18,21,36,1},我如何实现呢。任何没有复杂代码更改的简单代码都会有所帮助 class Program { static void Main(string[] args) { TreeNode rootNode = new TreeNode(); rootNod

我尝试为一个静态二叉树创建一个数据结构,其中包含一个静态根值和两个子节点。我试图使它对任何数量的子值都是动态的。如何使用静态根节点实现这一点。如果我使用myArray={3,11,8,18,21,36,1},我如何实现呢。任何没有复杂代码更改的简单代码都会有所帮助

class Program
{
    static void Main(string[] args)
    {
        TreeNode rootNode = new TreeNode();
        rootNode.value = 9;

        int[] myArray = { 3, 11 };

        for (int i = 0; i < myArray.Length; i++)
        {
            if (myArray[i] > rootNode.value)
            {
                //add to right node
                TreeNode right = new TreeNode();
                right.value = myArray[i];
                rootNode.rightNode = right;

            }
            else
            {
                //add to left node
                TreeNode left = new TreeNode();
                left.value = myArray[i];
                rootNode.leftNode = left;
            }
        }
    }
}

class TreeNode
{
    public int value { get; set; }
    public TreeNode leftNode { get; set; }
    public TreeNode rightNode { get; set; }

}
类程序
{
静态void Main(字符串[]参数)
{
TreeNode rootNode=新的TreeNode();
rootNode.value=9;
int[]myArray={3,11};
for(int i=0;irootNode.value)
{
//添加到右节点
TreeNode right=新的TreeNode();
right.value=myArray[i];
rootNode.rightNode=right;
}
其他的
{
//添加到左节点
TreeNode left=新的TreeNode();
left.value=myArray[i];
rootNode.leftNode=left;
}
}
}
}
三烯类
{
公共int值{get;set;}
公共树节点leftNode{get;set;}
公共树节点rightNode{get;set;}
}

嘿,这段代码运行正常,但如果需要,您可以进一步改进。对不起,我不得不说得有点长。希望你能理解代码。这并不难。顺便说一下,代码是用java编写的。只需将语法更改为c#

类程序
{
静态void Main(字符串[]参数)
{
扫描仪a=新的扫描仪(System.in);
System.out.println(“输入孩子的数量!”);
int input=a.nextInt();
TreeNode rootNode=新的TreeNode();
TreeNode父节点=根节点;
rootNode.value=9;
parent.childNodes=新树节点[input];
对于(int i=0;i对于(int i=0;i您想要什么类型的二叉树?完整的二叉树完整的二叉树只有一个条件,即所有节点都有0或2个子节点。这意味着什么值将有当前节点的后代子节点并不重要,但您发布此
if(myArray[i]>rootNode.value)
。那么你确定你想要完整的二叉树而不是完整的搜索二叉树吗?是的,我正在寻找它。很简单的方法是:1:为当前节点创建两个子节点;2:然后从步骤1开始按左个子节点设置当前节点;3:重复步骤1直到
array.Legth>1
;正如你所看到的,你将检索到一个完整的二叉树所有节点都有0或2个子节点。您只需在代码中对循环进行少量更改,并将最后一个元素添加到循环后的当前节点
class Program
{
    static void Main(string[] args)
    {
        Scanner a = new Scanner(System.in);
        System.out.println("Enter the number of childs!");
        int input = a.nextInt();
        TreeNode rootNode = new TreeNode();
        TreeNode parent = rootNode;
        rootNode.value = 9;

        parent.childNodes = new TreeNode[input];
        for(int i = 0; i< input; i++){
            parent.childNodes[i] = new TreeNode();
            parent.childNodes[i].value = 0;
        }
        parent.hasChild = true;
        int count = 1;
        int startingIndex = 0;
        int EndingIndex = input - 1;
        int next = 0;

        int[] myArray = { 19, 11, 12, 13 ,14, 15 };

        for (int i = 0; i < myArray.length; i++)
        {
            if(count <= input){
                if (myArray[i] > parent.value)
                {
                    //add to right node
                    parent.childNodes[EndingIndex].value = myArray[i];
                    EndingIndex--;
                    count++;
                }

                else
                {
                    //add to the left node
                    parent.childNodes[startingIndex].value = myArray[i];

                    startingIndex++;
                    count++;
                }
            }
            else{
                parent = parent.childNodes[next];
                parent.childNodes = new TreeNode[input];
                for(int j = 0; j< input; j++){
                    parent.childNodes[j] = new TreeNode();
                    parent.childNodes[j].value = 0;
                }
                parent.hasChild = true;
                next++;
                count = 1;
                i--;
                startingIndex = 0;
                EndingIndex = input - 1;
                next = 0;
            }

        }

        parent = rootNode;
        TreeNode grandparent = parent;
        System.out.println("root Node: " + parent.value);
        next = 0;
        int childs = 1;
        while(parent.hasChild == true){
            for(int i=0; i<input; i++){

                if(parent.childNodes[i].value != 0){
                    System.out.print("child " + childs + " : ");
                    childs++;
                    System.out.print(parent.childNodes[i].value);
                    System.out.println();
                }
            }
            childs = 1;
            System.out.println();
            parent = grandparent.childNodes[next];
            next++;
        }
    }
}

class TreeNode
{
    public int value;
    TreeNode[] childNodes;
    boolean hasChild = false;

}