DOC PREVIEW
CORNELL CS 211 - Lecture 18 Trees

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 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 23 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 23 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 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS211, Lecture 18 TreesAbout assignment A5 Functional programmingAppend one list to anotherSlide 4Overview of treesBinary treeTerminologyClass for binary tree nodesClass for general treesOne application of treesPowerPoint PresentationSlide 12Slide 13Recursion on treesTree searchWalks of treeIn-order and post-order walksSome useful routinesExampleUseful facts about binary treesTree with header elementTree with parent pointersSummary1CS211, Lecture 18 TreesI think that I shall never seeA poem as lovely as a treeA tree whose hungry mouth is prestAgainst the earth's sweet flowing breastA tree that looks to God all day,And lifts her leafy arms to pray;A tree that may in summer wearA nest of robins in her hair;Upon whose bosom snow has lain;Who intimately lives with rainPoems are made by fools like me,But only God can make a tree.Joyce KilmerReadings: Weiss,chapter 18,sections 18.1--18.3.2About assignment A5Functional programmingWrite an implementation of linked list using only recursion —no assignment statement or loops.Implement sets using it —again without assignment or loops.Of course, when using the implementation, you can use assignments.public class RList {public Object value;public RList next;}3Append one list to another.3 a5l1a64 a5 a66 a6 a77 b3l2b18 b3 b20 b2 b13 c4 c3l3c14 c3 c26 c2 c17 c68 c6 c50 c5 c44Append one list to another.3 a5l1a64 a5 a66 a6 a77 b3l2b18 b3 b20 b2 b1/** = l1 with l2 appended to it */public static RList append(Rlist l1, Rlist l2) { if (l1 == null) return l2; return new Rlist(l1.value, append(l1.next, l2) );}5Overview of trees5478 92547 8General treeNot a tree568List-like treeTree: recursive data structure.Definition 1:A tree is a (1) a value (the root value)together with (2) one or more other trees, called its children.Each of the circles is called a node of the tree. Definition does not allow for empty trees (trees with 0 nodes).childrenrootparent6Binary treeBinary tree: tree in which each node can have at most two children.Redefinition of binary tree to allow empty treeA binary tree is either(1) Ø (the empty binary tree)or (2) a root node (with a value), a left binary tree, a right binary tree547 82Binary treetree with root 4 has an empty right binary treetree with root 2 has an empty left binary treetree with root 7 has two empty children.7Terminology•Edge AB: A parent of B. B is child of A.•Generalization of parent and child: ancestor and descendant–root and A are ancestors of B•Leaf node: node with no descendants (or empty descendents)•Depth of node: length of pathfrom root to that node–depth(A) = 1 depth(B) = 2•Height of node: length of longest path from node to leaf–height(A) = 1 height(B) = 0•Height of tree = height of root–in example, height of tree = 254782Binary treeABLeft sub-tree of rootRight sub-tree of rootRoot of tree8Class for binary tree nodes/** An instance is a nonempty binary tree */public class TreeNode { private Object datum; private TreeNode left; private TreeNode right; /** Constructor: a one-node tree with root value ob */ public TreeNode(Object ob) { datum = ob; } /** Constructor: tree with root value ob and left and right subtrees l and r */ public TreeNode(Object ob, TreeNode l, TreeNode r) { datum = ob; left = l; right = r; } **getter and setter methods for all three fields**}empty tree is given by value null9Class for general trees5478 9278 315478 9278 31public class GTreeNode{ private Object datum; private GTreeNode left; private GTreeNode sibling; appropriate constructors and getter and setter methods}General treeTree represented using GTreeNode• Parent node points directly only to its leftmost child.• Leftmost child has pointer to next sibling, which points to next sibling etc.10One application of trees•Most languages (natural and computer) have a recursive, hierarchical structure.•This structure is implicit in ordinary textual representation.•Recursive structure can be made explicit by representing sentences in the language as trees: abstract syntax trees (AST’s)•AST’s are easier to optimize, generate code from, etc. than textual representation.•Converting textual representations to AST: job of parser11Syntax treesExpression ::= E $ F ::= IntegerE ::= T { <+ | –> T } F ::= – F T ::= F { <* | /> F } F ::= ( E ) ExpressionExpressionETF2 $ET TF2 + 3 $F12Writing a parser for the languageE ::= T { <+ | –> T }/** Token Scan.getToken() is first token of a sentence for E. Parse it, giving error mess. if there are mistakes. After the parse, Scan.getToken should be the symbol following the parsed E. */ public static void parseE() {parseT(); while (Scan.getToken() is + or - ) { Scan.scan();parse(T);}}13Writing a parser for the languageE ::= T { <+ | –> T }/** Token Scan.getToken() is first token of a sentence for E. Parse it, giving error mess. if there are mistakes. After the parse, Scan.getToken should be the symbol following the parsed E. Return a TreeNode that describes the parsed E */ public static TreeNode parseE() {TreeNode lop= parseT(); while (Scan.getToken() is + or - ) {Token op= Scan.getToken()); Scan.scan();TreeNode rop= parse(T);lop= new TreeNode(op, lop, rop);}}14Recursion on trees•Recursive methods can be written to operate on trees in the obvious way.•In most problems–base case: empty tree•sometimes base case is leaf node–recursive case: solve problem on left and right subtrees, and then put solutions together to compute solution for tree15•Analog of linear search in lists: given tree and an object, find out if object is stored in tree.•Trivial to write recursively; much harder to write iteratively. /** = “v occurs in t” */ public static boolean treeSearch(Object v, TreeNode t) { if (t == null) return false; return t.getDatum().equals(v) || treeSearch(v, t.getleft()) || treeSearch(v, t.getRight());}Tree search923 572016Walks of treeExample on last slide showed pre-order walk of tree:–process root–process left sub-tree –process right sub-tree•Intuition: think of prefix representation of expressions213A walk of a tree processes each node of the tree in some order.infix: 3 * 4 – (7 + 2)prefix: – * 3


View Full Document

CORNELL CS 211 - Lecture 18 Trees

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download Lecture 18 Trees
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 18 Trees 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 18 Trees 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?