# trees - Computer Science 211 Data Structures Mount Holyoke...

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

Computer Science 211 Data Structures Mount Holyoke College Fall 2009 Topic Notes: Trees We’ve spent the last several weeks looking at a variety of linear structures. There was a natural linear ordering of the elements in arrays, vectors, linked lists. We then put some restrictions on those structures, looking at stacks and queues and ordered linear structures. Just like we can write programs that can branch into a number of directions, we can design struc- tures that have branches. Today, we’ll start looking at our first more complicated structure: trees . In a linear structure, every element has unique successor. In trees, an element may have many successors. We usually draw trees upside-down in computer science. You won’t see trees in nature that grow with their roots at the top (but you can see some at Mass MoCA). Examples of Trees Expression trees The expression (2 * (4-1))+((2+7)/3) can be represented as + / \ --- ---- / \ * / / \ / \ 2 - + 3 / \ / \ 4 1 2 7 Once we have an expression tree, how can we evaluate it?

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

View Full Document
CS 211 Data Structures Fall 2009 We evaluate left subtree, then evaluate right subtree, then perform the operation at root. The evaluation of subtrees is recursive. Tournament Brackets Everyone has seen a tournament bracket: 1 1 2 1 4 2 3 1 8 4 5 2 7 3 6 1 16 8 9 4 13 5 12 2 15 7 10 3 14 6 11 (a complete and full tree) or 1 1 2 1 4 2 3 1 8 4 5 2 7 3 6 8 9 5 12 7 10 6 11 (neither complete nor full) Tree of Descendants The text looks at an example of a pedigree chart – looking at a person’s ancestors. Instead, let’s look at a person’s descendants: Anne Doug Peter Jim Todd Doug Pam Kim Josh Nick Kate Zach Alex Roland Redmond Darby Mead Definitions and Terminology A tree is either empty or consists of a node , called the root node , together with a collection of (disjoint) trees, called its subtrees . An edge connects a node to its subtrees The roots of the subtrees of a node are said to be the children of the node. 2
CS 211 Data Structures Fall 2009 There may be many nodes without any successors: These are called leaves or leaf nodes . The others are called interior nodes . All nodes except root have unique predecessor, or parent . A collection of trees is called a forest . Other terms are borrowed from the family tree analogy: sibling, ancestor, descendant Some other terms we’ll use: A simple path is series of distinct nodes such that there is an edge between each pair of successive nodes. The path length is the number of edges traversed in a path (equal to the number of nodes on the path - 1) The height of a node is length of the longest path from that node to a leaf. The height of the tree is the height of its root node. The

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.

{[ snackBarMessage ]}

### Page1 / 9

trees - Computer Science 211 Data Structures Mount Holyoke...

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

View Full Document
Ask a homework question - tutors are online