Exemple #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);
        }
Exemple #2
0
 public void UpdateDepth(int d)
 {
     depth = d;
     Left?.UpdateDepth(d + 1);
     Right?.UpdateDepth(d + 1);
 }