Show the binary search tree built by adding numbers in this

Show the binary search tree built by adding numbers in this specific order, assuming the graph is empty to start with: 50, 10, 90, 14, 62, 71, 42, 6.

Solution

bool BinarySearchTree::add(int value) {

      if (root == NULL) {

            root = new BSTNode(value);

            return true;

      } else

            return root->add(value);

}

bool BSTNode::add(int value) {

      if (value == this->value)

            return false;

      else if (value < this->value) {

            if (left == NULL) {

                  left = new BSTNode(value);

                  return true;

            } else

                  return left->add(value);

      } else if (value > this->value) {

            if (right == NULL) {

                  right = new BSTNode(value);

                  return true;

            } else

                  return right->add(value);

      }

      return false;

}

Show the binary search tree built by adding numbers in this specific order, assuming the graph is empty to start with: 50, 10, 90, 14, 62, 71, 42, 6.Solutionboo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site