Example #1
0
        public bool InsertNode(T data)
        {
            var node = BinaryTreeNode.CreateNode(data);

            if (Root == null)
            {
                Root = node;
                return(true);
            }

            var current = Root;

            while (current != null)
            {
                if (node > current || node < current)
                {
                    var left     = node < current;
                    var nextNode = GetBranch(current, left);
                    if (nextNode != null)
                    {
                        current = nextNode;
                        continue;
                    }

                    SetBranch(current, node, left);
                }
                else
                {
                    break;
                }
            }

            return(true);
        }
Example #2
0
        public BinarySearchTree(params T[] values)
        {
            if (values.Length == 0)
            {
                return;
            }

            Root = BinaryTreeNode.CreateNode(values[0]);
            for (var i = 1; i < values.Length; i++)
            {
                InsertNode(values[i]);
            }
        }