1 / 26

Foundations of Software Design Fall 2002 Marti Hearst

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

elroy
Download Presentation

Foundations of Software Design Fall 2002 Marti Hearst

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Foundations of Software DesignFall 2002Marti Hearst Lecture 15: Trees

  2. Trees

  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

  4. root Anatomy of a Tree siblings Ancestor & descendent parent & child

  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

  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

  7. Binary Tree Adapted from http://www.oopweb.com/Algorithms/Documents/PLDS210/VolumeFrames.html

  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 Complete tree Proper tree

  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

  10. 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

  11. 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

  12. Our Binary Tree • Defined recursively as consisting of • Data • A lefthand Binary Tree • A righthand Binary Tree

  13. Simple Binary Tree Code

  14. Recursion vs. Iterationprinting the left side of the tree

  15. 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

  16. root b a ab aa aaa aab PreOrder • Why? Textual representation of the tree (parents before children)

  17. root b a ab aa aaa aab InOrder • Why? Useful for searching in ordered trees

  18. root b a ab aa aaa aab PostOrder • Why? Computing a property of a node depends on the nodes below it.

  19. 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

  20. 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

  21. Breadth First Traversal We can reuse our Queue!!

  22. Depth First Traversal

  23. Depth First Search

  24. 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?

More Related