Trees/Binary Trees


Trees

Definition of a tree: a general tree is a finite set of elements called nodes (vertices) and a finite set of directed edges (arcs) that connect pairs of nodes. If a tree is not empty then one node, called the root, has no incoming edges, and every other node has one incoming edge.

Tree is a recursive data structure. Every node in a tree can be viewed as the root of its own tree. This is a subtree: a node and all its descendents. Here is a recursive definition of a tree:

Definition of a tree: A general tree is a set T which is either empty or consists of one or more nodes such that T is partitioned into disjoint subsets

Terminology

Examples

Inheritance Tree

java inheritance tree for throwable

Expression Tree

expression tree

Game Tree

game tree for end of tic tac toe

Binary Trees

A binary tree is a tree where a node has at most 2 children. A recursive definition similar to the above can be used for binary trees:

Definition of a binary tree A binary tree is a set T which is either empty or consists of one or more nodes such that T is partitioned into disjoint subsets

representation of binary tree as root, left subtree, and right subtree

Here are some additional definitions that pertain to binary trees.

Draw the full binary tree of heights 1, 2, 3, and 4. Draw a complete binary tree (one which is not full) of heights 3 and 4.
























For each of the following, decide whether it is balanced or not:

Tree Traversals

We just studied iterators to visit every element in a list. As a linear data structure, it's easy to vist every element in a list; just start at the beginning and follow the links to the end. For a tree, the traversal order is not as obvious. The traversals we will study here use the edges of the tree. In describing our traversal we will mention "visiting" a node. Visiting means that we process the node (print its data, update its data, etc). Because we will have to go up and down the tree to reach all nodes we will access a node at other times as we move around the tree, but we will only "visit" it once. We have three standard traversal orders:

Notice that the traversals are defined recursively! In each traversal, to process the subtrees you use the same order. Consider the following tree.

Preorder:

  1. visit node 1
  2. preorder TL of 1: visit node 2
  3. preorder TL of 2: visit node 4
  4. preorder TR of 2: visit node 5
  5. preorder TR of 1: visit node 3
  6. preorder TL of 3: visit node 6
  7. preorder TR of 3: visit node 7

This results in the following order: 1 2 4 5 3 6 7

Inorder:

  1. inorder TL of 1 (node 2): inorder TL of 2: visit node 4
  2. visit node 2
  3. inorder TR of 2: visit node 5
  4. visit node 1
  5. inorder TR of 1 (node 3): inorder TL of 3: visit node 6
  6. visit node 3
  7. inorder TR of 3: visit node 7

This results in the following order: 4 2 5 1 6 3 7

Postorder:

  1. postorder TL of 1 (node 2): postorder TL of 2: visit node 4
  2. postorder TR of 2: visit node 5
  3. visit node 2
  4. postorder TR of 1 (node 3): postorder TL of node 3: visit node 6
  5. postorder TR of 3: visit node 7
  6. visit node 3
  7. visit node 1

This results in the following order: 4 5 2 6 7 3 1

Perform preorder, inorder, and postorder traversals of the following tree:















Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2017