traversing a binary tree binary search tree insertion deleting from a binary search tree

207
Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree

Upload: vesta

Post on 22-Jan-2016

55 views

Category:

Documents


3 download

DESCRIPTION

Lecture 12. Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree. Traversing a Binary Tree Inorder Traversal. The Scenario. Imagine we have a binary tree We want to traverse the tree It’s not linear We need a way to visit all nodes - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Traversing a Binary TreeBinary Search Tree Insertion

Deleting from a Binary Search Tree

Page 2: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Traversing a Binary TreeInorder Traversal

Page 3: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

The Scenario

• Imagine we have a binary tree• We want to traverse the tree– It’s not linear– We need a way to visit all nodes

• Three things must happen:– Deal with the entire left sub-tree– Deal with the current node– Deal with the entire right sub-tree

Page 4: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

Page 5: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do left

Page 6: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do leftAt 9 – do left

Page 7: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do leftAt 9 – do leftAt 2 – do left

Page 8: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do leftAt 9 – do leftAt 2 – do left

At NIL

Page 9: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do leftAt 9 – do left

At 2 – do current

Page 10: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do leftAt 9 – do left

At 2 – do right

Page 11: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do leftAt 9 – do left

At 2 – do rightAt NIL

Page 12: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do leftAt 9 – do leftAt 2 - done

Page 13: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do leftAt 9 – do current

Page 14: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do leftAt 9 – do right

Page 15: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do leftAt 9 – do right

At NIL

Page 16: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do leftAt 9 – done

Page 17: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do current

Page 18: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do right

Page 19: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do rightAt 42 – do left

Page 20: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do rightAt 42 – do left

At NIL

Page 21: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do rightAt 42 – do current

Page 22: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do rightAt 42 – do right

Page 23: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do rightAt 42 – do right

At NIL

Page 24: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – do rightAt 42 – done

Page 25: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Use the Activation Stack

• With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off.

13

9

2

42

At 13 – done

Page 26: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Outline of In-Order Traversal

• Three principle steps:– Traverse Left– Do work (Current)– Traverse Right

• Work can be anything• Separate work from traversal

Page 27: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

•Traverse the tree “In order”:

–Visit the tree’s left sub-tree

–Visit the current and do work

–Visit right sub-tree

Page 28: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

In-Order Traversal Procedure

procedure In_Order(cur iot in Ptr toa Tree_Node)

// Purpose: perform in-order traversal, call

// Do_Something for each node

// Preconditions: cur points to a binary tree

// Postcondition: Do_Something on each tree

// node in “in-order” order if( cur <> NIL ) then In_Order( cur^.left_child ) Do_Something( cur^.data ) In_Order( cur^.right_child ) endif

endprocedure // In_Order

Page 29: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

Page 30: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

Page 31: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

Page 32: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

Page 33: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

Page 34: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

L

Page 35: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

L

Page 36: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

L

L

Page 37: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

L

L

Page 38: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

L

L

L

Page 39: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9

LPR

L

L

L

L

Page 40: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1

LPR

L

L

L

LP

Page 41: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1

LPR

L

L

L

LP

Page 42: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1

LPR

L

L

L

LPR

Page 43: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1

LPR

L

L

L

LPR

Page 44: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1

LPR

L

L

L

LPR

Page 45: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3

LPR

L

L

L

LPR

P

Page 46: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3

LPR

L

L

L

LPR

P

Page 47: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3

LPR

L

L

LPR

LPR

Page 48: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3

LPR

L

L

LPR

LPR

Page 49: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3

LPR

L

L

LPR

LPR

L

Page 50: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3

LPR

L

L

LPR

L

LPR

Page 51: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7

LPR

L

L

LPR

L

LPR

P

Page 52: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7

LPR

L

L

LPR

L

LPR

P

Page 53: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7

LPR

L

L

LPR

LPR

LPR

Page 54: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7

LPR

L

L

LPR

LPR

LPR

Page 55: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7

LPR

L

L

LPR

LPR

LPR

Page 56: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7

LPR

L

L

LPR

LPR

LPR

Page 57: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9

LPR

L

LP

LPR

