260 likes | 443 Views
Foundations of Software Design Fall 2002 Marti Hearst. Lecture 15: Trees. Trees. 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
E N D
Foundations of Software DesignFall 2002Marti Hearst Lecture 15: Trees
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
root Anatomy of a Tree siblings Ancestor & descendent parent & child
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
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
Binary Tree Adapted from http://www.oopweb.com/Algorithms/Documents/PLDS210/VolumeFrames.html
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 Complete tree Proper tree
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
Tree Terminology Height • 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
Tree Terminology Depth • 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
Our Binary Tree • Defined recursively as consisting of • Data • A lefthand Binary Tree • A righthand Binary Tree
root b a ab aa aaa aab 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 b a ab aa aaa aab PreOrder • Why? Textual representation of the tree (parents before children)
root b a ab aa aaa aab InOrder • Why? Useful for searching in ordered trees
root b a ab aa aaa aab PostOrder • Why? Computing a property of a node depends on the nodes below it.
Depth-first Search Full tree Note: DFS traversal is equivalent to PreOrder traversal DFS (to see it, run in Presentation Mode) From http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/ExhaustiveSearch.html
Breadth-first Search Full tree What do we need besides the runtime stack to make BFS work? BFS (to see it, run in Presentation Mode) From http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/ExhaustiveSearch.html
Breadth First Traversal We can reuse our Queue!!
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?