Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Similar presentations


Presentation on theme: "Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved."— Presentation transcript:

1 Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

2 Overview ● 10.1 – Introduces tree terminology – Discusses the implementation of binary trees ● 10.2 – Traversing trees ● 10.3 – General trees

3 Binary Trees ● Binary tree – Empty or a node with a left and right subtree. – Each of these subtrees is itself a binary tree. ● Children – The nodes directly below a node – In a binary tree, no node can have more than two children. ● Parent – Every node in a binary tree has exactly one parent, except for one a the top (root: no parent). ● Siblings: Nodes with the same parent. ● Root: Node at the top of a tree.

4 Binary Trees ● Leaves: Nodes with no children. ● Internal nodes: Nodes that are not leaves. ● Depth: – Number of lines along the path back to the root. – Root is a depth 0. ● Level – A level of the tree is the set of nodes at a particular depth. ● Height – The height of a tree is the depth of the deepest node.

5 Binary Trees

6 ● Descendants – A node's descendants are itself, its children, their children, and so on. ● Every node is a descendant of the root. ● Proper descendants – All of it descendants except itself. ● Ancestors – Itself, its parents, its grandparent, and so on. ● Proper ancestors – All of its ancestors except itself.

7 Binary Trees ● The number of nodes in a binary tree depends on the height of the tree and on how “skinny” or “busy” the tree is. – Linear tree ● Every internal node has only one child. – Perfect or Complete ● All of the leaves are at the same depth. ● Every internal node has exactly two children.

8 Binary Trees The number of nodes in a binary tree of height h can be anywhere between h + 1 (for a linear tree) and ● A binary tree with n nodes has height h between log 2 (n + 1) – 1 (for a perfect binary tree) and n-1 (for a linear tree) ● h Θ (log n) for a perfect binary tree. (for a perfect binary tree)

9 Binary Trees

10

11

12

13

14

15

16

17

18

19

20

