Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees...

47
Trees •What is a tree? •Binary Trees •Tree traversal •Implementation •Tree Applications –decision trees –game trees –heaps –binary search trees
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    237
  • download

    1

Transcript of Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees...

Page 1: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Trees

•What is a tree?

•Binary Trees

•Tree traversal

•Implementation

•Tree Applications–decision trees –game trees–heaps–binary search trees

Page 2: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Non-linear data structures

• Lists are linear structures– each node has at most one successor and one

predecessor

• There are a number of data structures in which each node can point to more that one other node– graphs– trees

Page 3: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Graphs

• a graph is a data structure designed to represent the connectivity between a group of objects

• a graph consists of a set of objects (vertices) and a collection of connections (edges) between pairs of objects

• edges may be directed (one-way) or undirected (two-way)

• a directed acyclic graph has no cycles - there is no path from a node back to itself

Page 4: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Graphs

Page 5: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

(as used in graph theory)

A tree (undirected) is ... an empty tree (i.e. zero nodes) or a node together with zero or more subtrees

G M DR W

H K Z

FB

A

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

a tree is a special case of a directed acyclic graph

trees can be defined recursively

Page 6: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

A tree (undirected) is ... an empty tree (i.e. zero nodes) or a node together with zero or more subtrees

A rooted tree (sometimes called a directed tree) is ...a tree in which each node is connected by an arc from itself to any node in its subtree.

K

G M DR W

ZH

FB

A

X

The tree’s root is the only node with no incoming arcs.

Each arc defines a parent (arc origin) and a child (arc destination). The terms ancestor and descendant follow the parent/child directions.

A leaf is any node with no outgoing arcs.

The level of a node is its distance (number of arcs) from the root.

The height of a tree is the maximum level of all its nodes.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 7: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

A tree (undirected) is ... an empty tree (i.e. zero nodes) or a node together with zero or more subtrees

A rooted tree (sometimes called a directed tree) is ...a tree in which each node is connected by an arc from itself to any node in its subtree.

K

G

M

DR

W

ZH

FB

A

X

A binary tree is a rooted tree with two additional properties.

1) Each child is either a right child or left child. 2) A parent can have one left and/or one right child.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 8: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

«interface»BinaryTree

«query» + boolean isEmpty( ) + BinTreeIterator iteratorAtRoot( )«update» + void makeRoot(Object z) + void clear( )

«interface»BinTreeIterator

«query» + BinTreeIterator clonedIt( ) + Object item( ) + boolean isOff( ) + boolean isRoot( ) + boolean hasLeft( ) + boolean hasRight( ) + BinTreeIterator left( ) + BinTreeIterator right( )«update» + void setNodeContent(Object z) + void sproutLeft(Object z) + void sproutRight(Object z) + void pruneLeft( ) + void pruneRight( )

Sample Code(assuming theTree conforms to BinaryTree)

theTree.clear();theTree.makeRoot(“A”);BinTreeIterator theIt = theTree.iteratorAtRoot();theIt.sproutLeft(“T”);theIt.sproutRight(“R”);theIt.left().sproutLeft(“E”);theIt = theIt.right();theIt.sproutLeft(“E”);theTree.iteratorAtRoot().setNodeContent(“Z”);

EE

RT

Z

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 9: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

A tree traversal algorithm is a procedure for visiting every node in a tree.

The most common tree traversal algorithms are considereddepth-first algorithms.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

An iterator for a tree needs to use some traversal algorithm.

KGMDRW ZH FBA

X

Page 10: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

K

G

M

DR

W

ZH

FB

A

X

A preorder traversal visits the root and then traverses the left subtree followed by the right subtree.

public void preOrder(BinTreeIterator it) { if (!it.isOff()) { visitNodeAt(it); preOrder( it.left() ); preOrder( it.right() ); }}

Visiting orderA, B, H, G, R, F, K, W, D, M, X, Z

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 11: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

K

G

M

DR

W

ZH

FB

A

X

