CIS 250 - Binary trees

Prelude

There is a very good online reference for the C++ standard template library available at http://cplusplus.com/reference/stl/. We will look at the demonstration program TestContainers.cpp to see how some of the data structures we have been looking at can be used.

Overview

Binary trees are an important and basic data structure used throughout computer science. They are also the basis of many algorithms and therefore find common use in a wide variety of programs. One of their most important uses is to maintain data in a specific order while allowing very fast insertion, deletion, modification, and access to that data.

Objectives

Binary Trees

Binary trees are an efficient way of storing arbitrary amounts of data that has to be searched frequently or kept in sorted order even though there are many changes being made. Today we will discuss binary trees in general and look at pseudocode for implementing a binary search tree data structure. In this case, we will make the class more useful by utilizing Java generics from the very beginning so that we end up with a generalized binary tree class that handles any non-primitive data type. Primitive data types will be easily handled using this data structure by encapsulating each primitive data type in its respective wrapper class.

We will discuss how items are inserted into and removed from a binary search tree. We will also discuss how to search for an item in the tree and look at different ways of traversing a binary tree (preorder, inorder, postorder).

Pseudocode for a binary tree class is posted and will be updated if we make significant changes. We can use this in class as a starting point for writing our own binary tree implementation.

Terminology

Resources

complete binary tree diagram

Binary tree properties and traversal

Templated Binary Search Tree Class

We will develop a templated binary search tree class in C++ during class and it will be posted to these notes after we have finished it. We will also test it to demonstrate how it works.