53
Data Structure & Algorithms in JAVA 5 th edition Michael T. Goodrich Roberto Tamassia Chapter 7: Trees CPSC 3200 Algorithm Analysis and Advanced Data Structure

General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Embed Size (px)

Citation preview

Page 1: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Data Structure & Algorithms in JAVA

5th editionMichael T. GoodrichRoberto Tamassia

Chapter 7: TreesCPSC 3200

Algorithm Analysis and Advanced Data Structure

Page 2: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Chapter Topics• General Trees.• Tree Traversal Algorithms.• Binary Trees.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 3: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

3

What is a Tree

• In computer science, a tree is an abstract model of a hierarchical structure.

• A tree consists of nodes with a parent-child relation.

• Applications:• Organization charts.• File systems.• Programming

environments.

Computers”R”Us

Sales

R&D

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 4: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

4

Subtree

Tree Terminology• Root: node without parent (A)• Internal node: node with at least

one child (A, B, C, F)• External node (a.k.a. leaf ): node

without children (E, I, J, K, G, H, D)• Ancestors of a node: parent,

grandparent, grand-grandparent, etc.

• Depth of a node: number of ancestors

• Height of a tree: maximum depth of any node (3)

• Descendant of a node: child, grandchild, grand-grandchild, etc.

A

B DC

G HE F

I J K

• Subtree: tree consisting of a node and its descendants.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 5: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

5

Tree Terminology (Cont.)• edge of tree T is a pair of nodes

(u,v) such that u is the parent of v, or vice versa.

• Path of T is a sequence of nodes such that any two consecutive nodes in the sequence form an edge.

• A tree is ordered if there is a linear ordering defined for the children of each node

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 6: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

6

Tree ADT• We use positions (nodes) to

abstract nodes.• getElement( ): Return the

object stored at this position.• Generic methods:• integer getSize( )• boolean isEmpty( )• Iterator iterator( )• Iterable positions( )

• Accessor methods:• position getRoot( )• position getThisParent(p)• Iterable children(p)

• Query methods:• boolean isInternal(p)• boolean isExternal(p)• boolean isRoot(p)

• Update method:• element replace (p, o)

• Additional update methods may be defined by data structures implementing the Tree ADT.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 7: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

7

Linked structure for General Tree

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 8: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

8

Depth and Height• Let v be a node of a tree T. The depth of v is the number of ancestors

of v, excluding v itself.• If v is the root, then the depth of v is 0• Otherwise, the depth of v is one plus the depth of the parent of v.

• The running time of algorithm depth(T, v) is O(dv), where dv denotes the depth of the node v in the tree T.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Algorithm depth(T, v):if v is the root of T then

return 0else

return 1+depth(T, w), where w is the parent of v in T

© 2010 Goodrich, Tamassia

Page 9: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

9

Data Structure (Tree)

• A tree is a data structure which stores elements in parent-child relationship.

A

B C

D E F G H

Root node

Internal nodesLeaf nodes (External nodes)

Siblings

Siblings

Siblings

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 10: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

10

Attributes of a tree

• Depth: the number of ancestors of that node (excluding itself).

• Height: the maximum depth of an external node of the tree/subtree.

A

B C

D E F G H

I

Depth(D) = ?Depth(D) = 1Depth(D) = 2

Depth(I) = ?Depth(I) = 3

Height = MAX[ Depth(A), Depth(B), Depth(C), Depth(D), Depth(E), Depth(F), Depth(G),

Depth(H), Depth(I) ]Height = MAX[ 0, 1, 1, 2, 2, 2, 2, 2, 3 ] = 3CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 11: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

11

Depth and Height (Cont.)• The height of a node v in a tree T is can be calculated using the

depth algorithm.

• algorithm height1 runs in O(n2) time

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Algorithm height1(T):h ← 0for each vertex v in T do

if v is an external node in T thenh ← max(h, depth(T, v))

return h

© 2010 Goodrich, Tamassia

Page 12: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

12

Depth and Height (Cont.)• The height of a node v in a tree T is also defined recursively:• If v is an external node, then the height of v is 0• Otherwise, the height of v is one plus the maximum height of a

child of v.

• algorithm height1 runs in O(n) time

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Algorithm height2(T, v):if v is an external node in T then

return 0else

h ← 0for each child w of v in T do

h ← max(h, height2(T, w))return 1+h

© 2010 Goodrich, Tamassia

Page 13: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

13

Preorder Traversal

• A traversal visits the nodes of a tree in a systematic manner.

• In a preorder traversal, a node is visited before its descendants.

• Application: print a structured document.

Make Money Fast!

1. Motivations

References

2. Methods

2.1 StockFraud

2.2 PonziScheme

1.1 Greed

1.2 Avidity

2.3 BankRobbery

1

2

3

5

4 6 7 8

9

Algorithm preOrder(v)visit(v)for each child w of v

preorder (w)

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 14: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

14

Postorder Traversal

• In a postorder traversal, a node is visited after its descendants.

• Application: compute space used by files in a directory and its subdirectories.

Algorithm postOrder(v)for each child w of v

postOrder (w)visit(v)

