Chapter 24 - Binary trees. In this chapter, the learning objectives are: Learn about binary trees; learn how to represent and handle a binary tree using the TreeNode class; learn about binary search trees; review sets and maps, and the java.util classes that implement them.
Trang 1and Data Structures
Maria Litvin ● Gary Litvin
2nd AP edition with GridWorld
Trang 2Objectives:
classes that implement them
Trang 3Some Applications of Trees
Trang 4Number of levels (equals
5 here)
node
node’s
right subtree
Trang 5Binary Tree Properties
example, a binary tree with 20 levels can
nodes.
where to proceed, left or right (used in binary search trees).
compared to the total number of nodes.
Trang 6The TreeNode Class
instead of next has left and right
public class TreeNode
{
Holds a reference to
the left childHolds a reference to the right child
Trang 7The TreeNode Class (cont’d)
// Constructors:
public TreeNode (Object v)
{ value = v; left = null; right = null; }
public TreeNode (Object v, TreeNode lt, TreeNode rt)
{ value = v; left = lt; right = rt; }
// Methods:
}
Trang 8Trees and Recursion
the left and right subtrees are smaller trees:
{ // Base case: root == null, // the tree is empty do nothing
if (root != null) // Recursive case {
process (root.getValue ( ));
traverse (root.getLeft ( ));
traverse (root.getRight ( ));
} }
Trang 10TreeNode Example 2
// returns a reference to a new tree, which is a
// copy of the tree rooted at root
Trang 11traverse the left and right subtrees.
A/ \
B C / \
D E
A B D E C
Trang 12process the root, then traverse the right
subtree.
A/ \
B C / \
D E
D B E A C
Trang 13subtrees, then process the root.
A/ \
B C / \
D E
D E B C A
Trang 14Traversals (cont’d)
Trang 15Binary Search Trees (BST)
comparator is supplied).
subtree are smaller than the value in the node and all the values in its right subtree are
larger than the value in the node.
15 / \
8 20 / \
1 12
15 / \
12 20
/ \
1 8
Trang 16BST (cont’d)
for quick searching and linked lists for
inserting and deleting values.
Trang 17// root refers to a BST; the nodes hold Strings
Trang 18{
TreeNode node = root;
while ( node != null )
Trang 22Find the smallest
node of the right
Link that note in
place of the root and
remove the old root
Trang 23BST (cont’d)
remove , and contains methods take O(log n)
time, where n is the number of nodes in the
tree.
degenerate into a nearly linear shape
tree balanced.
Trang 24java.util.TreeSet<E>
• compareTo (or comparator’s compare ) is
methods.
implements inorder traversal.
in ascending order.
Trang 25java.util.TreeSet<E> (cont’d)
Never mind
Trang 26java.util.TreeMap<K,V>
• compareTo (or comparator’s compare ) for
remove methods.
• The keySet method returns a TreeSet<K>
Trang 27java.util.TreeMap<K,V> (cont’d)
Trang 28Review:
for data retrieval applications?
Trang 29Review (cont’d):
in a binary tree with 10 levels?
a node in a doubly-linked list?
and postorder traversals of the following tree?
A / \
B C / \
D E
Trang 30Review (cont’d):
property What is it?
2 3 / \
4 5
4 / \
2 5 / \
1 3