Professional Documents
Culture Documents
Node structure
A data field and two pointers, left
and right
Data
Recursive!
17
32
89
65
37
12
76
41
59
72
83
95
Binary search tree: A binary tree that has the following properties:
The left subtree of a node contains only nodes with data less than the
node's data.
The right subtree of a node contains only nodes with data greater than
the node's data.
Both the left and right subtrees are also binary search trees.
Terminology
root node
Parent of the
node that
contains 12
Right subtree
of the tree of
which the 76
node is the
root
50
Left child of
the node that
contains 25
25
12
17
76
32
89
65
37
41
59
Leaf nodes
72
83
95
Insertion operation
We want to insert a new node into the tree. Where should we put it?
63
50
25
12
17
76
32
89
65
37
41
59
72
83
95
Insertion operation
It is bigger than 50,
so go down the right
subtree
63
50
25
12
17
76
32
89
65
37
41
59
72
83
95
Insertion operation
It is smaller than 76,
so go down the left
subtree
50
25
12
17
76
63
32
89
65
37
41
59
72
83
95
Insertion operation
It is smaller than 65,
so go down the left
subtree
50
25
12
76
89
65
37
63
17
32
41
59
72
83
95
Insertion operation
It is bigger than 59,
and 59 has no right
children, so thats
where it goes.
50
25
12
17
76
32
89
65
37
41
59
72
63
83
95
Task
Think about how you would code the insert operation
50
25
12
17
76
32
89
65
37
41
59
72
63
83
95
50
76
89
65
41
59
72
63
83
95
50
25
17
32
89
65
37
12
76
41
59
72
63
83
95
50
25
12
17
76
32
89
65
37
41
59
72
83
95
Inorder:
When you get to a node:
Traverse its left subtree.
Then print the node's
data.
Then traverse its right
subtree.
Postorder:
When you get to a node:
Traverse its left subtree.
Then traverse its right
subtree.
Then print the node's
data.
Which of these would you want to use to print your binary search tree in
ascending order?
Task
Write down the order of the numbers as printed by each of the
traversals.
void preOrder(Node n){
void preOrder(Node n){
if(n == null) return;
print(n);
preOrder(n.left);
preOrder(n.right);
}
50
25
12
17
76
65
37
32
41
59
72
89
83
95
4 (3 + 8)
This is written in what is called infix notation, in which the
operators (+, - , x, /) are written between their operands. It is the
usual notation that you are familiar with from mathematics.
However, there are two other ways of writing expressions like these,
and both of them are used in computer science.
In prefix notation, also known as Polish notation, the operators
come before their operands.
In postfix notation, also known as Reverse Polish notation, the
operators come after their operands.
(All operators are assumed to be binary.)
Examples
Infix: 4 (3 + 8)
Prefix: 4 + 3 8
Postfix: 4 3 8 +
Convert the following expressions to prefix and postfix:
1.
2.
3.
4.
4+5
4/3+7
(5 + 2) (3 + 1)
4 / (3 + 6) + 5 9
Task
Create annotated notes in Word, using
drawing objects.
Show how 5 x (4 + 3) is converted to
postfix notation.
Show how the postfix expression is
evaluated using a stack.
Task answer
Parse expression 5 (4
+ 3)
Convert to postfix:
543+
Push 5
Push 4
3
4
5
Push 3 (diagram 1)
Pop 3
Pop 4
Push 3 + 4 = 7 (diagram
2)
Pop 7
Pop 5
Push 7 5 = 35(diagram
3)
35