kotlin-coding-challenges

Binary Search Tree (insert)

Instructions

Having binary node (data, left, right) we need to implement two methods binary search tree methods:

Requirements that are always true for any given node in Binary Search Tree:

Challenge Solution

Examples

Example 1

val tree = Node(1)
tree.insert(4)
tree.insert(2)

// Result
//
//    1
//   / \
//  2   4

Example 2

val tree = Node(1)
tree.insert(4)
tree.insert(2)
tree.contains(4) // true
tree.contains(99) // false