11. Trees 1

# 11. Trees 1 - Maggie Johnson Handout#11 CS103B Trees I Key...

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

Maggie Johnson Handout #11 CS103B Trees I Key topics: * Hierarchical Structures * Tree Terminology * Trees and Recursion * Heaps Hierarchical Structures Trees are a very useful conceptual tool for representing relationships among certain classes of items. The relationship of the elements in a tree is one to many. For example, a family tree: In computer science, a compiler uses trees for representing algebraic expressions. Notice that the tree specifies an unambiguous order for evaluating an expression: Tree Terminology These simple trees illustrate many of the general characteristics of trees: 1) Each element of a tree is called a node or a vertex . 2) There is a unique first node called the root that has no predecessors, but may have many successors. 3) There may be many nodes that have no successors, but have one unique predecessor. These nodes are called leaves .

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

View Full Document
4) Each node that is neither a root or 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 includes the root). Other important terms: - subtree : given any node in a tree, that node with all its descendents is a subtree. - height : each node in a tree is at a particular level (root is at level 1); the height is always one less than the level (note: different textbooks have different definitions of height). - the maximum level of the tree is the height of the tree. (height of tree below is 2) - ancestor of node n: a node on the path from root to n. - descendent of node n: a node on the path from n to a leaf. A binary tree is a special type of tree where each node has exactly 0, 1, or 2 children. The example of the expression tree given above is an example of a binary tree. A recursive definition of a binary tree: T is a binary tree if either 1. T has no nodes, or 2. T is of the form r T(left) T(right) where r is the root node and T(left) and T(right) are both binary trees. The nodes of a tree typically contain values. A binary search tree (BST) is a binary tree that is "sorted" according to some particular order. For each node n, a binary search tree is a binary tree that satisfies the following three properties: - n's value is greater than all the values in its left subtree - n's value is less than all the values in its right subtree - both T(left) and T(right) are binary trees
Searching a binary search tree for an item is like doing a binary search. This is one of the real advantages of the tree structure. To find Wesley in a binary search tree requires 3 comparisons: Jane, Tom, and Wesley. A linear search would have required looking at every item in the list. Because of the recursive nature of trees, it is frequently much easier to express tree algorithms using recursion rather than iteration. This is a major benefits of trees because we can express some very complex operations cleanly and efficiently with recursion. Consider an algorithm that traverses a tree printing the data element of the node. According to the definition, a tree is either empty or has

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

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

### Page1 / 11

11. Trees 1 - Maggie Johnson Handout#11 CS103B Trees I Key...

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

View Full Document
Ask a homework question - tutors are online