cs16/

homeworks/

todo.txt1K

programs/

DDR.java10K

Stocks.java25K

h1c.doc3K

h1nc.doc2K

Robot.java20K

9

3

1

7

2 4 5 6

8

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 15: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

15

Tree traversals using “flags”

• The order in which the nodes are visited during a tree traversal can be easily determined by imagining there is a “flag” attached to each node, as follows:

• To traverse the tree, collect the flags:

preorder inorder postorder

A

B C

D E F G

A

B C

D E F G

A

B C

D E F G

A B D E C F G D B E A F C G D E B F G C ACPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 16: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

16

Other traversals• The other traversals are the reverse of these three standard

ones• That is, the right subtree is traversed before the left subtree is

traversed

• Reverse preorder: root, right subtree, left subtree.• Reverse inorder: right subtree, root, left subtree.• Reverse postorder: right subtree, left subtree, root.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 17: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

17

Binary Trees• A binary tree is a tree with the following

properties:• Each internal node has at most two

children (exactly two for proper binary trees).• The children of a node are an ordered

pair.• We call the children of an internal node

left child and right child.• Alternative recursive definition: a binary

tree is either• a tree consisting of a single node, or• a tree whose root has an ordered pair of

children, each of which is a binary tree.

A

B C

F GD E

H I

Applications:• arithmetic expressions.• decision processes.• searching.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 18: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

18

Tree Balance

• A binary tree is balanced if every level above the lowest is “full” (contains 2h nodes)

• In most applications, a reasonably balanced binary tree is desirable.

a

b c

d e f g

h i jA balanced binary tree

a

b

c

d

e

f

g h

i jAn unbalanced binary tree

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 19: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

19

Decision Tree• Binary tree associated with a decision process• internal nodes: questions with yes/no answer• external nodes: decisions

• Example: dining decision

Want a fast meal?

How about coffee?

On expense account?

Starbucks Spike’s Al Forno Café Paragon

Yes No

Yes No Yes No

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 20: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

20

Arithmetic Expression Tree

• Binary tree associated with an arithmetic expression• internal nodes: operators• external nodes: operands

• Example: arithmetic expression tree for the expression (2 (a - 1) + (3 b))

+

-2

a 1

3 b

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 21: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

21

Proper Binary Tree

• Is a binary tree where the number of external nodes is 1 more than the number of internal nodes.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 22: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

22

Proper Binary Tree

• Is a binary tree where the number of external nodes is 1 more than the number of internal nodes.

A

B C

D

Internal nodes = 2 External nodes = 2

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 23: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

23

Proper Binary Tree

• Is a binary tree where the number of external nodes is 1 more than the number of internal nodes.

A

B C

D

Internal nodes = 2 External nodes = 3

E

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 24: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

24

Proper Binary Tree

• Is a binary tree where the number of external nodes is 1 more than the number of internal nodes.

A

B C

D

Internal nodes = 3 External nodes = 3

E F

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 25: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

25

Proper Binary Tree

• Is a binary tree where the number of external nodes is 1 more than the number of internal nodes.

A

B C

D

Internal nodes = 3 External nodes = 4

E F G

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 26: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

26

Worst case: The tree having the minimum number of external and internal nodes.

Best case: The tree having the maximum number of external and internal nodes.

Properties of a Proper Binary Tree

1. The number of external nodes is at least h+1 and at most 2h

Ex: h = 3

External nodes = 3+1 = 4

External nodes = 23 = 8

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 27: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

27

Properties of a Proper Binary Tree

2. The number of internal nodes is at least h and at most 2h-1

Ex: h = 3Worst case: The tree having the minimum number of external and internal nodes.

Best case: The tree having the maximum number of external and internal nodes.

Internal nodes = 3

Internal nodes = 23 -1=7

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 28: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

28

Properties of a Proper Binary Tree

3. The number of nodes is at least 2h+1 and at most 2h+1 -1 Ex: h = 3

Internal nodes = 3 External nodes = 4----------------------------

Internal + External = 2*3 +1 = 7

Internal nodes = 7 External nodes = 8

-----------------------Internal + External = 23+1 – 1

= 15

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 29: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

29

Properties of a Proper Binary Tree

4. The height is at least log(n+1)-1 and at most (n-1)/2

Number of nodes = 7h = 3

Number of nodes = 15

h = 3

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 30: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

30

BinaryTree ADT

• The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT.

• Additional methods:• position getThisLeft(p)• position getThisRightight(p)• boolean hasLeft(p)• boolean hasRight(p)

• Update methods may be defined by data structures implementing the BinaryTree ADT.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 31: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

31

Linked Structure for Binary Trees

• A node is represented by an object storing• Element• Parent node• Left child node• Right child node

• Node objects implement the Position ADT

B

DA

C E

B

A D

C E

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 32: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

32

Binary Tree - Example

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 33: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

33

Implementation of the Linked Binary Tree Structure

• addRoot(e): Create and return a new node r storing element e and make r the root of the tree; an error occurs if the tree is not empty.

• insertLeft(v, e): Create and return a new node w storing element e, add w as the the left child of v and return w; an error occurs if v already has a left child.

