2017-08-13 78 views
-1

我试图用静态根值和2个子节点为静态二叉树创建数据结构。我试图让它对任何数量的孩子价值都是动态的。我如何用静态根节点来做到这一点。如果我拿myArray = {3,11,8,18,21,36,1},我该如何执行。没有复杂代码更改的任何简单代码都会有所帮助。具有静态根节点的C#二叉树数据结构

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

你想要什么样的二叉树? –

+0

完整的二叉树 – Kurkula

+0

完整的二叉树只有一个条件,即所有节点都有0或2个孩子。这意味着什么值将具有当前节点的后代子节点并不重要,但是您发布了这个'if(myArray [i]> rootNode.value)'。那么你确定你想要完整的二叉树,但完整的搜索二叉树吗? –

回答

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; 

}