Lab 13: Binary Search Trees

Tree

Child and Parent

Leaves

Sibling

Binary Tree

Left Complete Tree

Tree Traversals

Pre-order (NLR)

In-order (LNR)

Post-order (LRN)

Binary Search Tree (BST)

Insert X

Recursively do:

If found (X == node), we can either insert a a duplicated value or ignore it.
Otherwise insert X at the last spot.

Delete

Case 1: node n is a leaf (no children)

Case 2: node n has only one child

Case 3: node n has two children


Lab Task

An incomplete binary search tree implementation (TreeNode.h and Tree.h) is given to you. Your task is to complete the parts commented with "TO-DO" in Tree.h ONLY:

You are NOT allowed to make any changes to Treenode.h.

Test your Tree implementation with main.cpp.

Here is the expected output:


=== Enter 10 integer values ===

Preorder traversal
1 0 2 3 88 4 6 22 21 10
Inorder traversal
0 1 2 3 4 6 10 21 22 88
Postorder traversal
0 10 21 22 6 4 88 3 2 1

There are 9 levels in this binary tree

Search for 5:
Comparing 5 to 1; larger, walk right
Comparing 5 to 2; larger, walk right
Comparing 5 to 3; larger, walk right
Comparing 5 to 88; smaller, walk left
Comparing 5 to 4; larger, walk right
Comparing 5 to 6; smaller, walk left
Element was not found

Search for 22:
Comparing 22 to 1; larger, walk right
Comparing 22 to 2; larger, walk right
Comparing 22 to 3; larger, walk right
Comparing 22 to 88; smaller, walk left
Comparing 22 to 4; larger, walk right
Comparing 22 to 6; larger, walk right
Comparing 22 to 22; search complete
22 was found