public int GetHeight(KarthicAryTreeNode root)
        {
            //Height of root node is 0
            //Height of 1st level is 1

            if (root == null)
            {
                return(0);
            }

            //if there is a leaf node then return 0
            if (root.Children.Count == 0)
            {
                return(0);
            }

            int maxheight = 0;

            foreach (KarthicAryTreeNode child in root.Children)
            {
                maxheight = Math.Max(maxheight, GetHeight(child));
            }

            return(maxheight + 1);
        }
Exemple #2
0
        public string BreathFirstTraversal(KarthicAryTreeNode root)
        {
            if (root == null)
            {
                return(string.Empty);
            }

            StringBuilder sb = new StringBuilder();

            Queue <KarthicAryTreeNode> myqueue = new Queue <KarthicAryTreeNode>();

            myqueue.Enqueue(root);
            sb.Append(root.Data).Append(",");

            while (myqueue.Count != 0)
            {
                KarthicAryTreeNode current = myqueue.Dequeue();

                foreach (KarthicAryTreeNode child in current.Children)
                {
                    //visit child
                    sb.Append(child.Data).Append(",");
                    myqueue.Enqueue(child);
                }
            }

            return(sb.ToString());
        }
        public string DepthFirstSearch(KarthicAryTreeNode root)
        {
            StringBuilder sb = new StringBuilder();

            if (root == null)
            {
                return(string.Empty);
            }

            //root is not null..visit the current
            sb.Append(root.Data);

            //check for leaf node  //End of child marker
            if (root.Children.Count == 0)
            {
                sb.Append('(');
                return(sb.ToString());
            }

            //It is not leaf node..then go depth first search
            foreach (KarthicAryTreeNode child in root.Children)
            {
                sb.Append(DepthFirstSearch(child));
            }

            //End of child marker
            sb.Append('(');

            return(sb.ToString());
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        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);
        }
Exemple #6
0
 public void AddNode(KarthicAryTreeNode node)
 {
 }
Exemple #7
0
 public AryNodeWithIndex(KarthicAryTreeNode node, int index)
 {
     this.node  = node;
     this.index = index;
 }