Data Structures and Algorithms for Information Processing Lecture























- Slides: 23
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 l A binary tree has nodes, similar to nodes in a linked list structure. l Data of one sort or another may be stored at each node. l 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 subtreeis 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 internal nodes is 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 num. BTrees(int n) int num. BUTrees(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 internal nodes is 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 leaves levels • Complete trees have a simplementation 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 is. Leaf() { 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 tree. Copy(BTNode t) { if (t == null) return null; else { BTNode left. Copy = tree. Copy(t. left); BTNode right. Copy = tree. Copy(t. right); return new BTNode(t. data, left. Copy, right. Copy); } } 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 1 Process the root 2 Process the nodes in the left subtree 3 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 preorder. Print() { System. out. println(data); if (left != null) left. preorder. Print(); if (right != null) right. preorder. Print(); } 90 -723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved.
Inorder Traversal 1 Profess the nodes in the left subtree 2 Process the root 3 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 preorder. Print() { if (left != null) left. preorder. Print(); System. out. println(data); if (right != null) right. preorder. Print(); } 90 -723: Data Structures and Algorithms for Information Processing Lecture 5: Trees Copyright © 1999, Carnegie Mellon. All Rights Reserved.