Beispiel #1
0
        public static TreeNode Build()
        {
            TreeNode a = new TreeNode(), b = new TreeNode(), c = new TreeNode(), d = new TreeNode(), e = new TreeNode(), f = new TreeNode(), g = new TreeNode();
            TreeNode h = new TreeNode(), i = new TreeNode(), j = new TreeNode(), k = new TreeNode(), l = new TreeNode(), m = new TreeNode(), n = new TreeNode(), o = new TreeNode();

            a.data = "A";
            b.data = "B";
            c.data = "C";
            d.data = "D";
            e.data = "E";
            f.data = "F";
            g.data = "G";
            h.data = "H";
            i.data = "I";
            j.data = "J";
            k.data = "K";
            l.data = "L";
            m.data = "M";
            n.data = "N";
            o.data = "O";

            a.left = b;
            a.right = c;
            b.parent = c.parent = a;

            b.left = d;
            b.right = e;
            d.parent = e.parent = b;

            c.left = f;
            c.right = g;
            f.parent = g.parent = c;

            d.left = h;
            d.right = i;
            h.parent = i.parent = d;

            e.left = j;
            e.right = k;
            j.parent = k.parent = e;

            f.left = l;
            f.right = m;
            l.parent = m.parent = f;

            g.left = n;
            g.right = o;
            n.parent = o.parent = g;

            Console.WriteLine(string.Format("Next {0} = {1}", d.data, FindNext(d).data));

            return a;
        }
        public static List<List<TreeNode>> CreateLevelLists(TreeNode root)
        {
            Queue<TreeNode> queue = new Queue<TreeNode>();
            queue.Enqueue(root);

            List<List<TreeNode>> superList = new List<List<TreeNode>>();
            List<TreeNode> currentList = new List<TreeNode>();
            superList.Add(currentList);
            TreeNode levelFirst = null;

            while (queue.Count > 0)
            {
                //process one code
                TreeNode node = queue.Dequeue();

                //enqueue children
                if (node.left != null) queue.Enqueue(node.left);
                if (node.right != null) queue.Enqueue(node.right);

                if (levelFirst == null || levelFirst == node)
                {
                    //current node is first on the level
                    //create a new list
                    if (levelFirst == node)
                    {
                        currentList = new List<TreeNode>();
                        superList.Add(currentList);
                    }

                    //properly set the levelFirst
                    if (node.left != null)
                    {
                        levelFirst = node.left;
                    }
                    else if (node.right != null)
                    {
                        levelFirst = node.right;
                    }
                    else
                    {
                        //FIXME!!! first doesn't have children, need to make the next children the next
                        //if node doesn't have children, the next node will be the first on the level
                        levelFirst = null;
                    }

                }

                //add the node to the current list
                currentList.Add(node);
            }

            return superList;
        }
        public static void Test()
        {
            TreeNode a = new TreeNode(), b = new TreeNode(), c = new TreeNode(), d = new TreeNode(), e = new TreeNode(), f = new TreeNode(), g = new TreeNode();
            TreeNode h = new TreeNode(), i = new TreeNode(), j = new TreeNode(), k = new TreeNode(), l = new TreeNode(), m = new TreeNode(), n = new TreeNode(), o = new TreeNode();

            a.data = "A";
            b.data = "B";
            c.data = "C";
            d.data = "D";
            e.data = "E";
            f.data = "F";
            g.data = "G";
            h.data = "H";
            i.data = "I";
            j.data = "J";
            k.data = "K";
            l.data = "L";
            m.data = "M";
            n.data = "N";
            o.data = "O";

            a.left = b;
            a.right = c;

            b.left = d;
            b.right = e;

            c.left = f;
            c.right = g;

            d.left = h;
            d.right = i;

            e.left = j;
            e.right = k;

            f.left = l;
            f.right = m;

            g.left = n;
            g.right = o;

            bool foundParent;
            TreeNode common = FindParent(a, l, m, out foundParent);

            Console.WriteLine("Common parent: " + common.data);
        }
Beispiel #4
0
        private static TreeNode DoConvert(ref ListNode head, int n)
        {
            if (n <= 0)
            {
                return null;
            }

            TreeNode left = DoConvert(ref head, n / 2);

            TreeNode root = new TreeNode() { Value = head.Value, Left = left };

            head = head.Next;

            root.Right = DoConvert(ref head, n - n / 2 - 1);

            return root;
        }
        public static TreeNode FindParent(TreeNode node, TreeNode nodeOne, TreeNode nodeTwo, out bool foundParent)
        {
            foundParent = false;

            if (node == null)
            {
                return null;
            }
            else if (node == nodeOne || node == nodeTwo)
            {
                return node;
            }
            else
              //if (
              //(FindParent(node.left, nodeOne, nodeTwo, out found) == nodeOne && FindParent(node.right) == nodeTwo) ||
              //(FindParent(node.right) == nodeOne && FindParent(node.left) == nodeTwo)
              //)
            {
                //if it as already been found on the left side
                TreeNode leftTree = FindParent(node.left, nodeOne, nodeTwo, out foundParent);
                if (foundParent) return leftTree;

                //if it as already been found on the right side
                TreeNode rightTree = FindParent(node.right, nodeOne, nodeTwo, out foundParent);
                if (foundParent) return rightTree;

                //if this is the common parent node
                if ((leftTree == nodeOne && rightTree == nodeTwo) || (rightTree == nodeOne && leftTree == nodeTwo))
                {
                    foundParent = true;
                    return node;
                }
                else if (leftTree == nodeOne || leftTree == nodeTwo)
                {
                    return leftTree;
                }
                else if (rightTree == nodeOne || rightTree == nodeTwo)
                {
                    return rightTree;
                }
            }

            return null;
        }
Beispiel #6
0
        public static TreeNode FindNext(TreeNode node)
        {
            int levelUp = 0;

            TreeNode parent = node.parent;

            while (true)
            {
                //node is root
                if (parent == null) return null;

                if (levelUp == 0)
                {
                    if (parent.left != null && parent.left != node && parent.right != node)//can't be left brother
                    {
                        return parent.left;
                    }

                    if (parent.right != null && parent.right != node)
                    {
                        return parent.right;
                    }

                    levelUp++;
                    parent = parent.parent;
                }
                else
                {
                    //go down a level
                    if (parent.right != null)
                    {
                        parent = parent.right;
                        levelUp--;
                    }
                    else
                    {
                        //increase another level
                        parent = parent.parent;
                        levelUp++;
                    }
                }

            }
        }
        public static TreeNode BuildTree()
        {
            TreeNode a = new TreeNode(), b = new TreeNode(), c = new TreeNode(), d = new TreeNode(), e = new TreeNode(), f = new TreeNode(), g = new TreeNode();
            TreeNode h = new TreeNode(), i = new TreeNode(), j = new TreeNode(), k = new TreeNode(), l = new TreeNode(), m = new TreeNode(), n = new TreeNode(), o = new TreeNode();

            a.data = "A";
            b.data = "B";
            c.data = "C";
            d.data = "D";
            e.data = "E";
            f.data = "F";
            g.data = "G";
            h.data = "H";
            i.data = "I";
            j.data = "J";
            k.data = "K";
            l.data = "L";
            m.data = "M";
            n.data = "N";
            o.data = "O";

            a.left = b;
            a.right = c;

            b.left = d;
            b.right = e;

            c.left = f;
            c.right = g;

            d.left = h;
            d.right = i;

            e.left = j;
            e.right = k;

            f.left = l;
            f.right = m;

            g.left = n;
            g.right = o;

            return a;
        }