コード例 #1
0
        private static void prefixInfixToTree()
        {
            String prefix = "ABCDEFGH";
            string infix  = "BDCAFEHG";
            BNode  tree   = prefixInfixToTreeHelper(prefix, infix, 0, prefix.Length - 1, 0, infix.Length - 1);

            TreePrinter.Print(tree);
        }
コード例 #2
0
        private static void binaryTreetodll()
        {
            BNode root = new BNode();

            root.data = 5;
            BNode a = new BNode();

            a.data = 4;
            BNode b = new BNode();

            b.data = 8;
            BNode c = new BNode();

            c.data = 11;
            BNode d = new BNode();

            d.data = 13;
            BNode e = new BNode();

            e.data = 4;
            BNode f = new BNode();

            f.data = 7;
            BNode g = new BNode();

            g.data = 2;
            BNode h = new BNode();

            h.data = 5;
            BNode i = new BNode();

            i.data = 1;

            root.left  = a;
            root.right = b;
            a.left     = c;
            b.left     = d;
            b.right    = e;
            c.left     = f;
            c.right    = g;
            e.left     = h;
            e.right    = i;
            TreePrinter.Print(root);
            btol bto = treetodllHelper(root);
            var  cur = bto.head;

            while (cur != null)
            {
                Console.WriteLine(cur.data);
                cur = cur.right;
            }
            //TreePrinter.Print(bto.head);
        }
コード例 #3
0
        private static void pathsum()
        {
            BNode root = new BNode();

            root.data = 5;
            BNode a = new BNode();

            a.data = 4;
            BNode b = new BNode();

            b.data = 8;
            BNode c = new BNode();

            c.data = 11;
            BNode d = new BNode();

            d.data = 13;
            BNode e = new BNode();

            e.data = 4;
            BNode f = new BNode();

            f.data = 7;
            BNode g = new BNode();

            g.data = 2;
            BNode h = new BNode();

            h.data = 5;
            BNode i = new BNode();

            i.data = 1;

            root.left  = a;
            root.right = b;
            a.left     = c;
            b.left     = d;
            b.right    = e;
            c.left     = f;
            c.right    = g;
            e.left     = h;
            e.right    = i;

            int targetSum = 22;

            TreePrinter.Print(root);
            pathsumhelper(root, 0, targetSum, new List <int>());
        }
コード例 #4
0
        private static void flattenBinaryTree()
        {
            BNode root = new BNode();

            root.data = 5;
            BNode a = new BNode();

            a.data = 4;
            BNode b = new BNode();

            b.data = 8;
            BNode c = new BNode();

            c.data = 11;
            BNode d = new BNode();

            d.data = 13;
            BNode e = new BNode();

            e.data = 4;
            BNode f = new BNode();

            f.data = 7;
            BNode g = new BNode();

            g.data = 2;
            BNode h = new BNode();

            h.data = 5;
            BNode i = new BNode();

            i.data = 1;

            root.left  = a;
            root.right = b;
            a.left     = c;
            b.left     = d;
            b.right    = e;
            c.left     = f;
            c.right    = g;
            e.left     = h;
            e.right    = i;
            TreePrinter.Print(root);
            flattenHelper(root);
            TreePrinter.Print(root);
        }
コード例 #5
0
        private static void binarySearchTree()
        {
            BNode root = insertBST(null, 0);

            root = insertBST(root, 67);
            root = insertBST(root, 55);
            root = insertBST(root, 637);
            root = insertBST(root, 23);
            root = insertBST(root, 78);
            root = insertBST(root, 1);
            root = insertBST(root, 5);
            root = insertBST(root, 15);
            root = insertBST(root, 25);

            TreePrinter.Print(root);
            //root= deleteBST(root,5);
            //TreePrinter.Print(root);
            //root = mirrorBST(root);
            BNode ans = LeastCommonAns(root, 25, 15);

            TreePrinter.Print(root);
            BNode find = searchBST(root, 1);
        }
コード例 #6
0
        private static void zigzagLevelOrder()
        {
            BNode root = new BNode();

            root.data = 5;
            BNode a = new BNode();

            a.data = 4;
            BNode b = new BNode();

            b.data = 8;
            BNode c = new BNode();

            c.data = 11;
            BNode d = new BNode();

            d.data = 13;
            BNode e = new BNode();

            e.data = 4;
            BNode f = new BNode();

            f.data = 7;
            BNode g = new BNode();

            g.data = 2;
            BNode h = new BNode();

            h.data = 5;
            BNode i = new BNode();

            i.data = 1;

            root.left  = a;
            root.right = b;
            a.left     = c;
            b.left     = d;
            b.right    = e;
            c.left     = f;
            c.right    = g;
            e.left     = h;
            e.right    = i;


            TreePrinter.Print(root);

            Stack <BNode>      q   = new Stack <BNode>();
            Stack <BNode>      s   = new Stack <BNode>();
            List <List <int> > res = new List <List <int> >();

            q.Push(root);
            while (q.Count > 0 || s.Count > 0)
            {
                List <int> level = new List <int>();
                while (q.Count > 0)
                {
                    var cur = q.Pop();
                    level.Add(cur.data);
                    if (cur.left != null)
                    {
                        s.Push(cur.left);
                    }
                    if (cur.right != null)
                    {
                        s.Push(cur.right);
                    }
                }
                res.Add(level);

                level = new List <int>();
                while (s.Count > 0)
                {
                    var cur = s.Pop();
                    level.Add(cur.data);
                    if (cur.right != null)
                    {
                        q.Push(cur.right);
                    }
                    if (cur.left != null)
                    {
                        q.Push(cur.left);
                    }
                }
                res.Add(level);
            }


            foreach (var l in res)
            {
                foreach (var data in l)
                {
                    Console.Write("{0} ", data);
                }
                Console.WriteLine();
            }
        }