binary tree question

Label a binary tree with keys from the set {8, 21, 11, 16, 12, 1, 9}, so
that it is a legal binary search tree.
If I understand your question correctly this must be correct.

     8
    / \
   1   21
      /
     11
    /  \
   9    16
       /
     12


That would be the output if each element is inserted to the tree sequentially
Binary Search Tree's are sorted. cp http://en.wikipedia.org/wiki/Binary_search_tree

In computer science, a binary search tree (BST) is a node-based binary tree data structure which has the following properties:[1]

* The left subtree of a node contains only nodes with keys less than the node's key.
* The right subtree of a node contains only nodes with keys greater or equal than the node's key.
* Both the left and right subtrees must also be binary search trees.

To the OP: You shouldn't get too much trouble figuring out how it looks like with the given properties above. Tell us where you are stuck or at least show some effort. Just posting the question is not very polite

Ciao, Imi
Last edited on
There is more than one answer to the question. For good marks, OP should provide more than one tree. For top marks, justify why in a real program there could be more than one layout.
Topic archived. No new replies allowed.