Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 CS 151 Trees -- Basic Concepts and Terminology
Navigation
Log in


Forgot your password?
« May 2008 »
Su Mo Tu We Th Fr Sa
123
456789 10
11121314151617
18192021222324
25262728293031
 
Document Actions

Trees -- Basic Concepts and Terminology

by admin last modified 2005-05-11 18:12

Trees

Trees are used to represent hierarchical arrangements of data or objects.

example  Family trees

example  Expression trees

example  Parse trees

example  Folders and files in the Windows file system

example  The Java class hierarchy


Trees are found throughout software systems, where they may be used as:
  • part of the user view (e.g., hierarchy of folders in Windows)
  • support for particular algorithms (e.g., parse trees used by compilers)
  • data structures used to implement abstract data types (e.g., UNIX files are stored on disk as trees, databases make heavy use of trees for indexing)

definition  A tree T is a finite, non-empty set of nodes such that

1.    One node r is designated as the root of the tree.
2.    The remaining nodes are partitioned into n>=0 subsets T1, T2, ..., Tn, each of which is a tree.

A tree is normally drawn with the root at the top.

Terminology

  • T1, T2, ..., Tn are called the subtrees of T.

  • The roots r1, r2, ..., rn of T1, T2, ..., Tn are called the children of r.  r is the parent of r1, r2, ..., rn.

  • Two nodes with the same parent are called siblings.

  • The degree of a node is the number of children (or subtrees) of the node.

  • A node with no children is called a leaf (or external node ).  A node which is not a leaf is called an internal node.

  • A node n1 is an ancestor of a node n2 (and n2 is a descendant of n1) if n2 is a member of the tree whose root is n1.

  • A path in a tree is a non-empty sequence of nodes P = { n1, n2, ... , nk }, such that ni is the parent of ni+1 for all i = 1,...,k-1.  The length of the path P is k-1.

  • The level or depth of a node n in a tree is the length of the unique path in T from its root to n.  In particular, the root itself is at level 0 and its children are at level 1.

  • The height of a node n is the length of the longest path from n to a leaf.  In particular, all leaves are at height 0.

  • The height of a tree is the height of its root node.

Binary Trees

A binary tree is a tree in which each node can have at most two children.  Many of the most important applications of trees use binary trees.  One example we have seen is the expression tree for binary operators.  Another is the binary search tree, which we will study later.


definition  A binary tree T is a finite set of nodes such that either

T is an empty set, or

T consists of a root r and exactly two subtrees, TL and T R.  TL is called the left subtree of T, and TR is called the right subtree of T.


By this definition, a node is a leaf if both its subtrees are empty.

Note:  The formal definition of a binary tree differs slightly from that of a tree in general, for two reasons:
  • It allows the case of an empty tree.
  • A tree with only a left subtree is distinguished from a tree with only a right subtree.

In a complete binary tree, all the nodes with at least one empty subtree are at the same level.

Q:  How many nodes are there in a complete binary tree of height h?


Tree Traversals

A common operation on binary trees is a tree traversal.  A tree traversal is analogous to a sequential ordering of the items in a list.  However, because a tree is not one-dimensional, there is no single natural ordering.  Instead, we can choose from several traversal orderings, all with the property that each node in the tree is visited exactly once.  We look at four of them:  preorder, inorder, postorder, and breadth-first.  Preorder, inorder, and postorder are examples of depth-first traversals.

Preorder Traversal

The definition of a preorder traversal of a binary tree is:

Visit the root node;
Perform a preorder traversal of the left subtree;
Perform a preorder traversal of the right subtree;

Inorder Traversal

Perform an inorder traversal of the left subtree;
Visit the root node;
Perform an inorder traversal of the right subtree;

Postorder Traversal

Perform a postorder traversal of the left subtree;
Perform a postorder traversal of the right subtree;
Visit the root node;

Breadth-first Traversal

Breadth-first traversal does not have a simple recursive definition.  A breadth first search is performed by first visiting the root, then all the nodes at level 1, left to right, then all the nodes at level 2, left to right, and so on until every node has been visited.  Breadth-first traversal is sometimes called a level order traversal.



 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: