Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree - PowerPoint PPT Presentation

Loading...

PPT – Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree PowerPoint presentation | free to download - id: 41354b-MDYzO



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

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

Description:

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 ... – PowerPoint PPT presentation

Number of Views:766
Avg rating:3.0/5.0
Slides: 208
Provided by: ccGatech9
Learn more at: http://www.cc.gatech.edu
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree


1
Traversing a Binary TreeBinary Search Tree
Insertion Deleting from a Binary Search Tree
Lecture 12
2
Traversing a Binary TreeInorder Traversal
3
The Scenario
  • Imagine we have a binary tree
  • We want to traverse the tree
  • Its 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

4
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
42
2
5
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
42
2
At 13 do left
6
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
42
2
At 9 do left
At 13 do left
7
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
42
2
At 2 do left
At 9 do left
At 13 do left
8
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
42
2
At NIL
At 2 do left
At 9 do left
At 13 do left
9
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
42
2
At 2 do current
At 9 do left
At 13 do left
10
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
42
2
At 2 do right
At 9 do left
At 13 do left
11
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
42
2
At NIL
At 2 do right
At 9 do left
At 13 do left
12
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
42
2
At 2 - done
At 9 do left
At 13 do left
13
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
42
2
At 9 do current
At 13 do left
14
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
42
2
At 9 do right
At 13 do left
15
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
42
2
At NIL
At 9 do right
At 13 do left
16
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
42
2
At 9 done
At 13 do left
17
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
42
2
At 13 do current
18
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
42
2
At 13 do right
19
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
42
2
At 42 do left
At 13 do right
20
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
42
2
At NIL
At 42 do left
At 13 do right
21
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
42
2
At 42 do current
At 13 do right
22
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
42
2
At 42 do right
At 13 do right
23
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
42
2
At NIL
At 42 do right
At 13 do right
24
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
42
2
At 42 done
At 13 do right
25
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
42
2
At 13 done
26
Outline of In-Order Traversal
  • Three principle steps
  • Traverse Left
  • Do work (Current)
  • Traverse Right
  • Work can be anything
  • Separate work from traversal

27
  • Traverse the tree In order
  • Visit the trees left sub-tree
  • Visit the current and do work
  • Visit right sub-tree

28
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 ltgt NIL ) then
  • In_Order( cur.left_child )
  • Do_Something( cur.data )
  • In_Order( cur.right_child )
  • endif
  • endprocedure // In_Order

29
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
P
R
30
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
P
R
31
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
P
R
32
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
33
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
34
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
35
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
36
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
L
37
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
L
38
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
L
L
39
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
L
L
40
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1
L
L
P
41
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1
L
L
P
42
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1
L
L
P
R
43
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1
L
L
P
R
44
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1
L
L
P
R
45
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3
L
P
L
P
R
46
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3
L
P
L
P
R
47
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3
L
P
R
L
P
R
48
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3
L
P
R
L
P
R
49
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3
L
P
R
L
L
P
R
50
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3
L
P
R
L
L
P
R
51
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7
L
P
R
L
L
P
P
R
52
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7
L
P
R
L
L
P
P
R
53
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7
L
P
R
L
L
P
P
R
R
54
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7
L
P
R
L
L
P
P
R
R
55
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7
L
P
R
L
L
P
P
R
R
56
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7
L
P
R
L
L
P
P
R
R
57
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9
P
L
P
R
L
L
P
P
R
R
58
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9
P
L
P
R
L
L
P
P
R
R
59
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9
P
R
L
P
R
L
L
P
P
R
R
60
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9
P
R
L
P
R
L
L
P
P
R
R
61
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9
P
R
L
L
P
R
L
L
P
P
R
R
62
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9
P
R
L
L
P
R
L
L
P
P
R
R
63
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9 14
P
R
L
L
P
P
R
L
L
P
P
R
R
64
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9 14
P
R
L
L
P
P
R
L
L
P
P
R
R
65
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9 14
P
R
L
L
P
P
R
R
L
L
P
P
R
R
66
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9 14
P
R
L
L
P
P
R
R
L
L
P
P
R
R
67
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9 14
P
R
L
L
P
P
R
R
L
L
P
P
R
R
68
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9 14
P
R
L
L
P
P
R
R
L
L
P
P
R
R
69
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
L
Output 1 3 7 9 14 22
P
R
L
L
P
P
R
R
L
L
P
P
R
R
70
Continue?
Yes!
Enough Already!
71
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
L
Output 1 3 7 9 14 22
P
R
L
L
P
P
R
R
L
L
P
P
R
R
72
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
Output 1 3 7 9 14 22
P
R
L
L
P
P
R
R
L
L
P
P
R
R
73
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
Output 1 3 7 9 14 22
P
R
L
L
P
P
R
R
L
L
P
P
R
R
74
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22
P
R
L
L
P
P
R
R
L
L
P
P
R
R
75
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22
P
R
L
L
P
P
R
R
L
L
P
P
R
R
76
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22
P
R
L
L
L
P
P
R
R
L
L
P
P
R
R
77
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22
P
R
L
L
L
P
P
R
R
L
L
P
P
R
R
78
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36
P
R
L
L
L
P
P
P
R
R
L
L
P
P
R
R
79
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36
P
R
L
L
L
P
P
P
R
R
L
L
P
P
R
R
80
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36
P
R
L
L
L
P
P
P
R
R
R
L
L
P
P
R
R
81
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36
P
R
L
L
L
P
P
P
R
R
R
L
L
P
P
R
R
82
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
R
R
83
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
R
R
84
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
85
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
86
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
87
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
88
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
89
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
90
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67
P
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
91
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67
P
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
92
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67
P
P
R
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
93
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67
P
P
R
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
94
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67
P
P
R
R
L
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
95
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67
P
P
R
R
L
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
96
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
97
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
98
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
P
P
P
R
R
R
99
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
P
P
P
R
R
R
100
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
R
R
R
101
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
R
R
R
102
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
103
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
104
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
105
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
106
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
107
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
108
Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
109
Algorithm Example . . . InOrderPrint(root) . . .
root
L
P
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
110
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.