public void inOrder(BinTreeIterator it) { if (!it.isOff()) { inOrder( it.left() ); visitNodeAt(it); inOrder( it.right() ); }}

Visiting orderG, R, H, B, A, K, M, D, X, W, F, Z

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

An inorder traversal traverses the left subtree then visits the root and finally traverses the right subtree.

Page 12: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

K

G

M

DR

W

ZH

FB

A

X

public void postOrder(BinTreeIterator it) { if (!it.isOff()) { postOrder( it.left() ); postOrder( it.right() ); visitNodeAt(it); }}

Visiting orderR, G, H, B, M, X, D, W, K, Z, F, A

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

A postorder traversal traverses the left subtree followed by the right subtree and visits the root node last.

Page 13: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Expression Trees

• arithmetic expressions can be represented in the form of trees– leaf nodes are operands

– internal nodes are operators

• use the traversal algorithms to put the expression into prefix, postfix and infix form

+*54+3

Page 14: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

There are three popular representations of a binary tree. reference - using one dynamic cell per tree node with links from parent to children

array - store each node in an array cell of predefined location

recursive - a tree object contains its root node value and references to its

subtrees (objects of the same type). A

D E

H

B

J M

F

C

This lecture examines the first two.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 15: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Each binary tree node can belong to the BinTreeNode class.private class BinTreeNode {

public Object content; public BinTreeNode leftLink; public BinTreeNode rightLink;

public BinTreeNode(Object z) { content = z; leftLink = null; rightLink = null; }}

A BinTreeNode object...

to left child to right child

:BinTreeNode

content == nodeContent leftLink rightLink

Note: This is designed foruse as a local class.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 16: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

An abstract tree

Its representation

:BinTreeNode

content == “A” leftLink rightLink

A

D E F

B C:BinTreeNode

content == “C”leftLinkrightLink == null

:BinTreeNode

content == “B” leftLink rightLink

:BinTreeNode

content == “E”leftLinkrightLink == null

:BinTreeNode

content == “F”leftLink == nullrightLink

:BinTreeNode

content == “D”leftLinkrightLink == null

H J M

:BinTreeNode

content == “M”leftLink == nullrightLink == null

:BinTreeNode

content == “H”leftLink == nullrightLink == null

:BinTreeNode

content == “J”leftLink == nullrightLink == null

Page 17: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

«interface»BinaryTree

«query» + boolean isEmpty( ) + BinTreeIterator iteratorAtRoot( )«update» + void makeRoot(Object z) + void clear( )

«interface»BinTreeIterator

«query» + BinTreeIterator clonedIt( ) + Object item( ) + boolean isOff( ) + boolean isRoot( ) + boolean hasLeft( ) + boolean hasRight( ) + BinTreeIterator left( ) + BinTreeIterator right( )«update» + void setNodeContent(Object z) + void sproutLeft(Object z) + void sproutRight(Object z) + void pruneLeft( ) + void pruneRight( )

Assume that the following interfaces define the desired behavior for the binary tree.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 18: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

BinaryLinkedTree - BinTreeNode rootNode

«constructor» + BinaryLinkedTree( ) «query» + boolean isEmpty( ) + BinTreeIterator iteratorAtRoot()«update» + void makeRoot(Object z) + void clear( )

«interface»BinaryTree

«query» + boolean isEmpty( ) + BinTreeIterator iteratorAtRoot( )«update» + void makeRoot(Object z) + void clear( )

«interface»BinTreeIterator

«query» + BinTreeIterator clonedIt( ) + Object item( ) + boolean isOff( ) + boolean isRoot( ) + boolean hasLeft( ) + boolean hasRight( ) + BinTreeIterator left( ) + BinTreeIterator right( )«update» + void setNodeContent(Object z) + void sproutLeft(Object z) + void sproutRight(Object z) + void pruneLeft( ) + void pruneRight( )

- BinTreeNode

+ Object content + BinTreeNode leftLink + BinTreeNode rightLink

