Beruflich Dokumente
Kultur Dokumente
A & AB
Object-Oriented Programming
and Data Structures
Maria Litvin Gary Litvin
C
A
Binary Trees
Copyright 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved.
Objectives:
Learn about binary trees
Learn how to represent and handle a binary
tree using the TreeNode class
23-2
23-3
Right
child
node
Leaves
(nodes with
no children)
Number of
levels (equals
5 here)
nodes
right
subtree
23-4
23-5
Holds a reference to
the left child
Holds a reference to
the right child
23-6
23-7
23-8
TreeNode Example 1
public int countNodes (TreeNode root)
{
Base case
if (root == null)
return 0;
else
return 1 + countNodes (root . getLeft ()) +
countNodes (root . getRight ());
}
(for the root)
23-9
TreeNode Example 2
// returns a reference to a new tree, which is a
// copy of the tree rooted at root
public TreeNode copy (TreeNode root)
{
if (root == null)
return null;
else
return new TreeNode (root . getValue (),
copy (root . getLeft ()),
copy (root . getRight ()));
}
23-10
Traversals
Preorder: first process the root, then
traverse the left and right subtrees.
private void traversePreorder (TreeNode root)
{
if (root != null)
{
process (root.getValue());
traversePreorder (root.getLeft());
traversePreorder (root.getRight());
}
}
A
/ \
B C
/ \
D E
ABDEC
23-11
Traversals (contd)
Inorder: first traverse the left subtree, then
process the root, then traverse the right
subtree.
private void traverseInorder (TreeNode root)
{
if (root != null)
{
traverseInorder (root.getLeft());
process (root.getValue());
traverseInorder (root.getRight());
}
}
A
/ \
B C
/ \
D E
D B EAC
23-12
Traversals (contd)
Postorder: first traverse the left and right
subtrees, then process the root.
private void traversePostorder (TreeNode root)
{
if (root != null)
{
traversePostorder (root.getLeft());
traversePostorder (root.getRight());
process (root.getValue());
}
}
A
/ \
B C
/ \
D E
DEBCA
23-13
Traversals (contd)
Preorder: root
left
right
1
2
Inorder: left
root
right
2
1
Postorder: left
right
root
3
1
23-14
not a BST
15
/ \
8 20
/ \
1 12
15
/ \
12 20
/ \
1 8
23-15
BST (contd)
BSTs combine the benefits of sorted arrays
for quick searching and linked lists for
inserting and deleting values.
23-16
BST (contd)
Recursive contains
23-17
BST (contd)
Iterative contains
23-18
A BST Class
public class MyTreeSet
{
private TreeNode root;
...
private boolean contains (Object target)
{
return contains (root, target);
}
Private recursive
helper method
23-19
BST (contd)
27
The smallest
node
17
37
19
33
23
31
The largest
node
51
34
40
23-20
Adding a Node
Private recursive
helper method
23-21
Step 2:
Unlink that node and
promote its right
child into its place
50
50
30
25
60
75
40
60
20
90
30
25
70
40
20
69
75
90
70
69
72
72
50
Step 3:
Link that note in
place of the root and
remove the old root
60
30
25
20
75
40
90
70
69
72
23-22
BST (contd)
When the tree is balanced (bushy) the add,
remove, and contains methods take O(log n)
time, where n is the number of nodes in the
tree.
23-23
java.util.TreeSet<E>
Is implemented as a balanced BST.
compareTo (or comparators compare) is
used by the add, contains, and remove
methods.
23-24
java.util.TreeSet<E> (contd)
Never mind...
23-25
java.util.TreeMap<K,V>
Is implemented as a BST for keys.
compareTo (or comparators compare) for
keys is used by the put, get, containsKey, and
remove methods.
23-26
java.util.TreeMap<K,V> (contd)
23-27
Review:
23-28
Review (contd):
What is the largest possible number of nodes
in a binary tree with 10 levels?
23-29
Review (contd):
What is a Binary Search Tree?
Inorder traversal of a BST has a neat
property. What is it?
1
/ \
2 3
/ \
4 5
4
/ \
2 5
/ \
1 3
23-30