Data structures · 12 / 15 Exercise Hard

Binary search tree

Objective

Create a Python program that builds a binary search tree (BST) and queries it. In a BST, values smaller than a node go to its left and greater ones to its right.

Instructions

  • The first line contains the integers to insert, in that order and separated by spaces (duplicates are ignored). The second line contains the integers to search for.
  • Use a Node class with left and right, and recursive functions to insert and traverse.
  • Show Inorder: ... (in-order traversal, which comes out sorted), Height: h (number of levels; a single node has height 1), Minimum: m and Maximum: M.
  • Then, for each value to search, show X: yes or X: no.
Input
50 30 70 20 40 60 80 30
40 55 80
Output
Inorder: 20 30 40 50 60 70 80
Height: 3
Minimum: 20
Maximum: 80
40: yes
55: no
80: yes

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise