# 07 - CIS 121 Spring 2010 Data Structures and Introduction...

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

1 2/12/10 Lecture Notes 7 CIS 121 Spring 2010 Data Structures and Introduction to Algorithms using JAVA Lecture Notes 7: Trees, tree traversal, iterator © 1999-2010 S.Kannan, V.Tannen, S.Guha & K.Daniilidis © 2006 M.Goodrich & R.Tamassia (with permission) 2/12/10 Lecture Notes 7 2 Sections 6.2, 6.3, 6.4 Chapter 18 Recommended reading:

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

View Full Document
2 2/12/10 Lecture Notes 7 3 Trees : examples Unix directory structure /usr/skk/courses cse121/ cse220/ grades hws/ programs/ projects/ grades papers/ demos/ crypto 2-3trees msts 2/12/10 Lecture Notes 7 4 Evolutionary trees * ^ + - * + 4 -5 2 -2 4 7 3 Expression trees
3 2/12/10 Lecture Notes 7 5 Some definitions A tree contains nodes and edges , but we will also allow the empty tree consisting of no nodes at all. rooted tree is either empty or it has a distinguished node called the root . Every node except for the root has an edge from a unique parent node to itself. A node also has edges to child nodes if any. A node without children is called a leaf . A node with children is an internal node . Two nodes are siblings if they have the same parent. 2/12/10 Lecture Notes 7 6 u v w r r is the root . v is the parent of u . u is a child of v . w is another child of v . u is a leaf . v and w are internal nodes . (r,v) is an edge . These edges have direction . v is a node .

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

View Full Document
4 2/12/10 Lecture Notes 7 7 More definitions Node x is an ancestor of node y if either x = y or x is an ancestor of parent(y). Node y is then called a descendant of node x. A path is a sequence of edges. If x is an ancestor of y then there is a (unique!) path from x to y. The length of a path is the number of edges in it. (The length of a path from a node to itself is 0.) The depth of a node x is the length of the path from the root to x. The height of a node x is the length of the longest path from x to a descendant. The height of the root is also the height of the tree . By convention , we define the height of the empty tree to be -1 ! 2/12/10 Lecture Notes 7 8 u v w r l v is an ancestor of l . l is a descendant of r . w is an ancestor of w . depth of r is 0. depth of l is 3. height of w is 1. (There is a path of length 1 to l .) height of v is 2. (There is a path of length 2 to l .) height of the tree is 3.
5 2/12/10 Lecture Notes 7 9 Lists vs. Trees --- the magic of 2 over 1 A binary tree is a tree where every node has at most 2 children.These are good enough for most purposes. Population is proportional to the length Linked list Population explodes exponentially in the height Tree 2/12/10 Lecture Notes 7 10 How trees are put together In the bottom-up view we think of a tree as put together by adding a root and making it the parent of the roots of other trees .

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.

## 07 - CIS 121 Spring 2010 Data Structures and Introduction...

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

View Full Document
Ask a homework question - tutors are online