40
Trees Chapter 24

Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

Embed Size (px)

Citation preview

Page 1: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

Trees

Chapter 24

Page 2: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

2

Chapter Contents

Tree Concepts• Hierarchical

Organizations• Tree Terminology

Traversals of a Tree• Traversals of a Binary

Tree• Traversals of a

General Tree

Java Interfaces for Trees

• Interfaces for All Trees• Interface for Binary

Examples of Binary Trees• Expression Trees• Decision Trees• Binary Search Trees

Examples of General Trees• Parse Trees• Game Trees

Page 3: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

3

Tree Concepts

Previous data organizations place data in linear order

Some data organizations require categorizing data into groups, subgroups

This is hierarchical classification• Data items appear at various levels within the

organization

Page 4: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

4

Hierarchical Organization

Example: File directories

Fig. 24-1 Computer files organized into folders.

Page 5: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

5

Hierarchical Organization

Example: A university's organization

Fig. 24-2 A university's administrative structure.

Page 6: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

6

Hierarchical Organization

Example: Family trees

Fig. 24-3 Carole's children and grandchildren.

Page 7: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

7

Hierarchical Organization

Example: Family trees

Fig. 24-4 Jared's parents and grandparents.

Page 8: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

8

Tree Terminology

A tree is • A set of nodes• Connected by edges

The edges indicate relationships among nodes

Nodes arranged in levels • Indicate the nodes' hierarchy• Top level is a single node called the root

Page 9: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

9

Tree Terminology

Fig. 24-5 A tree equivalent to the tree in Fig. 24-1

Page 10: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

10

Tree Terminology

Nodes at a given level are children of nodes of previous level

Node with children is the parent node of those children

Nodes with same parent are siblings

Node with no children is a leaf node

The only node with no parent is the root node• All others have one parent each

Page 11: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

11

Tree Terminology

Empty trees?• Some authors specify a general tree must have at

least the root node• This text will allow all trees to be empty

A node is reached from the root by a path• The length of the path is the number of edges that

compose it

The height of a tree is the number of levels in the tree

The subtree of a node is a tree rooted at a child of that node

Page 12: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

12

Binary Trees

Each node has at most two children

Fig. 24-6 Three binary trees.

Page 13: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

13

Binary Trees

A binary tree is either empty or has the following form

• Where Tleft and Tright are binary trees

Page 14: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

14

Binary Trees

Every nonleaf in a full binary tree has exactly two children

A complete binary tree is full to its next-to-last level• Leaves on last level filled from left to right

The height of a binary tree with n nodes that is either complete or full is log2(n + 1)

Page 15: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

15

Binary Trees

Fig. 24-7 The number of nodes

in a full binary tree as a function

of the tree's height.

Page 16: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

16

Traversals of a Tree

Visiting a node• Processing the data within a node

This is the action performed on each node during traversal of a treeA traversal can pass through a node without visiting it at that momentFor a binary tree• Visit the root• Visit all nodes in the root's left subtree• Visit all nodes in the root's right subtree

Page 17: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

17

Traversals of a Tree

Preorder traversal: visit root before the subtrees

Fig. 24-8 The visitation order of a preorder traversal.

Page 18: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

18

Traversals of a Tree

Inorder traversal: visit root between visiting the subtrees

Fig. 24-9 The visitation order of an inorder traversal.

Page 19: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

19

Traversals of a Tree

Postorder traversal: visit root after visiting the subtrees

Fig. 24-10 The visitation order of a postorder traversal.

These are examples of a

depth-first traversal.

These are examples of a

depth-first traversal.

Page 20: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

20

Traversals of a Tree

Level-order traversal: begin at the root, visit nodes one level at a time

Fig. 24-11 The visitation order of a level-order traversal.

This is an example of a breadth-first

traversal.

This is an example of a breadth-first

traversal.

Page 21: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

21

Traversals of a General Tree

A general tree has traversals that are in• Level order• Preorder• Postorder

Inorder traversal not well defined for a general tree

Page 22: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

22

Traversals of a General Tree

Fig.24-12 The visitation order of two traversals of a general tree: (a) preorder; (b) postorder.

Page 23: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

23

Java Interfaces for Trees

An interface that specifies operations common to all trees