«constructor» + BinTreeNode(Object z)

- BTIterator

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 19: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

A key portion of the BinaryLinkedTree implementation is the private BTIterator class.

BinaryLinkedTree - BinTreeNode rootNode

«constructor» + BinaryLinkedTree( ) «query» + boolean isEmpty( ) + BinTreeIterator iteratorAtRoot()«update» + void makeRoot(Object z) + void clear( )

...public BinTreeIterator iteratorAtRoot() { return new BTIterator(rootNode);

}...

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 20: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

- BTIterator

- BinTreeNode theNode

«constructor» + BTIterator(BinTreeNode n)«query» + BinTreeIterator clonedIt( ) + Object item( ) + boolean isOff( ) + boolean isRoot( ) + boolean hasLeft( ) + boolean hasRight( ) + BinTreeIterator left( ) + BinTreeIterator right( )«update» + void setNodeContent(Object z) + void sproutLeft(Object z) + void sproutRight(Object z) + void pruneLeft( ) + void pruneRight( )

private class BTIterator implements BinTreeIterator {

private BinTreeNode theNode;

public BTIterator(BinTreeNode n) { theNode = n; } public BinTreeIterator clonedIt() { return new BTIterator(theNode); }

public Object item() { return theNode.content; }

public boolean isOff() { return (theNode == null); } ...}

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 21: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

A second possible representation for a binary tree is to store each node in a separate array cell.

The basic idea is to store node by level (first level 0, then level 1, ...) and within each level to store nodes left to right.

A

D E

B

F G

C

Example tree A :Object

B :Object

C :Object

D :Object

E :Object

F :Object

G :Object

array representation

treeCell :Object[ ]

[0][1][2][3][4][5][6]

Problem: What if the tree is incomplete (i.e., some potential nodes are missing)?Solution: Keep a parallel Boolean array. (see next slide) The Object of Data Abstraction

and Structure, David D. Riley© Addison Wesley pub.

Page 22: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Example treeA

D

I

B

O

F G

C

Example tree (with potentials)

A

D

I

B

O

F G

C

An incomplete tree can be thought of asa tree with “potential” nodes.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 23: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Example tree (with potentials)

A

D

I

B

O

F G

CA :Object

B :Object

C :Object

D :Object

F :Object

G :Object

representation

someTree :BinaryArrayTree

inUse treeCell :boolean[ ] :Object[ ]

[0][1][2][3][4][5][6][7][8][9]

[10][11][12][13][14]

I :Object

O :Object

[0][1][2][3][4][5][6][7][8][9]

[10][11][12][13][14]

truetruetruetrue

truetrue

true

true

false

false

falsefalsefalsefalsefalse

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 24: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

A

D E

B

F G

C

A :Object

B :Object

C :Object

D :Object

E :Object

F :Object

G :Object

treeCell :Object[ ]

[0][1][2][3][4][5][6]

Question:How is a tree iterator

represented?Answer:

As a single integer -- an array indexExample:

What if the index == 3?

Page 25: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Assume that treeIt is an int variable storing an iterator for our array implementation.

Give a formula for the left child of treeIt:

A

D E

B

F G

C

A :Object

B :Object

C :Object

D :Object

E :Object

F :Object

G :Object

treeCell :Object[ ]

[0][1][2][3][4][5][6]

treeIt * 2 + 1

Give a formula for the right child of treeIt:

(treeIt+1) * 2

Give a formula for the parent of treeIt:

(treeIt-1) / 2

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 26: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Note that a single data set can be represented by multiple BSTs.

4

0

2 6

Example A

6

2

4

Example B

0

0

2

Example C

4

62

4

0 6

Example D

Are there others for {0, 2, 4, 6}?

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 27: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Tree Applications

• Representing hierarchical data

• Decision Trees

• Game Trees

• Sorting (heaps and binary search trees)

• Set representation

Page 28: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Decision Trees

Page 29: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Coin Puzzle• Starting position

• Ending position

