Thought for the Day
“There is never enough time to do everything,
but there is always enough time to do the most
important thing.”
– Brian Tracy
Binary Trees
• Important subcategory of trees:
– maximum outdegree is two
– “left subtree” and “right subtree”
a
b
d
c
e
f
A Java Class for Binary Trees
• Operations?
–
–
–
–
create new node
access subtrees
access data
add new subtrees
A Java Class for Binary Trees
a
b
d
c
e
f
Class Diagram
Tree
data, lt, rt
getData, left, right,
addLeft, addRight
The Tree Class
public class Tree<T>
{ private T data;
private Tree<T> lt, // Ptr to left subtree
rt; // Ptr to right subtree
. . .
} // class Tree
Note: this really describes a single node
Constructors
public Tree (T value, Tree<T> left,
Tree<T> right)
// Constructor: creates a new node with left
// and right subtrees
{ lt = left;
rt = right;
data = value;
} // Constructor
public Tree (T value)
// Constructor: creates a new node
{ this(value, null, null);
Call first
} // Constructor
constructor
Other Methods
• Very simple
public Tree<T> left ()
// Return the left subtree of a tree
{ return lt; }
public void addLeft (Tree<T> left)
// Add a left subtree to a node
{ if (lt != null)
throw new …Exception(…);
lt = left;
} // addLeft
public T getData ()
// Access the data value in this node
{ return data; }
Using Trees
• Many applications
– decision trees
– compilers
• We will consider a simple “knowledge
base” (expert system) application
– children’s guessing game
The Guessing Game
• Program asks the user questions, attempting
to identify an animal
• The questions and answers (i.e. the
program’s “knowledge”) are stored in a tree
The Knowledge Base Tree
Does it live in water?
Does it have webbed feet?
duck
fish
Note: This is a proper
binary tree
Does it fly?
bird
Does it bark?
dog
cat
The Algorithm
• Starting at the root:
– if at a leaf node:
• give the answer
– else
• ask the question
• if answer is positive, go to left subtree
• otherwise go to right subtree
The Java Code
pos = root; // Start at root
while (pos != null)
{ if (pos.left() != null) // Question
{ System.out.println(pos.getData());
if (answer())
pos = pos.left();
else
pos = pos.right();
}
else // Leaf: must be an answer
{ System.out.println("It's a "
+ pos.getData());
break;
}
}
Initialising the Tree
Tree<String> p; // Temporary pointer
root = new Tree<String>("Does it live in water?");
root.addLeft(new Tree<String>("...webbed feet?"));
root.addRight(new Tree<String>("Does it fly?"));
p = root.right();
p.addLeft(new Tree<String>("bird"));
p.addRight(new Tree<String>("Does it bark?"));
p = p.right();
p.addLeft(new Tree<String>("dog"));
p.addRight(new Tree<String>("cat"));
// Return to left subtree of root
p = root.left();
p.addLeft(new Tree<String>("duck"));
p.addRight(new Tree<String>("fish"));
Initialising the Tree: An
Alternative
• Bottom up:
Tree<String> left, right, p;
left = new Tree<String>("dog");
right = new Tree<String>("cat");
p = new Tree<String>("Does it bark?",
left, right);
...
Uses the constructor rather than the
addLeft and addRight methods
Traversing Trees
• We often need to work through a tree
“visiting” each node
• Several different ways that we can do this:
–
–
–
–
–
in-order
pre-order
post-order
breadth-order
etc., etc.
Traversal Methods
• In-Order (LNR):
a
– d b e a f c g
• Pre-Order (NLR):
b
–a b d e c f g
c
• Post-Order (LRN):
–d e b f g c a
• Breadth-Order:
–a b c d e f g
d
e
f
g
© Copyright 2026 Paperzz