21 Binary Tree Program package Tutorial9; import java.util.*; public class BinaryTree { private Object value; //instance variables private BinaryTree left = null; private BinaryTree right = null; public BinaryTree(Object o, BinaryTree l, BinaryTree r) { //constructor value = o; left = l; right = r; }

22 Binary Tree Program (cont.) // The Vector class implements a growable array of objects. // The Vector class is similar to ArrayList class public Vector getPreorder() { Vector vec = new Vector(); return traversePreorder( this, vec ); } public Vector getInorder() { Vector vec = new Vector(); return traverseInorder( this, vec ); } public Vector getPostorder() { Vector vec = new Vector(); return traversePostorder( this, vec ); }

23 Binary Tree Program (cont.) private Vector traversePreorder(BinaryTree b, Vector v) { if ( b != null ) { v.addElement( b.value ); // root traversePreorder( b.left, v ); // left traversePreorder( b.right, v ); // right } return v; } private Vector traverseInorder(BinaryTree b, Vector v) { if ( b != null ) { traverseInorder( b.left, v ); // left v.addElement( b.value ); // root traverseInorder( b.right, v ); // right } return v; }

24 Binary Tree Program (cont.) private Vector traversePostorder(BinaryTree b, Vector v) { if ( b != null ) { traversePostorder( b.left, v ); // left traversePostorder( b.right, v ); // right v.addElement( b.value ); // root } return v; }

25 Binary Tree Program (cont.) public static void main ( String [] args ) { // construct the binary tree BinaryTree l = new BinaryTree( "D", null, null ); BinaryTree r = new BinaryTree( "E", null, null ); l4 = new BinaryTree( "B", l, r ); BinaryTree l2 = new BinaryTree( "H", null, null ); BinaryTree r2 = new BinaryTree( "I", null, null ); l3 = new BinaryTree( "F", l2, r2 ); r3 = new BinaryTree( "G", null, null ); r4 = new BinaryTree( "C", l3, r3 ); BinaryTree root = new BinaryTree( "A", l4, r4 ); // traverse the binary tree Vector v1 = root.getPreorder(); System.out.println( "Preorder Traverse: " +v1 ); Vector v2 = root.getInorder(); System.out.println( "Inorder Traverse: " +v2 ); Vector v3 = root.getPostorder(); System.out.println( "Postorder Traverse: " +v3 ); } // end of main metohd } // end of binary tree program

26 Binary Tree Traversal Outputs (from the previous program) A B C D E FG H I Inorder output (v2): Preorder output (v1): Postorder output (v3): ABDECFHIG DBEAHFICG DEBHIFGCA

27 Tree Traversal ● To output the contents in a binary tree ● Four meaningful orders in which to traverse a binary tree. – Preorder – Inorder – Postorder – Level order

28 Tree Traversal

29 Tree Traversal (Preorder)

30 Tree Traversal (Inorder)

31 Tree Traversal (Postorder)

32 Tree Traversal (Preorder, using stack) ● Using the stack to do Preorder traversal iteratively (not recursively) ● Note, we have to push the right child before left one, because of the last-in, first-out policy of a stack.

33 Tree Traversal (Levelorder, using queue) ● Using the queue to do Levelorder (i.e. breadth-first) traversal iteratively. ● Note, we add the left child before right one, because of the first-in, first-out policy of a queue.

34 Tree Traversal ● Level order traversal is sometimes called breadth-first. ● The other traversals are called depth-first. ● Traversal takes Θ (n) time in both breadth-first and depth-first. ● Memory usage in a perfect tree is Θ (log n) in depth-first and Θ (n) in breadth-first traversal.

35 Tree Traversal

36 Using ArrayQueue to Store Binary Tree index01234567891011 nodeABCDEFGHIJKL Left- child index 1-4689------ Right- child index 2357-10---11--

37 General Trees ● General trees differ from binary trees in three ways: – A node in a general tree may have more than two children. – A node in a general tree has a (possibly empty) sequence of children, rather than a certain number of “slots” to fill. ● In binary trees – a tree with a left subtree but no right subtree – a tree with a right subtree but no left subtree. ● No such distinction is made for general trees – General trees cannot be empty. This restriction is made to avoid having to distinguish between a node with no subtrees and a node with several empty subtrees, which would be drawn identically.

38 General Trees ● Inheritance diagrams showing the relationships between classes is a general tree.

39 General Trees ● Simplest is to represent each node as an item. – Array of children or list of children. – First-child, next-sibling ● A less intuitive but more space-efficient representation has each node keeping track of its first child and its next sibling.

40 General Trees

41

42

43

44

45

46

47

48

49 ● How do we decide which is the best move to make?

50 General Trees ● If it is not possible to win in a single move, consider how the opponent might reply. ● The value of each leaf is determined by score(). ● The value of an internal node is determined by taking either the minimum (if the node represents a board with O to play) or the maximum (X to play) of the node's children. – Minimax algorithm ● Considering all possible moves out to the end of the game.

51 General Trees

52 ● Once we determine the value of a child, we no longer need the subtree rooted at that child. ● We invoke a method which determines the value of the root by determining the value of its descendants.

53 General Trees ● The structure of minimaxForO() is almost identical to that of playBestMove(). – Differences: ● It returns the value of the board on which it is invoked, rather than making the best move. ● It looks for the move leading to the minimum score, rather than the maximum. ● It invokes minimaxForX() instead of score().

54 General Trees

55

56 Summary ● A tree is a branching, hierarchical structure. – A binary tree either is empty or consists of a node, a left subtree, and a right subtree. – A general tree (which cannot be empty) consists of a node and zero or more subtrees. ● In the widest possible binary tree, called a perfect tree, the number of nodes is exponential in the height of the tree. ● The height of such a tree is logarithmic in the number of nodes.

57 Summary ● Binary trees can be traversed – Preorder – Inorder – Postorder – Level order ● The first three have very elegant recursive algorithms, but level order traversal requires a queue.

58 Summary ● A binary tree is usually represented by a linked structure. ● General trees, presentations include the array of children representation and the first-child, next-sibling representation. ● Trees may not explicitly be constructed as data structures, but are implicit in the way the program runs.

59 Chapter 10 Self-Study Homework ● Pages: 265-279 ● Exercises: 10.1, 10.2, 10.6, 10.7


Download ppt "Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved."

Similar presentations


Ads by Google