Insert in a BST Z89043


Statement
 

pdf   zip   tar

thehtml

A binary search tree (BST) is a binary tree such that for each node n in the tree the elements in the left subtree of the tree rooted at n are less than the element at node n, and the elements of the right subtree of the tree rooted at n are greater than the element at the node n.

Write a program that reads a sequence of integer numbers and builds a binary search tree by inserting the numbers in the sequence in an empty tree. The numbers are inserted in the order they appear in the sequence. If a number is already in the tree, insertion has no effect in the tree, otherwise the number is inserted in a spot that guarantees that the resulting tree is a BST.

The program must write the BST resulting from inserting the numbers in the input sequence in pre-order.

Public test cases
  • Input

    30
    10
    50
    0
    100
    15
    50
    120
    30
    110
    

    Output

    Pre-order traversal: 30 10 0 15 50 100 120 110
    
  • Information
    Author
    Language
    English
    Official solutions
    Python
    User solutions
    Python