2017-08-13 69 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; 

}