Solarion
Honorary Master
- Joined
- Nov 14, 2012
- Messages
- 28,051
- Reaction score
- 17,804
Hi guys. Please can you shed a little light on this output I get. I basically don't get what this series of numbers means. I can't grasp how it represents the whole node tree!
Output: 6 4 6 2 5 1 6 3 6
Source Article
C#:
using System;
public class Tree
{
Node root;
// Tree Node
public class Node
{
public int data;
public Node left, right;
public Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
// Function to insert nodes in level order
public Node insertLevelOrder(int[] arr,
Node root, int i)
{
// Base case for recursion
if (i < arr.Length)
{
Node temp = new Node(arr[i]);
root = temp;
// insert left child
root.left = insertLevelOrder(arr,
root.left, 2 * i + 1);
// insert right child
root.right = insertLevelOrder(arr,
root.right, 2 * i + 2);
}
return root;
}
// Function to print tree
// nodes in InOrder fashion
public void inOrder(Node root)
{
if (root != null)
{
inOrder(root.left);
Console.Write(root.data + " ");
inOrder(root.right);
}
}
// Driver code
public static void Main(String []args)
{
Tree t2 = new Tree();
int []arr = { 1, 2, 3, 4, 5, 6, 6, 6, 6 };
t2.root = t2.insertLevelOrder(arr, t2.root, 0);
t2.inOrder(t2.root);
}
}
Output: 6 4 6 2 5 1 6 3 6
Source Article
Last edited:

