Exemple #1
0
        /// <summary>
        /// Adds a node to a BST
        /// </summary>
        /// <remarks>Time Complexity: O(log(n))</remarks>
        public void Add(int val)
        {
            BstNode runner = this.Root;

            if (runner is null)
            {
                this.Root = new BstNode(val);
            }
            while (runner != null)
            {
                if (val < runner.Value)
                {
                    if (runner.Left is null)
                    {
                        runner.Left = new BstNode(val);
                        return;
                    }
                    runner = runner.Left;
                }
                else
                {
                    if (runner.Right is null)
                    {
                        runner.Right = new BstNode(val);
                        return;
                    }
                    runner = runner.Right;
                }
            }
        }
Exemple #2
0
 int Size(BstNode node)
 {
     if (node is null)
     {
         return(0);
     }
     return(1 + Size(node.Right) + Size(node.Left));
 }