Docsity

Quiz Study Guide - Algorithms and Data Structures | CSE 331, Quizzes of Computer Science

Toggle sidebar
bg1
CSE 331, Summer 2002, Lecture #7 (5/29/02
1. Quiz
2. Trees
(a) * Why? Trees provide very natural representation for surprisingly many things:
i. * File systems
ii. * Decision tree (e.g., phone tree)
iii. * Any hierarchical (data) management scheme (e.g., LDAP, NIS+)
iv. * Parsing (C, C++, XML)
(b) Definition
i. More formally: (a recursive definition:) A tree Tis defined as having a node r, (defined as its
root) and zero or more edges, each connecting rto a distinct (sub-) tree Tk. By definition,
each root may have but one parent.
ii. Graph theoretic: A tree is a connected, acyclic graph.
iii. Difference? What about r?
iv. Siblings? Parents? Children? Grandparents? Grandchildren?
v. Path: A path from node n1to node nkis defined as a sequence of nodes n1, n2, ..., nksuch
that niis the parent of ni+1 for 1 ≤i < k.
vi. The path length is defined as number of edges on path (i.e., k−1).
vii. Directed? Undirected?
viii. Proper Ancestor: if niis proper ancestor of nj,niis great −grandkparent of nj.
ix. Proper Descendant: if njis proper descendant of ni,njis great −grandkchild of ni.
x. Ancestor/Descendant (sans proper): nican be same node as nj.
(c) * Representation
i. * Array-based, Linked-list-based
A. Pure array: bound sibling size, place each blevel children in next set of array cells.
B. First-child/next sibling: linked list holds all siblings.
C. Pure pointer: linked-list for each node holds its children.
(d) Traversal
i. Generally done recursively – call on a node, print out / recurse on children of node
ii. Salient point is order in which nodes are traversed: two general types; one non-general.
iii. Pre-order:
print_out( node n )
{ print n’s contents;
foreach child of n print_out( child ); }
iv. Post-order:
print_out( node n )
{ foreach child of n print_out( child );
print n’s contents; }
v. In-order (for binary trees):
print_out( node n )
{ print_out( left child of n );
print n’s contents;
print_out( right child of n ); }
pf3

Partial preview of the text

Download Quiz Study Guide - Algorithms and Data Structures | CSE 331 and more Quizzes Computer Science in PDF only on Docsity!

CSE 331, Summer 2002, Lecture #7 (5/29/

  1. Quiz
  2. Trees

(a) * Why? Trees provide very natural representation for surprisingly many things: i. * File systems ii. * Decision tree (e.g., phone tree) iii. * Any hierarchical (data) management scheme (e.g., LDAP, NIS+) iv. * Parsing (C, C++, XML) (b) Definition i. More formally: (a recursive definition:) A tree T is defined as having a node r, (defined as its root) and zero or more edges, each connecting r to a distinct (sub-) tree Tk. By definition, each root may have but one parent. ii. Graph theoretic: A tree is a connected, acyclic graph. iii. Difference? What about r? iv. Siblings? Parents? Children? Grandparents? Grandchildren? v. Path: A path from node n 1 to node nk is defined as a sequence of nodes n 1 , n 2 , ..., nk such that ni is the parent of ni+1 for 1 ≤ i < k. vi. The path length is defined as number of edges on path (i.e., k − 1). vii. Directed? Undirected? viii. Proper Ancestor: if ni is proper ancestor of nj , ni is great − grandk^ parent of nj. ix. Proper Descendant: if nj is proper descendant of ni, nj is great − grandk^ child of ni. x. Ancestor/Descendant (sans proper): ni can be same node as nj. (c) * Representation i. * Array-based, Linked-list-based A. Pure array: bound sibling size, place each blevel^ children in next set of array cells. B. First-child/next sibling: linked list holds all siblings. C. Pure pointer: linked-list for each node holds its children. (d) Traversal i. Generally done recursively – call on a node, print out / recurse on children of node ii. Salient point is order in which nodes are traversed: two general types; one non-general. iii. Pre-order: print_out( node n ) { print n’s contents; foreach child of n print_out( child ); }

iv. Post-order: print_out( node n ) { foreach child of n print_out( child ); print n’s contents; }

v. In-order (for binary trees): print_out( node n ) { print_out( left child of n ); print n’s contents; print_out( right child of n ); }

(e) Binary Trees (Expression Trees, Binary Search Trees, etc.) i. Tree in which no node may have more than two children. ii. Representation is easy with pure pointer implementation. iii. Uses: Expression Trees, Searching, many others ... (f) Expression trees (Binary Tree Instance/Application) i. Binary operators have two children – may be operands, may be another operator (with more children). ii. Unary operators have only one child – may be an operand, may be another operator. iii. We can use a stack to build an expression tree for pre- and post-order aritmetical expressions. iv. Example: (a + b * c) + ((d * e + f) * g) v. Corresponding tree. vi. Pre-order: + + a * b c * + * d e f g vii. Post-order: a b c * + d e * f + g * + viii. Example: Using a stack, we can build an expression tree from either of the above. (g) Binary Search Trees (BST’s)

i. Simple binary tree with one property: at any given node n, assuming their existence in the tree, nodes with value less than n’s are in n’s left subtree; nodes with value greater than n’s are in n’s right subtree. ii. This can make for quick searching. iii. Interfaces: MakeEmpty, Find, FindMin, FindMax, Insert, Delete iv. MakeEmpty: Memory leaking routine for tree initialization. v. Find: Make decision at each node: 1) Have we found the node? 2) Would it be left or right of current node? If we can traverse no more, DNE; otherwise we’ll find it. vi. FindMin: Progress down left subtree from root as far as possible; last node must be minimum. vii. FindMax: Progress down right subtree from root as far as possible; last node must be maxi- mum. viii. Insert: Similar to Find; if there, increment/do nothing; if not where it should be, add. ix. Delete: Three cases: node to be deleted has 0 subtrees, 1 subtree or 2 subtrees. A. Zero subtrees: Drop it. B. One subtrees: Subtree of to-be-deleted node must all be greater than (right subtree) or less than (left subtree), drop the TBD node, attach its parent to deleted nodes child. C. Two subtrees: More complex. Basically, replace TBD node with either the greatest node of the left subtree or the least node of the right, deleting it and the TBD node (easy: it must have only one node) and reattaching it in place of the deleted node. x. Lazy Delete: also an option to simply mark as deleted and do inserts around “deleted” item. xi. Examples. xii. Complexity analysis: MakeEmpty: As implemented: O(1); If implemented correctly? A traver- sal: O(N ) What kind? Pre-order? Post-order? xiii. Complexity analysis: Find A. Worst case is depth of worst case tree: O(N ). B. Average case is average tree depth. Total tree depth for all paths generally defined by recurrence relation: D(N ) = D(i) + D(N − i) + N where i is the number of nodes in root node’s left subtree. Random distribution gives i = N − i, roughly, therefore D(N ) = 2D(N/2) + N. By rule two of master theorem: D(N ) = Θ(N log N ). C. This term represents the total tree depth for all paths in an average tree, therefore, Θ(N log N/N ) = Θ(log N ) represents average path length. xiv. Analysis: FindMin: Worst case: worst case depth of tree: O(N ). Average case: average depth of tree: O(log N ).