LPR

LPR

Page 58: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9

LPR

L

LP

LPR

LPR

LPR

Page 59: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9

LPR

L

LPR

LPR

LPR

LPR

Page 60: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9

LPR

L

LPR

LPR

LPR

LPR

Page 61: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9

LPR

L

LPR

LPR

LPR

LPR

L

Page 62: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9

LPR

L

LPR

LPR

LPR

LPR

L

Page 63: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14

LPR

L

LPR

LPR

LPR

LPR

LP

Page 64: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14

LPR

L

LPR

LPR

LPR

LPR

LP

Page 65: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14

LPR

L

LPR

LPR

LPR

LPR

LPR

Page 66: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14

LPR

L

LPR

LPR

LPR

LPR

LPR

Page 67: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14

LPR

L

LPR

LPR

LPR

LPR

LPR

Page 68: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14

LPR

L

LPR

LPR

LPR

LPR

LPR

Page 69: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LP

LPR

LPR

LPR

LPR

LPR

Page 70: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Continue?

Yes!EnoughAlready!

Page 71: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LP

LPR

LPR

LPR

LPR

LPR

Page 72: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Page 73: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Page 74: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

Page 75: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

Page 76: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

L

Page 77: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

L

Page 78: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LP

Page 79: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LP

Page 80: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

Page 81: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

Page 82: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

L

Page 83: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

L

Page 84: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LP

Page 85: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LP

Page 86: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LPR

Page 87: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LPR

Page 88: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LPR

Page 89: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LPR

Page 90: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LPR

P

Page 91: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

LPR

LPR

P

Page 92: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Page 93: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Page 94: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

Page 95: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

Page 96: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LP

Page 97: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LP

Page 98: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Page 99: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Page 100: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

Page 101: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

L

Page 102: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LP

Page 103: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LP

Page 104: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Page 105: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Page 106: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Page 107: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Page 108: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Proc InOrderPrint(pointer)

pointer NOT NIL?

InOrderPrint(left child)

print(data)

InOrderPrint(right child)

22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Page 109: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Algorithm Example

. . .

InOrderPrint(root)

. . .22

root

67

363 14

447

94

971

9Output: 1 3 7 9 14 22 36 44 67 94 97

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

LPR

Page 110: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Summary

• An In-Order traversal visits every node– Recurse left first– Do something with current– Recurse right last

• The “left, current, right” logic is repeated recursively at every node.

• For a BST, an in-order traversal accesses the elements in ascending order.

Page 111: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Questions?

Page 112: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Binary Search Tree Insertion

Page 113: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Tree Node Defined

In general:Node definesa record data isoftype <type> left, right isoftype ptr toa Nodeendrecord

In this example:Node definesa record data isoftype num left, right isoftype ptr toa Nodeendrecord

Page 114: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Scenario

• We have a Binary Search Tree– It can be empty– Or have some elements in it already

• We want to add an element to it– Inserting/adding involves 2 steps:• Find the correct location• Do the steps to add a new node

• Must maintain “search” structure

Page 115: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Finding the Correct Location

12

3

7

41

98

Start withthis tree

Page 116: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Finding the Correct Location

12

3

7

41

98

Where would4 be added?

Page 117: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Finding the Correct Location

12

3

7

41

98

To the topdoesn’t work

4

Page 118: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Finding the Correct Location

12

3

7

41

98In the middledoesn’t work

4

Page 119: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Finding the Correct Location

12

3

7

41

98

At the bottomDOES work!

4

Page 120: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Finding the Correct Location

• Must maintain “search” structure– Everything to left is less than current– Everything to right is greater than current

• Adding at the “bottom” guarantees we keep search structure.

• We’ll recurse to get to the “bottom” (i.e. when current = nil)

Page 121: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Finding the Correct Location

if (current = nil)

DO “ADD NODE” WORK HERE

elseif (current^.data > value_to_add) then

// recurse left

Insert(current^.left, value_to_add)

else

// recurse right

Insert(current^.right, value_to_add)

endif

Page 122: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Adding the Node

• Current is an in/out pointer– We need information IN to evaluate current– We need to send information OUT because

