public static KarthicAryTree SetUpAryTreeWithThreeChildren() { KarthicAryTree tree = new KarthicAryTree(); KarthicAryTreeNode node1 = new KarthicAryTreeNode(1); KarthicAryTreeNode node2 = new KarthicAryTreeNode(2); KarthicAryTreeNode node3 = new KarthicAryTreeNode(3); KarthicAryTreeNode node4 = new KarthicAryTreeNode(4); KarthicAryTreeNode node5 = new KarthicAryTreeNode(5); KarthicAryTreeNode node6 = new KarthicAryTreeNode(6); KarthicAryTreeNode node7 = new KarthicAryTreeNode(7); KarthicAryTreeNode node8 = new KarthicAryTreeNode(8); KarthicAryTreeNode node9 = new KarthicAryTreeNode(9); tree.root = node1; node1.Children.Add(node2); node1.Children.Add(node3); node1.Children.Add(node4); //level 2 node2.Children.Add(node5); node2.Children.Add(node6); node4.Children.Add(node7); node4.Children.Add(node8); node4.Children.Add(node9); return(tree); }
public KarthicAryTree BuildAryTreeFromArray(int[] array, int k) { //int[] array has value..remember if the value is zero mean there is no node...there might be lot of empty value.. //bcoz all the node in n-ary may or may not have n children //build the root List <KarthicAryTreeNode> nodelist = new List <KarthicAryTreeNode>(); //we got to maintain the node list Dictionary <int, KarthicAryTreeNode> ht = new Dictionary <int, KarthicAryTreeNode>(); //ht key is index and value is tree node KarthicAryTree tree = new KarthicAryTree(); tree.root = new KarthicAryTreeNode(array[0]); //nodelist.Add(tree.root); ht.Add(0, tree.root); //we already build the root so start from the 1 for (int index = 1; index < array.Length; index++) { //only if the value is greater than 0 we build the tree if (array[index] > 0) { //get the parent index int parentindex = (index - 1) / k; //since we build level by level the parent node would already be present when the code comes here //get the parent node KarthicAryTreeNode parent = ht[parentindex]; //build child KarthicAryTreeNode child = new KarthicAryTreeNode(array[index]); //nodelist.Add(child); ht.Add(index, child); parent.Children.Add(child); } //else //{ // //Update 5/5/2015 // //If the code comes here mean an array[index] == 0 found // //that means no children exists for the given index // //to maintain the index of nodelist so that parent is maintained in right order // nodelist.Add(null); //} } return(tree); }
private void button1_Click(object sender, EventArgs e) { //Lets buid a n-ary tree ///////////////////1 ///////2///////////3//////////////////4 ///5//////6//////////////////////7///8////9 /// // For a k-ary tree with height h, the upper bound for the maximum number of leaves is k^h. //The height h of a k-ary tree does not include the root node, with a tree containing only a root node having a height of 0. //The total number of nodes in a perfect k-ary tree is (k^{h+1} - 1)/(k-1), while the height h is KarthicAryTree tree = TreeHelper.SetUpAryTreeWithThreeChildren(); string result = tree.BreathFirstTraversal(tree.root); //serialize the n-ary tree..convert to array and to bytes int[] array = ConvertAryTreeIntoString(tree); string filepath = this.textBox2.Text; byte[] buffer = GetBytes(array); //write the output in file StoreByteSIntofile(filepath, buffer); //deserialize the tree from byte[] or from file byte[] bufferfromfile = ConvertFileIntoByte(filepath); int[] arrayfrombyte = GetArrayFromByte(bufferfromfile); KarthicAryTree tree2 = BuildAryTreeFromArray(arrayfrombyte, 3); string result2 = tree2.BreathFirstTraversal(tree2.root); this.textBox1.Text = result2; bool output = result.Equals(result2); }
public int[] ConvertAryTreeIntoString(KarthicAryTree tree) { //Logic //we need to do level by level traversal and store the value in array using below formula // cth children = k * i + 1 + c //where k is the maximum number of children the node can have //i is the index of the parent //c is the index of the children relative to parent (1st children index o, 2nd children index 1) // parent index = i - 1 / k // where i is the index of the child and k is the k-ary number //how to know the size of the array.. //i guess the interview will give the no of nodes in n-ary tree OR //the interview won't give you anything //If the interviewer giving no info about the no of nodes or height, we can calculate no of nodes by height //No of nodes of any tree (binary or n-ary) //No. of node = k^(h+1) -1 / (k -1) where k is 2 for binary and h is height of the tree //calculate the no of nodes using the forumula // No. of node = k^(h+1) -1 / (k -1) //This is the same formula for binary tree as well..for binary tree k = 2 so the formula becomes //2^(h+1) -1 //Get the height of the n-ary tree int height = tree.GetHeight(tree.root); //for eg 3-ary tree (0 to 3 children) // (k^{h+1} - 1) / (k - 1) int k = 3; //For 3-ary tree int totalnodes = (int)Math.Ceiling(Math.Pow(k, (height + 1)) - 1) / (k - 1); int[] array = new int[totalnodes]; //each node can have max of 3 children..so take the max possible size int index = 0; array[index] = tree.root.Data; Queue <AryNodeWithIndex> myqueue = new Queue <AryNodeWithIndex>(); //the root's index is 0 myqueue.Enqueue(new AryNodeWithIndex(tree.root, 0)); while (myqueue.Count != 0) { AryNodeWithIndex current = myqueue.Dequeue(); int parentindex = current.index; //for every parent we start from 0 int childnumber = 0; foreach (KarthicAryTreeNode child in current.node.Children) { int childindex = k * parentindex + 1 + childnumber; array[childindex] = child.Data; childnumber += 1; //visit child myqueue.Enqueue(new AryNodeWithIndex(child, childindex)); } } return(array); }