111
Questions?
112
Binary Search Tree Insertion
113
Tree Node Defined
  • In general
  • Node definesa record
  • data isoftype lttypegt
  • left, right isoftype ptr toa Node
  • endrecord
  • In this example
  • Node definesa record
  • data isoftype num
  • left, right isoftype ptr toa Node
  • endrecord

114
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

115
Finding the Correct Location
Start withthis tree
12
41
3
98
7
116
Finding the Correct Location
Where would 4 be added?
12
41
3
98
7
117
Finding the Correct Location
4
To the topdoesnt work
12
41
3
98
7
118
Finding the Correct Location
12
41
4
98
3
In the middledoesnt work
7
119
Finding the Correct Location
12
41
3
98
7
At the bottomDOES work!
4
120
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.
  • Well recurse to get to the bottom (i.e. when
    current nil)

121
Finding the Correct Location
  • if (current nil)
  • DO ADD NODE WORK HERE
  • elseif (current.data gt value_to_add) then
  • // recurse left
  • Insert(current.left, value_to_add)
  • else
  • // recurse right
  • Insert(current.right, value_to_add)
  • endif

122
Adding the Node
  • Current is an in/out pointer
  • We need information IN to evaluate current
  • We need to send information OUT because were
    changing the tree (adding a node)
  • Once weve 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)

123
Adding the Node
  • current lt- new(Node)
  • current.data lt- value_to_add
  • current.left lt- nil
  • current.right lt- nil

124
The Entire Module
  • procedure Insert(cur iot in/out Ptr toa Node,
  • data_in iot in num)
  • if(cur NIL) then
  • cur lt- new(Node)
  • cur.data lt- data_in
  • cur.left lt- NIL
  • cur.right lt- NIL
  • elseif(cur.data gt data_in)
  • Insert(cur.left, data_in)
  • else
  • Insert(cur.right, data_in)
  • endif
  • endprocedure // Insert

125
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.

126
Head iot Ptr toa Node head lt- NIL Insert(head, 42)
127
Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
128
Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
129
Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
130
Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
131
Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
132
Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
133
Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
134
Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
135
Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
136
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
42
137
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
data_in 23
138
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
data_in 23
139
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
data_in 23
140
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
data_in 23
141
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
data_in 23
142
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 23
143
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 23
144
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 23
145
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 23
146
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 23
147
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
42
23
148
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
149
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
150
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
151
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
152
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
153
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
154
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
155
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
156
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 35
157
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 35
158
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 35
159
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 35
160
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 35
161
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 35
162
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
42
23
35
163
Continue?
yes...
Ive had enough!
164
. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 47
165
. . Insert(head, 23) Insert(head,
35) Insert(head, 4
About PowerShow.com