DOC PREVIEW
UW CSE 373 - Lecture 7 Notes

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

TreesReadingsWhy Do We Need Trees?Tree JargonMore Tree JargonDefinition and Tree TriviaPathsImplementation of TreesArbitrary BranchingBinary TreesMinimum depth vs node countMaximum depth vs node countA degenerate treeTraversing Binary TreesSlide 15Binary Search TreesOperations on Binary Search TreesBinary SearchTreeFindFindMinInsert OperationInsert 95Insert Done with call-by-referenceCall by Value vs Call by ReferenceDelete OperationSlide 26Delete “5” - No childrenDelete “24” - One childDelete “10” - two childrenThen Delete “11” - One childRemove from TextFindMin SolutionTrees CSE 373Data StructuresLecture 712/26/03 Trees - Lecture 7 2Readings•Reading ›Chapter 4.1-4.312/26/03 Trees - Lecture 7 3Why Do We Need Trees?•Lists, Stacks, and Queues are linear relationships•Information often contains hierarchical relationships ›File directories or folders ›Moves in a game›Hierarchies in organizations •Can build a tree to support fast searching12/26/03 Trees - Lecture 7 4Tree Jargon• root• nodes and edges• leaves• parent, children, siblings• ancestors, descendants• subtrees• path, path length• height, depthABC DEF12/26/03 Trees - Lecture 7 5More Tree Jargon•Length of a path = number of edges•Depth of a node N = length of path from root to N•Height of node N = length of longest path from N to a leaf•Depth of tree = depth of deepest node•Height of tree = height of rootABC DEFdepth=0, height = 2depth = 2, height=0depth=1, height =012/26/03 Trees - Lecture 7 6Definition and Tree Trivia•A tree is a set of nodes,i.e., either ›it’s an empty set of nodes, or›it has one node called the root from which zero or more trees (subtrees) descend•Two nodes in a tree have at most one path between them•Can a non-zero path from node N reach node N again?No. Trees can never have cycles (loops)12/26/03 Trees - Lecture 7 7Paths•A tree with N nodes always has N-1 edges (prove it by induction)Base Case: N=1 one node, zero edgesInductive Hypothesis: Suppose that a tree with N=k nodesalways has k-1 edges.Induction: Suppose N=k+1…The k+1st node must connectto the rest by 1 or more edges.If more, we get a cycle. So it connects by just 1 more edgek+112/26/03 Trees - Lecture 7 8Implementation of Trees•One possible pointer-based Implementation›tree nodes with value and a pointer to each child›but how many pointers should we allocate space for?•A more flexible pointer-based implementation›1st Child / Next Sibling List Representation›Each node has 2 pointers: one to its first child and one to next sibling›Can handle arbitrary number of children12/26/03 Trees - Lecture 7 9Arbitrary BranchingABC DEFAB C DE FDataFirstChild SiblingNodes of same depth12/26/03 Trees - Lecture 7 10Binary Trees•Every node has at most two children›Most popular tree in computer science•Given N nodes, what is the minimum depth of a binary tree? (This means all levels but the last are full!)›At depth d, you can have N = 2d to N = 2d+1-1 nodes  Nlogd implies 12N22min1dd12/26/03 Trees - Lecture 7 11Minimum depth vs node count•At depth d, you can have N = 2d to 2d+1-1 nodes •minimum depth d is  (log N)12 36 74 5T(n) = (f(n)) meansT(n) = O(f(n)) and f(n) = O(T(n)),i.e. T(n) and f(n) have the same growth rated=2N=22 to 23-1 (i.e, 4 to 7 nodes)12/26/03 Trees - Lecture 7 12Maximum depth vs node count•What is the maximum depth of a binary tree?›Degenerate case: Tree is a linked list!›Maximum depth = N-1•Goal: Would like to keep depth at around log N to get better performance than linked list for operations like Find12/26/03 Trees - Lecture 7 13A degenerate tree1523476A linked list with high overheadand few redeeming characteristics12/26/03 Trees - Lecture 7 14Traversing Binary Trees•The definitions of the traversals are recursive definitions. For example:›Visit the root›Visit the left subtree (i.e., visit the tree whose root is the left child) and do this recursively›Visit the right subtree (i.e., visit the tree whose root is the right child) and do this recursively•Traversal definitions can be extended to general (non-binary) trees12/26/03 Trees - Lecture 7 15Traversing Binary Trees•Preorder: Node, then Children (starting with the left) recursively + * + A B C D•Inorder: Left child recursively, Node, Right child recursively A + B * C + D•Postorder: Children recursively, then NodeA B + C * D +A*BCD++12/26/03 Trees - Lecture 7 16Binary Search Trees•Binary search trees are binary trees in which ›all values in the node’s left subtree are less than node value›all values in the node’s right subtree are greater than node value•Operations:›Find, FindMin, FindMax, Insert, DeleteWhat happens when we traverse the tree in inorder?95109699949712/26/03 Trees - Lecture 7 17Operations on Binary Search Trees•How would you implement these?›Recursive definition of binary search trees allows recursive routines›Call by reference helps too•FindMin•FindMax•Find•Insert•Delete95109699949712/26/03 Trees - Lecture 7 18Binary SearchTree951096999497dataleft right95 9410 9796 9912/26/03 Trees - Lecture 7 19FindFind(T : tree pointer, x : element): tree pointer {case { T = null : return null; T.data = x : return T; T.data > x : return Find(T.left,x); T.data < x : return Find(T.right,x)}}12/26/03 Trees - Lecture 7 20FindMin•Design recursive FindMin operation that returns the smallest element in a binary search tree.›FindMin(T : tree pointer) : tree pointer {// precondition: T is not null //???}12/26/03 Trees - Lecture 7 21Insert Operation•Insert(T: tree, X: element) ›Do a “Find” operation for X›If X is found  update (no need to insert)›Else, “Find” stops at a NULL pointer›Insert Node with X there•Example: Insert 951096999497?12/26/03 Trees - Lecture 7 22Insert 95109699949710969994979512/26/03 Trees - Lecture 7 23Insert Done with call-by-referenceInsert(T : reference tree pointer, x : element) : integer {if T = null then T := new tree; T.data := x; return 1;//the links to //children are nullcase T.data = x : return 0; T.data > x : return Insert(T.left, x); T.data < x : return Insert(T.right, x);endcase}Advantage of reference parameter is that the call hasthe original pointer not a copy.This is where call byreference makes adifference.12/26/03 Trees - Lecture 7 24Call by Value vs Call by


View Full Document

UW CSE 373 - Lecture 7 Notes

Download Lecture 7 Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 7 Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7 Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?