we’re changing the tree (adding a node)

• Once we’ve found the correct location:– Create a new node– Fill in the data field

(with the new value to add)– Make the left and right pointers point to nil

(to cleanly terminate the tree)

Page 123: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Adding the Node

current <- new(Node)

current^.data <- value_to_add

current^.left <- nil

current^.right <- nil

Page 124: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

The Entire Module

procedure Insert(cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

Page 125: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Tracing Example

The following example shows a trace of the BST insert.

• Begin with an empty BST (a pointer)

• Add elements 42, 23, 35, 47 in the correct positions.

Page 126: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Head iot Ptr toa Nodehead <- NILInsert(head, 42)

Page 127: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Head iot Ptr toa Nodehead <- NILInsert(head, 42)

head

Page 128: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Head iot Ptr toa Nodehead <- NILInsert(head, 42)

head

Page 129: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Head iot Ptr toa Nodehead <- NILInsert(head, 42)

head

Page 130: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Head iot Ptr toa Nodehead <- NILInsert(head, 42)

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

head

Page 131: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Head iot Ptr toa Nodehead <- NILInsert(head, 42)

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

head

Page 132: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Head iot Ptr toa Nodehead <- NILInsert(head, 42)

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

head

42

Page 133: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Head iot Ptr toa Nodehead <- NILInsert(head, 42)

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

head

42

Page 134: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Head iot Ptr toa Nodehead <- NILInsert(head, 42)

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

head

42

Page 135: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Head iot Ptr toa Nodehead <- NILInsert(head, 42)

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

head

42

Page 136: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

Page 137: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 23

Page 138: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 23

Page 139: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 23

Page 140: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

Page 141: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

Page 142: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

23

Page 143: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

23

Page 144: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

23

Page 145: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

23

Page 146: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 23

23

Page 147: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

Page 148: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

Page 149: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

Page 150: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

Page 151: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

Page 152: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

Page 153: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

Page 154: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

Page 155: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

Page 156: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

35

Page 157: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

35

Page 158: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

35

Page 159: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

35

Page 160: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

35

Page 161: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

data_in = 35

35

Page 162: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

35

Page 163: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Continue?

yes... I’ve had enough!

Page 164: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

data_in = 47

35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

Page 165: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

data_in = 47

35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

Page 166: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

data_in = 47

35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

Page 167: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

data_in = 47

35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

Page 168: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

data_in = 47

35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

Page 169: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

data_in = 47

35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

47

Page 170: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

data_in = 47

35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

47

Page 171: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

data_in = 47

35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

47

Page 172: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

data_in = 47

35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

47

Page 173: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

data_in = 47

35

procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert

47

Page 174: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

head

42

.

.Insert(head, 23)Insert(head, 35)Insert(head, 47)..

23

35

47

Page 175: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Summary

• Preserve “search” structure!• Inserting involves 2 steps:– Find the correct location• For a BST insert, always insert at the

“bottom” of the tree– Do commands to add node• Create node• Add data• Make left and right pointers point to nil

Page 176: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Questions?

Page 177: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Deleting from a Binary Search Tree

(BST)

Page 178: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

The Scenario

• We have a Binary Search Tree and want to remove some element based upon a match.

• Must preserve “search” property• Must not lose any elements (i.e. only

remove the one element)

Page 179: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

BST Deletion

• Search for desired item.

• If not found, then return NIL or print error.

• If found, perform steps necessary to accomplish removal from the tree.

Page 180: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Four Cases for Deletion

• Delete a leaf node• Delete a node with only one child (left)• Delete a node with only one child (right)• Delete a node with two children

Cases 2 and 3 are comparable and only need slight changes in the conditional statement used

Page 181: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Leaf Node

Simply use an “in/out” pointer and assign it to “nil”. This will remove the node from the tree.

cur <- nil

14

50

94

9 71 116

1086613

42

Page 182: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Leaf Node

Simply use an “in/out” pointer and assign it to “nil”. This will remove the node from the tree.

cur <- nil

Let’s delete 42.

14

50

94

9 71 116

1086613

42

Page 183: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Leaf Node

Simply use an “in/out” pointer and assign it to “nil”. This will remove the node from the tree.

cur <- nil

Move the pointer;now nothing pointsto the node.

14

50

94

9 71 116

1086613

42

Page 184: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Leaf Node

Simply use an “in/out” pointer and assign it to “nil”. This will remove the node from the tree.

cur <- nil

The resulting tree.

14

50

94

9 71 116

1086613

Page 185: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with One Child

Use an “in/out” pointer.

Determine if it has a left or a right child.

Point the current pointer to the appropriate child:

cur <- cur^.left_childor

cur <- cur^.right_child

14

50

94

71 116

10866

42

Page 186: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with One Child

Use an “in/out” pointer.

Determine if it has a left or a right child.

Point the current pointer to the appropriate child:

cur <- cur^.left_childor

cur <- cur^.right_child

Let’s delete 14.

14

50

94

71 116

10866

42

Page 187: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with One Child

Use an “in/out” pointer.

Determine if it has a left or a right child.

Point the current pointer to the appropriate child:

cur <- cur^.right_child

Move the pointer; now nothing points to the node.

14

50

94

71 116

10866

42

Page 188: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with One Child

Use an “in/out” pointer.

Determine if it has a left or a right child.

Point the current pointer to the appropriate child:

cur <- cur^.right_child

The resulting tree.

50

94

71 116

10866

42

Page 189: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with One Child

Use an “in/out” pointer.

Determine if it has a left or a right child.

Point the current pointer to the appropriate child:

cur <- cur^.left_childor

cur <- cur^.right_child

Let’s delete 71.

14

50

94

71 116

10866

42

Page 190: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with One Child

Use an “in/out” pointer.

Determine if it has a left or a right child.

Point the current pointer to the appropriate child:

cur <- cur^.left_child

Move the pointer; now nothing points to the node.

14

50

94

71 116

10866

42

Page 191: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with One Child

Use an “in/out” pointer.

Determine if it has a left or a right child.

Point the current pointer to the appropriate child:

cur <- cur^.left_child

The resulting tree.

14

50

94

116

108

6642

Page 192: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with Two Children

Copy a replacementvalue from a descendantnode. - Largest from left - Smallest from right

Then delete that descendant node to remove the duplicate value. - We know this will be an easier case.

14

50

94

71 116

10866

42

Page 193: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with Two Children

14

50

94

71 116

10866

42

Let’s delete 50.

Page 194: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with Two Children

14

50

94

71 116

10866

42

Look to the leftsub-tree.

Page 195: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with Two Children

14

50

94

71 116

10866

42

42Find and copy thelargest value(this will erase theold value but createsa duplicate).

Page 196: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with Two Children

14

42

94

71 116

10866

42

The resulting treeso far.

Page 197: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with Two Children

14

42

94

71 116

10866

42

Now delete theduplicate fromthe left sub-tree.

Page 198: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with Two Children

14

42

94

71 116

10866

The final resultingtree – still has searchstructure.

Page 199: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with Two Children

14

50

94

71 116

10866

42

Let’s delete 94.

Page 200: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with Two Children

14

50

94

71 116

10866

42

Look to the rightsub-tree.

Page 201: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with Two Children

14

50

94

71 116

10866

42

Find and copy thesmallest value(this will erase theold value but createsa duplicate).

108

Page 202: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

108

Delete a Node with Two Children

14

50

71 116

10866

42

The resulting treeso far.

Page 203: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

108

Delete a Node with Two Children

14

50

71 116

10866

42

Now delete theduplicate fromthe left sub-tree.

Page 204: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Delete a Node with Two Children

14

50

71 116

66

42

The final resultingtree – still has searchstructure.

108

Page 205: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Summary

• Deleting a node from a binary search tree involves two steps:– Search for the element– Then perform the deletion

• We must preserve the search structure and only delete the element which matches.

• Four cases:– Deleting a leaf node– Deleting a node with only the left child– Deleting a node with only the right child– Deleting a node with both children

Page 206: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree

Questions?

Page 207: Traversing a Binary Tree Binary Search Tree Insertion  Deleting from a Binary Search Tree