09.BINARY TREES

SYNTAX

template<class Type>
struct Node
{
  Type info;
  Node<Type> *lLink;
  Node<Type> *rLink;
};
  • Circle represents a "node"

  • Each node will have three fields: an information and two pointers (L/R)

  • A node will have information such as "A"

  • Nodes can have a pointer to the left (lLink) and a pointer to the right (rLink)

DEFINITIONS

DATA STRUCTURES MAIN OPERATIONS

  • the three main priority for a programmer when defining data structures are:

    • insert

    • delete

    • search

Binary Trees

  • A binary tree, T, is either empty or such that

    • T has a special node called the root node

    • T has two sets of nodes, TsubL and TsubR (left subtree and right subtree of T)

    • TsubL and TsubR are binary trees

BINARY TREES, ARRAYS, & LINKED LISTS COMPARISON

  • when organizing data, a highest priority is to insert, delete, and search data as fast as possible

    • Arrays: Random access data structure (by index)

      • +Item search can be fast (mainly if the data is sorted: binary search)

      • -Item insertion and deletion are time consuming (data shifting)

    • Linked Lists: Access by pointers

      • +Faster in insertion and deletion (update some pointers)

      • -Sequential search (must begin with the first item)

    • Binary Trees

      • +More efficient in item insertion, deletion and search

BINARY TREES VISUALIZATION

  • A is the root of T

  • B is the left child of A (C is the right child of A)

  • A is the parent of B and C

  • TsubL = {B, D, E, G}, TsubR = {C, F, H}, B is the root of TsubL

BINARY TREE W/ ONE NODE

BINARY TREE W/ TWO NODES

BINARY TREE W/ THREE NODES

LEAF

  • a node in a tree with no left and right children

PARENT

  • a node U is called the parent of a node V if there is an edge (branch) from U to V

PATH

  • a path from a node X to a node Y in a binary tree is a sequence of nodes

LEVEL

  • the level of a node in a binary tree is the number of branches on the path from the root to the node

HEIGHT

  • the height of a binary tree is the number of nodes on the longest path from the root to a leaf

BINARY TREE TRAVERSAL

  • the item insertion, deletion, and search operations require that the binary tree be traversed

  • the traversal must start from the root (as we have a pointer to it)

  • for each node, we have two choices

    • visit the node first

    • visit the subtrees first

  • the above two choices lead to three different traversals: inorder, preorder, & postorder traversals

INORDER TRAVERSAL

  • the binary tree is traversed as follows:

    • traverse the left subtree (recursive call)

    • visit the current root

    • traverse the right subtree (recursive call)

PREORDER TRAVERSAL

  • the binary tree is traversed as follows:

    • visit the current root

    • traverse the left subtree (recursive call)

    • traverse the right subtree (recursive call)

POSTORDER TRAVERSAL

  • the binary tree is traversed as follows:

    • traverse the left subtree (recursive call)

    • traverse the right subtree (recursive call)

    • visit the currrent root

BINARY SEARCH TREE T

  • T is either empty or:

    • T has a special node called the root node

    • T has two sets of nodes, TsubL and TsubR, called left subtree and right subtree of T

    • The value in the root node is larger than every value in the left subtree and smaller than every value in the right subtree

    • TsubL and TsubR are binary search trees

BINARY SEARCH TREES OPERATIONS (FUNCTIONS)

  • determine whether the binary tree is empty

  • find the height of the binary tree

  • find the number of nodes in the binary tree

  • find the number of leaves in the binary tree

  • traverse the binary tree

IMPLEMENTATION

UML

VISUALIZATION

SOURCE

HEADER FILE: binaryTree.h

The data members of the class are divided into public, protected, and private. All the public functions have no parameters. The parameters are specified in the private functions.

  • the public members are to be used outside the class to handle the binary tree as an ADT

  • the protected member(the pointer to the root of the tree) is to be used to derive special binary trees

  • the private members are used to implement the public functions. the user of the class doesn't need to access them

HEADER FILE: binaryTreeImplementation.h

Last updated