• insertRight(v ,e): Create and return a new node z storing element e, add z as the the right child of v and return z; an error occurs if v already has a right child.

• remove(v): Remove node v, replace it with its child, if any, and return the element stored at v; an error occurs if v has two children.

• attach(v, T1, T2): Attach T1 and T2, respectively, as the left and right subtrees of the external node v; an error condition occurs ifv is not external.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 34: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

34

Binary Search Tree (BST)

• Binary trees are excellent data structures for searching large amounts of information.

• When used to facilitate searches, a binary tree is called a binary search tree.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 35: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Binary Search Tree (BST)

• A binary search tree (BST) is a binary tree in which:• Elements in left subtree are smaller than the current node.• Elements in right subtree are greater than the current node.

10

7 12

5 9 11

25

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 35

Page 36: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

36

Traversing the tree

• There are three common methods for traversing a binary tree and processing the value of each node: • Pre-order• In-order• Post-order

• Each of these methods is best implemented as a recursive function.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 37: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

37

Tree Traversal (Pre-order)

• Pre-order: Node Left Right

A

B C

D E F G

A B D E C F GCPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 38: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

38

Exercise: Pre-order traversal

• Insert the following items into a binary search tree.50, 25, 75, 12, 30, 67, 88, 6, 13, 65, 68

• Draw the binary tree and print the items using Pre-order traversal.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 39: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

39

Tree Traversal (In-order)

• In-order: Left Node Right

A

B C

D E F G

D B E A F C GCPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 40: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

40

Exercise: In-order traversal

• From the previous exercise, print the tree’s nodes using In-order traversal.

50

25

75

12

30

67

88

6 13

65

68

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 41: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

41

Tree Traversal (Post-order)

• Post-order: Left Right Node

A

B C

D E F G

D E B F G C ACPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 42: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

42

Exercise: Post-order traversal

• From the previous exercise, print the tree’s nodes using Post-order traversal.

50

25

75

12

30

67

88

6 13

65

68

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 43: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

43

Inorder Traversal

• In an inorder traversal a node is visited after its left subtree and before its right subtree

• Application: draw a binary tree• x(v) = inorder rank of v• y(v) = depth of v

Algorithm inOrder(v)if hasLeft (v)

inOrder (left (v))visit(v)if hasRight (v)

inOrder (right (v))

3

1

2

5

6

7 9

8

4

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 44: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

44

Delete a node

• After deleting an item, the resulting binary tree must be a binary search tree.1. Find the node to be deleted.2. Delete the node from the tree.

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 45: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

45

Delete (Case 1)

• The node to be deleted has no left and right subtree (the node to be deleted is a leaf).

60

50

70

30

53

65

80

51

57

61

67

79

95

delete(30)

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 46: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

46

Delete (Case 2)

• The node to be deleted has no left subtree (the left subtree is empty but it has a nonempty right subtree).

60

50

70

30

53

65

80

35

51

57

61

67

79

95

delete(30)

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 47: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

47

Delete (Case 3)

• The node to be deleted has no right subtree (the right subtree is empty but it has a nonempty left subtree).

60

50

70

30

53

65

80

25

35

51

57

61

67

79

delete(80)

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 48: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

48

Delete (Case 4)

• The node to be deleted has nonempty left and right subtree.

60

50

70

30

53

65

80

25

35

51

57

61

67

79

95

delete(70)

79

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 49: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

49

Delete (Case 4)

• The node to be deleted has nonempty left and right subtree.

60

50

70

30

53

65

80

25

35

51

57

61

67

79

95

delete(70)

67

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013

Page 50: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

50

Binary Search• Binary search can perform operations get, floorEntry and

ceilingEntry on an ordered map implemented by means of an array-based sequence, sorted by key• similar to the high-low game• at each step, the number of candidate items is halved• terminates after O(log n) steps

• Example: find(7)

1 3 4 5 7 8 9 11 14 16 18 19

1 3 4 5 7 8 9 11 14 16 18 19

1 3 4 5 7 8 9 11 14 16 18 19

1 3 4 5 7 8 9 11 14 16 18 19

0

0

0

0

ml h

ml h

ml h

l=m =hCPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 51: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

51

Binary Search Trees

• A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes and satisfying the following property:• Let u, v, and w be three

nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w)

• External nodes do not store items.

• An inorder traversal of a binary search trees visits the keys in increasing order.

6

92

41 8

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 52: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

52

Search• To search for a key k, we trace

a downward path starting at the root.

• The next node visited depends on the comparison of k with the key of the current node.

• If we reach a leaf, the key is not found.

• Example: get(4):• Call TreeSearch(4,root)

Algorithm TreeSearch(k, v)if T.isExternal (v)

return vif k < key(v)

return TreeSearch(k, T.left(v))else if k = key(v)

return velse { k > key(v) }

return TreeSearch(k, T.right(v))

6

92

41 8

<

>=

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

Page 53: General Trees. Tree Traversal Algorithms. Binary Trees. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

53

End of Chapter 7

CPSC 3200 University of Tennessee at Chattanooga – Summer 2013