Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Data Structures and Algorithms for Information Processing Lecture 5: Trees 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may be stored at each node. Each node is either a leaf, having no children, or an internal node, with one or two children 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Binary Trees 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Some Terminology • Root is the unique node with no parent • Leaves or terminals are nodes with no children • A subtree is a node together with all its descendents • Level of a node is number of nodes on path from the node to root 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Example use of Trees Each internal node labeled by an operator Each leaf labeled by a variable or numeric value Arithmetic Expressions A*(((B+C)*(D*E))+F) Tree determines a unique value 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Example Use of Trees Representing Chinese Characters • Characters composed hierarchically into boxes • Boxes can be oriented left-right, top-down, or outside-inside • Each leaf labeled by one of 300-400 radicals 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Binary vs. Binary-Unary Important distinction : Sometimes binary trees are defined to allow internal nodes with one or two children. There is a big difference... 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Counting Trees The number of binary trees with n internal nodes is 1 2n n 1 n There is no such formula if we allow unary internal nodes! 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Exercise Write efficient Java functions int numBTrees(int n) int numBUTrees(int n) that return the number of binary trees and binary/unary trees, respectively. 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Properties of Trees • There is exactly one path connecting any two nodes • Any two nodes have a least common ancestor • A tree with N internal nodes has N+1 external nodes (easy to prove by induction) 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. The Best Trees A binary tree is full if internal nodes fill every level, except possibly the last. The height of a full binary tree with N internal nodes is h log 2 N 1 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Complete Binary Trees • A complete tree is one having and 2 n leaves n levels • Complete trees have a simple implementation using arrays (How?) 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Class for Binary Nodes public class BTNode { private Object data; private BTNode left; private BTNode right; ... 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Class for Binary Nodes public BTNode(Object obj, BTNode l, BTNode r) { data = obj; left = l; right= r; } public boolean isLeaf() { return (left == null) && (right == null); } ... 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Copying Trees public static BTNode treeCopy(BTNode t) { if (t == null) return null; else { BTNode leftCopy = treeCopy(t.left); BTNode rightCopy = treeCopy(t.right); return new BTNode(t.data, leftCopy, rightCopy); } } 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Tree Traversals P M E S A L R A T Preorder traversal 90-723: Data Structures and Algorithms for Information Processing E E Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Tree Traversals P M L E S A R A T Inorder traversal 90-723: Data Structures and Algorithms for Information Processing E E Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Tree Traversals P M E S A L R A T Postorder traversal 90-723: Data Structures and Algorithms for Information Processing E E Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Tree Traversals P M E S A L R A T Level order traversal 90-723: Data Structures and Algorithms for Information Processing E E Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Preorder Traversal Process the root Process the nodes in the left subtree Process the nodes in the right subtree 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Preorder Print public void preorderPrint() { System.out.println(data); if (left != null) left.preorderPrint(); if (right != null) right.preorderPrint(); } 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Inorder Traversal Profess the nodes in the left subtree Process the root Process the nodes in the right subtree 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved. Inorder Print public void preorderPrint() { if (left != null) left.preorderPrint(); System.out.println(data); if (right != null) right.preorderPrint(); } 90-723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved.