Top Banner
Trees Chapter 15
49

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

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Trees Chapter 15. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.

Trees

Chapter 15

Page 2: Trees Chapter 15. 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 15. 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 15. 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

Computer files organized into folders.

Page 5: Trees Chapter 15. 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

A university's administrative structure.

Page 6: Trees Chapter 15. 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

Carole's children and grandchildren.

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

7

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 8: Trees Chapter 15. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.

8

Tree Terminology

A tree equivalent to the my stuff tree

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

9

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 10: Trees Chapter 15. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.

10

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 11: Trees Chapter 15. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.

11

Binary Trees General tree: in general, node can have an arbitrary

number of children Binary tree if each node has at most two children

Three binary trees.

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

12

Binary Trees

A binary tree is either empty or has the following form

Where Tleft and Tright are binary trees

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

13

Binary Trees If a binary tree of height h has all leaves on the same level h and

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

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

14

Binary Trees

The number of nodes in a full

binary tree as a function of the tree's height.

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

15

Binary Trees

Total number of nodes n for a full tree can be calculated as:

The height of a binary tree with n nodes that is either complete or full is

log2(n + 1)

1

0

2 2 1h

i h

i

n

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

16

Traversals of a Tree Traversing items is a common operation.

Previous chapters, data was arranged linearly. Order of traversal was clear.

During tree traversal, we must visit each item exactly once in a systematic way. The order is not unique.

Visiting a node Processing the data within a node

A traversal can pass through a node without visiting it at that moment

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

17

Traversals of a Tree

Subtree of the root of binary trees are themselves binary trees. Using this recursive nature, we define recursive traversal:

For a binary tree Visit the root Visit all nodes in the root's left subtree Visit all nodes in the root's right subtree

Order does not matter. Visit root before, between or after visiting two subtrees.

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

18

Traversals of a Tree

Preorder traversal: visit root before the subtrees

The visitation order of a preorder traversal.

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

19

Traversals of a Tree

Inorder traversal: visit root between visiting the subtrees

The visitation order of an inorder traversal.

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

20

Traversals of a Tree

Postorder traversal: visit root after visiting the subtrees

The visitation order of a postorder traversal.

These are examples of a depth-first traversal.

Follow a path that descends the levels of a

tree as deeply as possible until reaches a leaf

These are examples of a depth-first traversal.

Follow a path that descends the levels of a

tree as deeply as possible until reaches a leaf

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

21

Traversals of a Tree

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

The visitation order of a level-order traversal.

This is an example of a breadth-first

traversal.

Follow a path that explores an

entire level before moving to the next level.

This is an example of a breadth-first

traversal.

Follow a path that explores an

entire level before moving to the next level.

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

22

Traversals Exercise

The order of these nodes being visited using 4 different traversal methods

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

23

Answer

In this binary tree, D = node, L = left, R = right

Preorder (DLR) traversal yields: A, H, G, I, F, E, B, C, D

Postorder (LRD) traversal yields: G, F, E, I, H, D, C, B, A

In-order (LDR) traversal yields: G, H, F, I, E, A, B, D, C

Level-order traversal yields: A, H, B, G, I, C, F, E, D

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

24

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 25: Trees Chapter 15. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.

25

Traversals of a General Tree

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

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

26

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 27: Trees Chapter 15. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.

27

Expression Trees for Algebraic Expression The root contains the binary operator and children contain the operands. Order of children matches the order of operands. Such binary tree is called

expression Trees. No parentheses since order of operations is captured by the shape of the expression tree

Expression trees for four algebraic expressions..

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

28

Expression Trees Inorder traversal produces the original infix expression, but

without any parentheses Preorder traversal produces the prefix expression Postorder traversal produces the postfix expression (b) preorder: +*abc postorder: ab*c+

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

29

Expression Trees

Algorithm for evaluating an expression tree in postorder traversal

Algorithm evaluate(expressionTree)if (root of tree is operand)

return operand

else{ 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 30: Trees Chapter 15. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.

30

Exercises

Draw an expression tree for each of these algebraic expressions.

a. a+b*c b. (a+b)*c

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

31

Decision Trees Basis of Expert System

An expert system also known as a knowledge based system that contains some knowledge. It was first developed by researchers in artificial intelligence.

The most common form of expert systems is made up of a set of rules that analyze information.

Each parent in a decision tree is a question that has a finite number of response.

Each possible answer to the question corresponds to a child of the node.

Leave node are conclusions that have no children

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

32

Decision Trees

Helps users solve problems, make decisions

A binary decision tree to solve TV problem.

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

33

Decision Trees A 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 34: Trees Chapter 15. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.

34

Decision Trees

The decision tree for a guessing game

Facts are used to build this decision tree.

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

35

Binary Search Trees

We can traverse nodes in any tree, so searching a node is possible. The least efficient search will be sequential search.

However, 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 36: Trees Chapter 15. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.

36

Binary Search Trees

A binary search tree of names.

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

37

Binary Search Trees

Two binary search trees containing the same names as the tree in previous slide

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

38

Questions

How many different binary search trees can you form from the strings a, b, and c?

What are the heights of the shortest and tallest trees that you formed?

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

39

Answer

How many different binary search trees can you form from the strings a, b, and c?

5 different structures What are the heights of the shortest and tallest trees

that you formed? 2 and 3

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

40

Binary Search Trees

Search begins at the root, ends at either the target node or leaf node.

The number of comparison is the number of nodes along the path from the root to target node.

The height of a tree directly affects the length of the longest path from the root to a leaf and hence affects the efficiency of a worst-case search.

Searching a binary search tree of height h is O(h).

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

41

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 42: Trees Chapter 15. 2 Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.

42

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. Root node contains the largest data

Minheap Object in a node is ≤ descendant objects Root node contains the smallest data

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

43

Heaps

(a) A maxheap and (b) a minheap that contain the same values

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

44

Question?

Can a binary search tree ever be a maxheap?

Can you think of any ADT we have learnt so far that can be implemented by maxheap?

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

45

Parse Tree for Compiler These rules form a grammar for algebraic

expression, much like English language grammar. An algebraic expression is either a term or two terms

separated by + or – operator A term is either a factor or two factors separated by * or /

operator. A factor is either a variable or an algebraic expression

enclosed in parentheses. A variable is a single letter

Check the syntax of each statement by applying these rules, if we can the derivation can be given as a parse tree.

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

46

Parse TreeA parse tree for the

algebraic expression

a * (b + c)1). An algebraic expression is either a term or two terms separated by + or – operator

2). A term is either a factor or two factors separated by * or / operator.

3). A factor is either a variable or an algebraic expression enclosed in parentheses.

4). A variable is a single letter

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

47

Parse Tree

Parse tree should be a general tree so that I can accommodate any expression.

We are not restricted to algebraic expression. We can use parse tree to check the validity of

any string according to any grammar Compiler uses parse tree both to check the

syntax of a program and to produce executable code.

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

48

Exercise

Draw a parse tree for the algebraic expression a*b + c

a + b*c (a+b)*(c-d)

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

49

Answer<Expression>

<term> <term>

<Variable>

+

<factor> <factor>*

<Variable>

<factor>

<factor>

Cba