![Page 1: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/1.jpg)
1
Foundations of Software DesignFall 2002Marti Hearst
Lecture 15: Trees
![Page 2: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/2.jpg)
2
Trees
![Page 3: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/3.jpg)
3
Trees• Trees are very important and useful• They are usually drawn upside-down• The allow us to represent hierarchy
– File system– Book structure– Employees in a bureacracy
• Every node has exactly one parent – Except the root
![Page 4: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/4.jpg)
4
Anatomy of a Tree
root
siblings
parent &child
Ancestor & descendent
![Page 5: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/5.jpg)
5
Tree Terminology• Parent / Child
– If node u is the parent of node v, then v is the child of u• Siblings
– Two nodes that are children of the same parent.• Leaf (External node)
– A node is a leaf if it has no children• Internal node
– A node is internal if it has one or more children• Ancestor / Descendent
– An ancestor of a node is either the node itself or an ancestor of the parent of the node
– (this is recursive)• Subtree
– The subtree of T rooted at node v is the tree consisting of all the descendents of v in T (including v)
Adapted from http://www.oopweb.com/Algorithms/Documents/PLDS210/VolumeFrames.html
![Page 6: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/6.jpg)
6
Binary Tree• The simplest form of tree is a Binary Tree• A Binary Tree consists of
– (a) A node (called the root node) and– (b) Left and right subtrees– Both the subtrees are themselves binary trees
• Note: this is a recursive definition
![Page 7: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/7.jpg)
7
Binary Tree
Adapted from http://www.oopweb.com/Algorithms/Documents/PLDS210/VolumeFrames.html
![Page 8: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/8.jpg)
8
Tree Terminology• Proper tree
– A binary tree is proper if every node has either 2 or 0 children
– Another way to say this: • all internal nodes have exactly 2 children
– Even an improper binary tree is still a tree• Complete tree
– A tree (binary or otherwise) is complete if every leaf is the same distance from the root
Proper treeComplete tree
![Page 9: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/9.jpg)
9
Trees vs. Graphs / Networks• The Web is not a tree. Why not?
– Nodes in a tree can have only one parent• Graphs / Networks are the more general case
![Page 10: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/10.jpg)
10
Tree TerminologyHeight
– The height of a tree is the height of the root• Intuitively – the length of the longest path from the root
to a leaf, across all the leaves– More formally, the height of a node v is
• 0 if v is a leaf• 1 + maximum height of v’s children
– (this is a recursive definition)
Adapted from http://www.oopweb.com/Algorithms/Documents/PLDS210/VolumeFrames.html
![Page 11: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/11.jpg)
11
Tree TerminologyDepth
– The depth of a node v in T is the number of ancestors of v, excluding v itself.
– (The inverse of height, from the node’s viewpoint)– More formally:
• If v is the root, the depth of v is 0• Otherwise, the depth of v is one plus the depth of the
parent of v
![Page 12: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/12.jpg)
12
Our Binary Tree• Defined recursively as consisting of
– Data– A lefthand Binary Tree– A righthand Binary Tree
![Page 13: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/13.jpg)
13
Simple Binary Tree Code
![Page 14: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/14.jpg)
14
![Page 15: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/15.jpg)
15
![Page 16: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/16.jpg)
16
Recursion vs. Iterationprinting the left side of the tree
![Page 17: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/17.jpg)
17
Tree Traversal• Preorder• Inorder• Postorder
• All defined in terms of – When do you visit the node itself– When do you visit the lefthand side– When do you visit the righthand side
root
aaa
abaa
a
aab
b
![Page 18: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/18.jpg)
18
PreOrder root
aaa
abaa
a
aab
b
Why? Textual representation of the tree (parents before children)
![Page 19: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/19.jpg)
19
InOrderroot
aaa
abaa
a
aab
b
Why? Useful for searching in ordered trees
![Page 20: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/20.jpg)
20
PostOrderroot
aaa
abaa
a
aab
b
Why? Computing a property of a node depends on the nodes below it.
![Page 21: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/21.jpg)
21
Depth-first Search
From http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/ExhaustiveSearch.html
Full tree
DFS (to see it, run inPresentationMode)
Note: DFS traversal is equivalent to PreOrder traversal
![Page 22: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/22.jpg)
22
Breadth-first Search
From http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/ExhaustiveSearch.html
Full tree
BFS (to see it, run inPresentationMode)
What do we need besides the runtime stack to make BFS work?
![Page 23: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/23.jpg)
23
Breadth First Traversal
We can reuse our Queue!!
![Page 24: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/24.jpg)
24
Depth First Traversal
![Page 25: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/25.jpg)
25
Depth First Search
![Page 26: Foundations of Software Design Fall 2002 Marti Hearst](https://reader035.vdocuments.us/reader035/viewer/2022070420/56815e07550346895dcc5ade/html5/thumbnails/26.jpg)
26
Algorithm Analysis• What is the running time for
– Depth First Traversal?• O(n)
– Depth First Search?• Worst case: O(n)
– Breadth First Traversal?• O(n)
– Breadth First Search• Worst case: O(n)
– How could we improve these search times?