Handout 17

# Handout 17 - Mehran Sahami CS103B Handout #17 February 2,...

This preview shows pages 1–3. Sign up to view the full content.

Mehran Sahami Handout #17 CS103B February 2, 2009 Introduction to Trees Hierarchical Structures Trees are very useful conceptual tools for representing relationships among certain classes of items. For example, trees can be used to represent familial relationships, such as the following stylized family tree: Here, “gp” is grandparent, “mf” is mother/father, “ua” is uncle/aunt, “k1”, “k2” and “k3” are kids, and “c1” and “c2” are cousins of the kids. As you may recall from our initial discussion of graphs (in the context of discussing representations for relations), the diagram above is a graph . And, as you may have guessed by now, a tree is simply a particular type of graph . More formally stated: Definition 1 : A tree is a connected graph with no cycles. (A cycle is a path of length one or more that starts and ends at the same vertex and does not contain repeated edges.) The relationship of the elements in a tree is one-to-many . In other words, as we descend down the tree, each node in the tree can have many nodes “underneath” it. For example, in computer science, a compiler uses trees for representing algebraic expressions. Notice that the tree specifies an unambiguous order for evaluating an expression, as seen below. Trees used to represent expressions, as the tree above, are sometimes called expression trees . In order to evaluate the expression represented above, we would need to evaluate the tree bottom-up. In other words, before we can evaluate the „/‟ operator at the top of the tree, we must evaluate the sub-expressions (a – b) and ((c * d) + e) beneath it. To more accurately describe this process, we first present some terminology with respect to trees.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Tree Terminology The simple trees shown above illustrate many of the general characteristics of trees: Each element of a tree is called a node or a vertex (since trees are really graphs). There is a unique first node called the root that has no predecessors, but may have many successors. There may be many nodes that have no successors, but have one unique predecessor. These nodes are called leaves . Each node that is neither a root nor a leaf has a unique predecessor, called the parent node, and at least one successor, called a child node. An internal node is any node that is not a leaf (so this can include the root, if the root has any children). There are also several other important terms in regard to trees: subtree : given any node in a tree, that node with all its descendents is a called a subtree. height : each node in a tree is at a particular level (root is at level 0). The height of a tree is always equal to the maximum level of the tree. For example, the height of the tree below is 2. ancestor
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/26/2009 for the course CS 103B taught by Professor Sahami,m during the Winter '08 term at Stanford.

### Page1 / 10

Handout 17 - Mehran Sahami CS103B Handout #17 February 2,...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online