コード例 #1
0
        private void button10_Click(object sender, EventArgs e)
        {
            //Lets buid a n-ary tree
            ///////////////////A
            ///////B///////////C//////////////////D
            ///E//////F//////////////////////G///H////I///J
            ///


            AryTree <char> tree = TreeHelper.SetAryTreewithMaxFourchildrens();

            //Serialization is done by using dfs end of children pointer...
            string result = tree.DepthFirstSearch(tree.root);



            string filepath = this.textBox2.Text;

            byte[] buffer = GetBytes(result);
            //write the output in file
            StoreByteSIntofile(filepath, buffer);

            //deserialize the tree from byte[] or from file

            byte[] bufferfromfile = ConvertFileIntoByte(filepath);
            char[] arrayfrombyte  = GetCharArrayFromByte(bufferfromfile);

            AryTree <char> tree2 = DeserializeTreeFromArray(arrayfrombyte, result.Length);

            string result2 = tree2.DepthFirstSearch(tree2.root);

            this.textBox1.Text = result2;

            bool output = result.Equals(result2);
        }
コード例 #2
0
        public static AryTree <char> SetAryTreewithMaxFourchildrens()
        {
            AryTree <char> tree = new AryTree <char>();

            AryTreeNode <char> node1 = new AryTreeNode <char>('A');
            AryTreeNode <char> node2 = new AryTreeNode <char>('B');
            AryTreeNode <char> node3 = new AryTreeNode <char>('C');
            AryTreeNode <char> node4 = new AryTreeNode <char>('D');
            AryTreeNode <char> node5 = new AryTreeNode <char>('E');
            AryTreeNode <char> node6 = new AryTreeNode <char>('F');
            AryTreeNode <char> node7 = new AryTreeNode <char>('G');
            AryTreeNode <char> node8 = new AryTreeNode <char>('H');
            AryTreeNode <char> node9 = new AryTreeNode <char>('I');

            AryTreeNode <char> node10 = new AryTreeNode <char>('J');
            AryTreeNode <char> node11 = new AryTreeNode <char>('K');


            //Lets buid a n-ary tree
            ///////////////////A
            ///////B///////////C//////////////////D
            ///E//////F//////////////////////G///H////I///J
            /////////K/////////////////////////////////////
            tree.root = node1;
            node1.Children.Add(node2);
            node1.Children.Add(node3);
            node1.Children.Add(node4);

            node2.Children.Add(node5);
            node2.Children.Add(node6);

            node4.Children.Add(node7);
            node4.Children.Add(node8);
            node4.Children.Add(node9);
            node4.Children.Add(node10);

            node6.Children.Add(node11);

            return(tree);
        }
コード例 #3
0
        private AryTree <char> DeserializeTreeFromArray(char[] array, int count)
        {
            if (array.Length == 0)
            {
                return(null);
            }

            AryTree <char> tree = new AryTree <char>();

            int index = 0;
            //Add first element to root
            Stack <AryTreeNode <char> > mystack = new Stack <AryTreeNode <char> >();
            AryTreeNode <char>          root    = new AryTreeNode <char>(array[index]);

            tree.root = root;

            mystack.Push(root);
            index++;

            AryTreeNode <char> parent = null;

            while (mystack.Count != 0 && index < count)
            {
                if (array[index] == '(')
                {
                    mystack.Pop();
                }
                else
                {
                    parent = mystack.Peek();
                    AryTreeNode <char> child = new AryTreeNode <char>(array[index]);
                    parent.Children.Add(child);
                    mystack.Push(child);
                }
                index++;
            }

            return(tree);
        }
コード例 #4
0
 public void AddNode(AryTree <T> node)
 {
 }