【问题标题】:C# binary tree data structure with static root node具有静态根节点的 C# 二叉树数据结构
【发布时间】:2017-08-13 19:49:15
【问题描述】:

我尝试为具有静态根值和 2 个子节点的静态二叉树创建数据结构。我试图使其对任意数量的子值都具有动态性。如何使用静态根节点来做到这一点。如果我采用 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; }

}

【问题讨论】:

  • 你想要什么样的二叉树?
  • 一棵完整的二叉树
  • 完整二叉树只有一个条件,即所有节点都有 0 或 2 个子节点。这意味着什么值将具有当前节点的后代子节点并不重要,但是您发布此if (myArray[i] &gt; rootNode.value)。那么你确定你想要完整的二叉树而不是完整的搜索二叉树吗?
  • 是的,我正在寻找那个
  • 嗯,方法很简单:1:为当前节点创建两个子节点; 2:然后通过步骤1的左子节点设置当前节点; 3:重复步骤1直到array.Legth &gt; 1;如您所见,您将检索所有节点都有 0 或 2 个子节点的完整二叉树。您只需要在代码中更改一个循环并在循环后将最后一个元素添加到当前节点

标签: c# data-structures tree binary-tree


【解决方案1】:

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

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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    相关资源
    最近更新 更多