Presentation is loading. Please wait.

Presentation is loading. Please wait.

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Data Structures.

Similar presentations


Presentation on theme: "90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Data Structures."— Presentation transcript:

1 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Data Structures and Algorithms for Information Processing Lecture 5: Trees

2 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Binary Trees lA binary tree has nodes, similar to nodes in a linked list structure. lData of one sort or another may be stored at each node. lEach node is either a leaf, having no children, or an internal node, with one or two children

3 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Binary Trees

4 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees 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

5 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Example use of Trees Arithmetic Expressions A*(((B+C)*(D*E))+F)  Each internal node labeled by an operator  Each leaf labeled by a variable or numeric value Tree determines a unique value

6 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees 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

7 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees 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...

8 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Counting Trees The number of binary trees with internal nodes is There is no such formula if we allow unary internal nodes!

9 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees 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.

10 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees 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)

11 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees 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

12 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Complete Binary Trees A complete tree is one having levels and leaves Complete trees have a simple implementation using arrays (How?)

13 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Class for Binary Nodes public class BTNode { private Object data; private BTNode left; private BTNode right;...

14 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees 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); }...

15 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees 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); }

16 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Tree Traversals A S A M P L E R TE E Preorder traversal

17 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Tree Traversals A S A M P L E R TE E Inorder traversal

18 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Tree Traversals A S A M P L E R TE E Postorder traversal

19 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Tree Traversals A S A M P L E R TE E Level order traversal

20 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Preorder Traversal 1Process the root 2Process the nodes in the left subtree 3Process the nodes in the right subtree

21 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Preorder Print public void preorderPrint() { System.out.println(data); if (left != null) left.preorderPrint(); if (right != null) right.preorderPrint(); }

22 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Inorder Traversal 1Profess the nodes in the left subtree 2Process the root 3Process the nodes in the right subtree

23 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Inorder Print public void preorderPrint() { if (left != null) left.preorderPrint(); System.out.println(data); if (right != null) right.preorderPrint(); }


Download ppt "90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Data Structures."

Similar presentations


Ads by Google