程序代做 CS61B Lecture #20: Trees

CS61B Lecture #20: Trees
Last modified: Wed Oct 16 16:03:40 2019 CS61B: Lecture #20 1

A Recursive Structure

Copyright By PowCoder代写 加微信 powcoder

• Trees naturally represent recursively defined, hierarchical objects with more than one recursive subpart for each instance.
• Common examples: expressions, sentences.
– Expressions have definitions such as “an expression consists of a
literal or two expressions separated by an operator.”
• Also describe structures in which we recursively divide a set into multiple subsets.
Last modified: Wed Oct 16 16:03:40 2019 CS61B: Lecture #20 2

Formal Definitions
• Trees come in a variety of flavors, all defined recursively:
– 61A style: A tree consists of a label value and zero or more
branches (or children), each of them a tree.
– 61A style, alternative definition: A tree is a set of nodes (or vertices), each of which has a label value and one or more child nodes, such that no node descends (directly or indirectly) from itself. A node is the parent of its children.
– Positional trees: A tree is either empty or consists of a node containing a label value and an indexed sequence of zero or more children, each a positional tree. If every node has two positions, we have a binary tree and the children are its left and right sub- trees. Again, nodes are the parents of their non-empty children.
– We’ll see other varieties when considering graphs.
Last modified: Wed Oct 16 16:03:40 2019 CS61B: Lecture #20 3

Tree Characteristics (I)
• The root of a tree is a non-empty node with no parent in that tree (its parent might be in some larger tree that contains that tree as a subtree). Thus, every node is the root of a (sub)tree.
• The order, arity, or degree of a node (tree) is its number (maximum number) of children.
• The nodes of a k-ary tree each have at most k children.
• A leaf node has no children (no non-empty children in the case of
positional trees).
Last modified: Wed Oct 16 16:03:40 2019 CS61B: Lecture #20 4

Tree Characteristics (II)
• The height of a node in a tree is the largest distance to a leaf. That is, a leaf has height 0 and a non-empty tree’s height is one more than the maximum height of its children. The height of a tree is the height of its root.
• The depth of a node in a tree is the distance to the root of that tree. That is, in a tree whose root is R, R itself has depth 0 in R, and if node S ̸= R is in the tree with root R, then its depth is one greater than its parent’s.
Last modified: Wed Oct 16 16:03:40 2019 CS61B: Lecture #20 5

A Tree Type, 61A Style
public class Tree

Fundamental Operation: Traversal
• Traversing a tree means enumerating (some subset of) its nodes. • Typically done recursively, because that is natural description.
• As nodes are enumerated, we say they are visited.
• Three basic orders for enumeration (+ variations):
– Preorder: visit node, traverse its children.
– Postorder: traverse children, visit node.
– Inorder: traverse first child, visit node, traverse second child (binary trees only).
351515 024 236 036
142 inorder
Last modified: Wed Oct 16 16:03:40 2019 CS61B: Lecture #20 7

Problem: Convert
into (- (- (* x (+ y 3))) z)
Preorder Traversal and Prefix Expressions
static String toLisp(Tree T) { if (T.arity() == 0) return T.label(); else {
String R; R = “(” + T.label();
for (int i = 0; i < T.arity(); i += 1) R += " " + toLisp(T.child(i)); return R + ")"; (Assume Tree

Inorder Traversal and Infix Expressions Problem: Convert
into ((-(x*(y+3)))-z)
To think about: how to get rid of all those paren- theses.
static String toInfix(Tree T) { if (T.arity() == 0) {
return T.label();
} else if (T.arity() == 1) {
return “(” T.label() + toInfix(T.child(0)) + “)”; } else {
return “(” toInfix(T.child(0)) + T.label() + toInfix(T.child(1)) + “)”; }
Last modified: Wed Oct 16 16:03:40 2019 CS61B: Lecture #20 9

Postorder Traversal and Postfix Expressions Problem: Convert
static String toPolish(Tree T) { String R; R = “”;
for (int i = 0; i < T.arity(); i += 1) R += toPolish(T.child(i)) + " "; return R + String.format("%s:%d", T.label(), T.arity()); into x y 3 +:2 *:2 -:1 z -:2 Last modified: Wed Oct 16 16:03:40 2019 CS61B: Lecture #20 10 A General Traversal: The Visitor Pattern void preorderTraverse(Tree

Iterative Depth-First Traversals
• Tree recursion conceals data: a stack of nodes (all the T arguments) and a little extra information. Can make the data explicit:
void preorderTraverse2(Tree

Traverse all nodes at depth 0, then depth 1, etc:
Level-Order (Breadth-First) Traversal
Last modified: Wed Oct 16 16:03:40 2019
CS61B: Lecture #20 13

Breadth-First Traversal Implemented
A simple modification to iterative depth-first traversal gives breadth- first traversal. Just change the (LIFO) stack to a (FIFO) queue:
void breadthFirstTraverse(Tree

Tree Representation
(a) Embedded child pointers (+ optional parent pointers)
… … …
(c) child/sibling pointers
(b) Array of child pointers (+ optional parent pointers)
(d) breadth-first array (complete trees)
Last modified: Wed Oct 16 16:03:40 2019
CS61B: Lecture #20 19

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com