CmSc 250 Intro to Algorithms


Trees

Learning Goals
Exam-like problems


  1. Definitions
    • Tree - a non-empty collection of nodes & edges
    • Node - can have a name and carry other associated information
    • Path - a list of distinct nodes in which successive nodes are connected by edges. Any two nodes must have one and only one path between them, else it is not a tree

    • Root - starting point (top) of the tree
    • Parent (ancestor) of a node A - the node "above" node A
    • Child (descendent) of a node A - the node "below" node A
    • Siblings - nodes that have same parent
    • Leaf (terminal node) - a node with no children
    • Level of a node - the number of edges between this node and the root
    • Depth of a node - the number of edges from the root to that node.
      The depth of the root is 0.
    • Height of a node - the largest number of edges from that node to a leaf.
      The height of each leaf is zero
    • Height of a tree - the longest path from the root to a leaf node.
    • Balanced tree - the difference between the height of the left subtree and the right subtree is not more than 1.
    • Ordered trees - The order of the children matters, i.e. children are listed in a particular order.

  2. Properties
    • Exactly one path connecting any two nodes
    • A tree with N nodes has N - 1 edges

  3. Tree representation
  4. A. Java version

    Class TreeNode
    {
    	Object  element;
    	TreeNode  firstChild;
    	TreeNode  nextSibling;
    }
    
    

    B. C++ version

    struct Treenode
    {
    	Object  element;
    	Treenode  *firstChild;
    	Treenode  *nextSibling;
    };
    

  5. Binary trees
  6. Each  node has at most two children.

    Height of a well-balanced tree with N leaf-nodes : about log2N.

    4. 1. Computing the height of a binary tree

    Consider the following binary tree

    At each level the number of the nodes is doubled.
    There are three levels, and the total number of nodes is:

    
    1 + 2 + 22 + 23 = 24 - 1 = 15

    Note that 24 - 1 = 2*23 - 1
    Note also that 3 = log(23)

    In general, if we have a tree with M levels, the number of the nodes would be:

    1 + 2 + 22 + …. 2M = 2(M+1) - 1 = 2*2M - 1

    Let N be the number of the nodes.
    We will find now how M (the depth of the tree) depends on the number of the nodes.

    N = 2*2M - 1

    2*2M = N + 1

    2M = (N+1)/2

    M = log((N+1)/2) = O(logN)

    Thus, given a balanced binary tree with N nodes, the height of the tree is O( log(N))

    Given a balanced binary tree with M levels, the number of the nodes is O(2M)

    Average height of a binary tree :O(sqrt(N)) - computed by considering all possible cases of a binary tree with N nodes.

    Worst case: when the tree is a list - all nodes except the leaf have only one child: height : N-1

    4. 2. Binary tree traversal

    Pre-order traversal:

    Visit the root first then the left child then the right child

    C O M P U T E R L A N D

    In-order Traversal

    Visit the left child then the root then the right child

    P M U O C T E L R N A D

    Post-order Traversal

    Visit the left child then the right child then the root

    P U M O L N D A R E T C

    Depth first traversal = Pre-order traversal

    Breadth first traversal - visit all nodes in level 0, then in level 1, etc.

    Traversal is implemented by means of recursive functions.
    The following is a pseudocode for traversal procedures:

    pre_orderVisit(tree)
    { 
      
     if (current == NULL) return;	
     else
      {
         process (current);
         pre_orderVisit (left_tree);
         pre_orderVisit (right_tree);
      }
    }
    
    in_orderVisit(tree)
    { 
     if (current == NULL) return;
     else	
       {
         in_orderVisit (left_tree);
         process (current);
         in_orderVisit (right_tree);
       }
    }
    
    post_orderVisit(tree)
    { 
     if (current == NULL) return;
     else	
       {
         post_orderVisit (left_tree);
         post_orderVisit (right_tree);
         process (current);
       }
    }

    4. 3. Binary tree node declaration

    A. Java version

    Class BinaryNode
    {
    	Object Element;		// the data in the node
    	BinaryNode left;	// Left child
    	BinaryNode right;	// Right child
    }

    B. C++ version

    struct BinaryNode
    {
    	Object Element;		// the data in the node
    	BinaryNode *left;	// Left child
    	BinaryNode *right;	// Right child
    };
    
    

    4. 4. Expression trees

    Binary trees are widely used in compiler design.
    Here is an example of expression trees.

Learning goals

  1. Know what is a tree structure, basic concepts and definitions
  2. Know how a general tree is represented
  3. Know the methods of traversing binary trees

Exam-like questions and problems

  1. Give definitions for the following concepts related to trees:
    root, child, parent, siblings, leaf, level of a node, depth of a tree
  2. Describe the three methods of traversing a binary tree
  3. Explain the relation between the height M of a well-balanced binary tree and the number of nodes N.
  4. What is the minimal height of a binary tree with 17 nodes?
    What would be the maximal height?
  5. What is the maximal number of nodes in a tree with 5 levels?
    What would be the minimal number of nodes?
  6. Draw a binary tree with 10 nodes and minimal number of levels.
  7. Choose a name of a state with more than 7 letters.
    Construct a binary tree with minimal number of levels and assign the letters to the nodes
    so that the name can be read in a pre-order, in-order, and post-order traversal.

Back to Contents page
Created by Lydia Sinapova