Ejemplo n.º 1
0
        public static CartesianTreeNode BuildCartesian(int[] inputArray)
        {
            var root    = new CartesianTreeNode(null, 0, inputArray[0]);
            var current = root;

            for (int i = 1; i < inputArray.Length; i++)
            {
                var val = inputArray[i];

                while (current.value > val && current != null)
                {
                    current = current.Up;
                }

                var node = new CartesianTreeNode(current, i, val);
                if (current != null)
                {
                    node.Left = current.Right;
                    if (node.Left != null)
                    {
                        node.Left.Up = node;
                    }
                    current.Right = node;
                }
                else
                {
                    node.Left = root;
                    root.Up   = node;
                    root      = node;
                }

                current = node;
            }

            root.UpdateDepth(0);
            return(root);
        }
Ejemplo n.º 2
0
 public CartesianTreeNode(CartesianTreeNode up, int index, int value)
 {
     Up         = up;
     this.index = index;
     this.value = value;
 }