• Valid moves– coin can move one cell in either direction – coin cannot be placed on top of a smaller coin

• Goal– get from start to end configuration in a minimal number of moves

NDPQ01234

NDPQ

Page 30: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Solving the Puzzle

• A tree can be used to represent the various possible sequences of moves

• represent a configuration with a 4-digit base 5 number– one digit for each coin– value of digit is cell coin is in

• start = 0123

• end = 4321

Page 31: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Decision Tree for Coin Puzzle

Page 32: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Game Trees

• In order to program a computer to play a game, you need a way for the computer to determine the best move.– minmax procedure allows the computer to trace

all possible games through a specified number of turns

– alpha-beta pruning reduces the number of branches in the evaluation tree

Page 33: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Ordered Trees

• by putting constraints on the way nodes are put into a tree, searching through a collection of data can be made more efficient– heaps– binary search trees

Page 34: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Suppose that a tree contains node values that are Comparable.

This would permit ordering node content according to < and >.

The best known of all such trees is called a Binary Search Tree.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 35: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

DEFNA binary search tree has two properties:

1) It is a binary tree of nodes that are Comparable.

2) For every node the content of its left subtree is less than the node’s

own content and the right subtree is greater.

7

1 6

0

3

4

8

9

Example BST

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 36: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Why aren’t these binary search trees?

7

1 6

0

3

4

9

8

Example 1

5

5

2 9

Example 2

3

20 4

Example 3

4

0 3

5

2

1

6

7

Example 4

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 37: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

The algorithm for searching for a particular value within a BST is...

/* assert: treeIt is of type BinaryTreeIterator and is positioned at the BST’s root */

while (!treeIt.isOff()&& !searchVal.equals(treeIt.item()))

{ if (searchVal.compareTo(treeIt.item()) < 0) treeIt = treeIt.left(); else treeIt = treeIt.right();}

/* assert: searchVal is within the tree implies treeIt.item().equals(searchVal)and searchVal is not within the tree implies treeIt.isOff()

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 38: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

The algorithm for inserting a value within a BST follows the pattern of the BST search.

5

Example: Begin by inserting 5 into an empty tree.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 39: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

The algorithm for inserting a value within a BST follows the pattern of the BST search.

5

4

Example: Next insert 4.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 40: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

5

4

The algorithm for inserting a value within a BST follows the pattern of the BST search.

1

Example: Next insert 1.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 41: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

5

1

4

The algorithm for inserting a value within a BST follows the pattern of the BST search.

7

Example: Next insert 7.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 42: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

5

1

4 7

The algorithm for inserting a value within a BST follows the pattern of the BST search.

8

Example: Next insert 8.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 43: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

5

1

4

8

7

The algorithm for inserting a value within a BST follows the pattern of the BST search.

9

Example: Next insert 9.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 44: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

5

1

4

9

8

7

The algorithm for inserting a value within a BST follows the pattern of the BST search.

6

Example: Next insert 6.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 45: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

5

1

4

9

6 8

7

The algorithm for inserting a value within a BST follows the pattern of the BST search.

2

Example: Finally, insert 2.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 46: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

Given a binary tree of N nodes, how many must be probed to ensure that some search value is found? N

Given a complete BST of N nodes, how many must be probed to ensure that some search value is found? log2N (approximately)

More Efficient Searching

TreesortA BST is sorted. Which of the tree traversalalgorithms visits nodes in ascending order? in order

How many probes are required to sort a treevia treesort (in the best case)? Nlog2N (approximately)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 47: Trees What is a tree? Binary Trees Tree traversal Implementation Tree Applications –decision trees –game trees –heaps –binary search trees.

What is the BST shape that is least efficient to search?

BalanceBalance is the bushiness property of a tree.

A tree in which no node has two children is maximally out of balance.

A complete tree is perfectly balanced.

An AVL tree is a BST for which every node has the following property: its left and right subtree’s vary in height by at most one.

4

0

2 6

BST 1

0

2

BST 2

4

6

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.