public interface TreeInterface{ public Object getRootData();

public int getHeight();public int getNumberOfNodes();public boolean isEmpty();public void clear();

} // end TreeInterface

Page 24: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

24

Java Interfaces for Trees

Interface for iterators for various traversals

import java.util.Iterator;public interface TreeIteratorInterface{ public Iterator getPreorderIterator();

public Iterator getPostorderIterator();public Iterator getInorderIterator();public Iterator getLevelOrderIterator();

} // end TreeIteratorInterface

Page 25: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

25

Java Interfaces for Trees

Interface for a class of binary treespublic interface BinaryTreeInterface extends TreeInterface,

TreeIteratorInterface{ /** Sets an existing binary tree to a new one-node binary tree.

* @param rootData an object that is the data in the new tree’s root */public void setTree(Object rootData);

/** Sets an existing binary tree to a new binary tree.* @param rootData an object that is the data in the new tree’s root* @param leftTree the left subtree of the new tree* @param rightTree the right subtree of the new tree */public void setTree(Object rootData,|

BinaryTreeInterface leftTree,BinaryTreeInterface rightTree);

} // end BinaryTreeInterface

Page 26: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

26

Java Interfaces for Trees

Fig. 24-13 A binary tree whose nodes contain one-letter strings.

Page 27: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

27

Examples of Binary Trees

Expression Trees

Fig. 24-14 Expression trees for four algebraic expressions.

Page 28: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

28

Examples of Binary Trees

Algorithm for evaluating an expression tree in postorder traversal

Algorithm evaluate(expressionTree)if (expressionTree is empty)

return 0else{ firstOperand = evaluate(left subtree of expressionTree)

secondOperand = evaluate(right subtree of expressionTree)operator = the root of expressionTreereturn the result of the operation operator and its operands

firstOperand and secondOperand}

Page 29: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

29

Decision TreesA decision tree can be the basis of an expert system• Helps users solve problems, make decisions

Fig. 24-15 A binary decision tree.

Page 30: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

30

Decision TreesA possible Java interface for a binary decision tree.

public interface DecisionTreeInterface extends BinaryTreeInterface{ /** Task: Gets the data in the current node.

* @return the data object in the current node */public Object getCurrentData();/** Task: Determines whether current node contains an answer.* @return true if the current node is a leaf */public boolean isAnswer();/** Task: Moves the current node to the left (right) child of the current node. */public void advanceToNo();public void advanceToYes();/** Task: Sets the current node to the root of the tree.*/public void reset();

} // end DecisionTreeInterface

Page 31: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

31

Decision Trees

Fig. 24-16 An initial decision tree for a guessing game.

Page 32: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

32

Decision Trees

Fig. 24-17 The decision tree for a guessing game after acquiring another fact.

Page 33: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

33

Binary Search Trees

A search tree organizes its data so that a search is more efficient

Binary search tree • Nodes contain Comparable objects• A node's data is greater than the data in the

node's left subtree• A node's data is less than the data in the

node's right subtree

Page 34: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

34

Binary Search Trees

Fig. 24-18 A binary search tree of names.

Page 35: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

35

Binary Search Trees

Fig. 24-19 Two binary search trees containing the same names as the tree in Fig. 24-18

Page 36: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

36

Binary Search Trees

An algorithm for searching a binary search tree

Algorithm bstSearch(binarySearchTree, desiredObject)// Searches a binary search tree for a given object.// Returns true if the object is found.if (binarySearchTree is empty)

return falseelse if (desiredObject == object in the root of binarySearchTree)

return trueelse if (desiredObject < object in the root of binarySearchTree)

return bstSearch(left subtree of binarySearchTree, desiredObject)else

return bstSearch(right subtree of binarySearchTree, desiredObject)

Page 37: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

37

Heaps

A complete binary tree• Nodes contain Comparable objects• Each node contains no smaller (or no larger)

than objects in its descendants

Maxheap• Object in a node is ≥ its descendant objects

Minheap• Object in a node is ≤ descendant objects

Page 38: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

38

Heaps

Fig. 24-20 (a) A maxheap and (b) a minheap that contain the same values

Page 39: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

39

Examples of General Trees

Fig. 24-21 A parse tree for the algebraic

expression a * (b + c)

Page 40: Trees Chapter 24. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals

40

Examples of General Trees

Fig. 24-22 A portion of a game tree for tic-tac-toe