An Interface for all Trees
An Interface for all Trees
•
TreeInterface:
specifies basic operations
p
p
common to all trees.
public interface TreeInterface
{ public Object getRootData();
public int getHeight();
public int getNumberOfNodes();
public boolean isEmpty();
bli
id l
()
public void clear();
} // end TreeInterface
An Iterator Interface for traversing trees
An Iterator Interface for traversing trees
•
TreeIteratorInterface:
TreeIteratorInterface:
specifies operations
specifies operations
for tree traversal
public interface TreeIteratorInterface
p
{ public Iterator getPreorderIterator();
public Iterator getPostorderIterator();
p
g
();
public Iterator getInorderIterator();
An Interface for Binary Trees
y
public interface BinaryTreeInterface extends TreeInterface,
TreeIteratorInterface
{
/** Sets an existing binary tree to a new one-node binary tree
{
/ Sets an existing binary tree to a new one node binary tree.
* @param rootData an object that is the data in the new
* tree’s root
/*
public void setTree(Object rootData);
public void setTree(Object rootData);
/** Sets an existing binary tree to a new binary tree.
* @param rootData an object that is the data in the new
* tree’s root
* @param leftTree the left subtree of the new tree
* @param rightTree the right subtree of the new tree */
public void setTree(Object rootData,
BinaryTreeInterface
BinaryTreeInterface
leftTree,BinaryTreeInterface rightTree);
} // end BinaryTreeInterface
Dr. Abu-ArqoubExample of how to use the Binary Tree
ADT
ADT
• Using the operations of the binary tree
Using the operations of the binary tree
ADT construct the following binary tree
then display the nodes in preorder
then display the nodes in preorder
a
e
c
b
Example of how to use the Binary Tree
ADT
ADT
// represent each leaf as a one-node tree
p
BinaryTreeInterface dTree = new BinaryTree();
dTree.setTree("D");
BinaryTreeInterface fTree = new BinaryTree();
fTree.setTree("F");
f
()
BinaryTreeInterface gTree = new BinaryTree();
gTree.setTree("G");
BinaryTreeInterface hTree = new BinaryTree();
BinaryTreeInterface hTree = new BinaryTree();
hTree.setTree("H");
BinaryTreeInterface emptyTree = new BinaryTree();
BinaryTreeInterface emptyTree new BinaryTree();
Example of how to use the Binary Tree
ADT
ADT
// form larger subtrees
BinaryTreeInterface eTree = new BinaryTree();
eTree.setTree("E", fTree, gTree); // subtree rooted at E
BinaryTreeInterface bTree = new BinaryTree();
bTree.setTree("B", dTree, eTree); // subtree rooted at B
BinaryTreeInterface cTree = new BinaryTree();
cTree.setTree("C", emptyTree, hTree); // subtree rooted at C
BinaryTreeInterface aTree = new BinaryTree();
Example of how to use the Binary Tree
ADT
ADT
// display root, height, number of nodes
System.out.println("Root of tree is " + aTree.getRootData());
System.out.println("Height of tree is " + aTree.getHeight());
System.out.println("Tree has " + aTree.getNumberOfNodes() + "
nodes");
// display nodes in preorder
Iterator preorder = aTree.getPreorderIterator();
while (preorder.hasNext())
System.out.print(preorder.next() + " ");
System.out.println();
Linked implementation of a Tree
ADT
ADT
• Nodes in a Binary Tree
Nodes in a Binary Tree
Interface for class of nodes
suitable for a binary tree
public interface
BinaryNodeInterface
{
public
Object getData();
public void
setData(Object newData);
public
BinaryNodeInterface getLeftChild();
public
BinaryNodeInterface getRightChild();
bli
id
tL ftChild(Bi
N d I t f
l ftChild)
public void
setLeftChild(BinaryNodeInterface leftChild);
public void
setRightChild(BinaryNodeInterface rightChild);
public boolean
hasLeftChild();
public boolean
hasRightChild();
public boolean
hasRightChild();
public boolean
isLeaf();
} // end BinaryNodeInterface
Dr. Abu-ArqoubClass for Binary Node
package TreePackage;
class BinaryNode implements BinaryNodeInterface{
private Object data; private BinaryNode left; private BinaryNode right; private BinaryNode right;
public BinaryNode()
{
this(null); // call next constructor } // end default constructor
public BinaryNode(Object dataPortion)
{
this(dataPortion, null, null); // call next constructor this(dataPortion, null, null); // call next constructor } // end constructor
public BinaryNode(Object dataPortion, BinaryNode leftChild,BinaryNode rightChild)
{
Class for Binary Node
y
public Object getData()
{
return data;
} // end getData
public voidsetData(Object newData)
{
data = newData;
} // end setData
public BinaryNodeInterface getLeftChild()
{
return left;
} // end getLeftChild
public void setLeftChild(BinaryNodeInterface leftChild)
{
left = (BinaryNode)leftChild;
} // end setLeftChild
Class for Binary Node
Class for Binary Node
public boolean hasLeftChild()
{{
return left != null;
} // end hasLeftChild
public boolean isLeaf()
{
return (left == null) && (right == null);
} // end isLeaf
} // end isLeaf
// Implementations of getRightChild, setRightChild, and hasRightChild
are analogous to
Binary Tree class Implementation
Binary Tree class Implementation
package TreePackage; import java util *;
import java.util. ;
public class BinaryTree implements BinaryTreeInterface {
private BinaryNode root;
bli Bi T () public BinaryTree()
{
root = null;
} // end default constructor
public BinaryTree(Object rootData)
{
root = new BinaryNode(rootData); } // end constructor
public BinaryTree(Object rootData, BinaryTree leftTree, BinaryTree rightTree)
{
privateSetTree(rootData, leftTree, rightTree); } // end constructor
Binary Tree class Implementation
Binary Tree class Implementation
public void setTree(Object rootData)
{
root = new BinaryNode(rootData);
} // end setTree
public void setTree(Object rootData, BinaryTreeInterface
leftTree, BinaryTreeInterface rightTree)
{
privateSetTree(rootData, (BinaryTree)leftTree,
(BinaryTree)rightTree);
Binary Tree class Implementation
Binary Tree class Implementation
/*
private void privateSetTree(Object rootData,
BinaryTree leftTree,BinaryTree rightTree)
{{
// < FIRST DRAFT - See Segments 25.5 - 25.8 for
improvements. >
root = new BinaryNode(rootData);
root new BinaryNode(rootData);
if (leftTree != null)
root.setLeftChild(leftTree.root);
if (rightTree != null)
root.setRightChild(rightTree.root);
} // end privateSetTree
*/
Dr. Abu-ArqoubBinary Tree class Implementation
Binary Tree class Implementation
public Object getRootData()
{{
Object rootData = null;
if (root != null)
rootData = root getData();
rootData = root.getData();
return rootData;
} // end getRootData
public boolean isEmpty()
{
return root == null;
} // end isEmpty
} // end isEmpty
public void clear()
Binary Tree class Implementation
Binary Tree class Implementation
protected void setRootData(Object rootData)
{{
root.setData(rootData); } // end setRootData
protected void setRootNode(BinaryNode rootNode)
{{
root = rootNode; } // end setRootNode
protected BinaryNode getRootNode()
p y g ()
{
return root;
} // end getRootNode
public int getHeight() public int getHeight()
{ return root.getHeight();// in node class
} // end getHeight
public int getNumberOfNodes()
{ return root.getNumberOfNodes(); } // end getNumberOfNodes
Binary Tree class Implementation
Binary Tree class Implementation
public int getHeight()
//
in node class
{{
return getHeight(this); //
call private getHeight
} // end getHeight
private int getHeight(BinaryNode node)
{
int height = 0;
if (node != null)
height = 1 + Math.max(getHeight(node.left),
getHeight(node.right));
Binary Tree class Implementation
Binary Tree class Implementation
public int getNumberOfNodes()
{
int leftNumber = 0;
int rightNumber = 0;
if (left != null)
leftNumber = left.getNumberOfNodes();
g
()
if (right != null)
rightNumber = right.getNumberOfNodes();
g
g
g
();
return 1 + leftNumber + rightNumber;
} // end getNumberOfNodes
} // end getNumberOfNodes
Binary Tree class Implementation
Binary Tree class Implementation
public void inorderTraverse()
{{
inorderTraverse(root);
} // end inorderTraverse
private void inorderTraverse(BinaryNode node)
{
if (node != null)
if (node ! null)
{
inorderTraverse((BinaryNode)node.getLeftChild());
System.out.println(node.getData());
y
p
(
g
());
inorderTraverse((BinaryNode)node.getRightChild());
} // end if
Interface for Binary Search Tree
y
import java.util.Iterator;
public interface SearchTreeInterface extends TreeInterface {
/** Task: Searches for a specific entry in the tree / Task: Searches for a specific entry in the tree.
* @param entry an object to be found
* @return true if the object was found in the tree */
public boolean contains(Comparable entry);
/** Task: Retrieves a specific entry in the tree. * @param entry an object to be found
* @return either the object that was found in the tree or * null if no such object exists */
public Comparable getEntry(Comparable entry); public Comparable getEntry(Comparable entry);
/** Task: Adds a new entry to the tree. If the entry matches * an object that exists in the tree already, replaces
* the object with the new entry.
* @ E t bj t t dd t th t * @param newEntry an object to add to the tree
* @return either null if newEntry was not in the tree already, * or an existing entry that matched the parameter
* newEntry and has been replaced in the tree */
public Comparable add(Comparable newEntry);
p p ( p y);
Interface for Binary Search Tree
Interface for Binary Search Tree
/** Task: Removes a specific entry from the tree.
* @param entry an object to be removed
* @return either the object that was removed from the tree or
* null if no such object exists */
public Comparable remove(Comparable entry);
/** Task: Creates an iterator that traverses all entries in the
* tree.
* @return an iterator that provides sequential access to the
* entries in the tree */
public Iterator getInorderIterator();
Implementation code for some operations of
BST
BST
public Comparable getEntry(Comparable entry)
{
return findEntry(getRootNode(), entry); } // end getEntry
} // end getEntry
private Comparable findEntry(BinaryNode rootNode, Comparable entry)
{
Comparable result = null; if (rootNode != null)
{
Object rootEntry = rootNode.getData(); if (entry.equals(rootEntry))
result = (Comparable)rootEntry; else if (entry.compareTo(rootEntry) < 0)
result = findEntry((BinaryNode)rootNode.getLeftChild(), entry); l
else
result = findEntry((BinaryNode)rootNode.getRightChild(), entry); } // end if
return result;; } // end findEntry
Implementation code for some operations of
BST
BST
public boolean contains(Comparable entry)
{
return getEntry(entry) != null;
{
return getEntry(entry) != null;
} // end contains
public Comparable add(Comparable newEntry)
{{
Comparable result = null;
if (isEmpty())
setRootNode(new BinaryNode(newEntry)); // method is protected
setRootNode(new BinaryNode(newEntry)); // method is protected
// in BinaryTree
else // look for newEntry in tree
{
BinaryNode currentNode = getRootNode(); // currentNode != null
BinaryNode currentNode = getRootNode(); // currentNode != null
BinaryNode parentNode = null;
Implementation code for some operations of
BST
BST
while (!found && (currentNode != null) )
{
Object currentEntry = currentNode.getData();
int comparison = newEntry.compareTo(currentEntry);
if (comparison < 0)
{ // search left
direction = 'L';
parentNode = currentNode;
currentNode = (BinaryNode)currentNode.getLeftChild();
}
else if (comparison > 0)
{ // search right
direction = 'R';
parentNode = currentNode;
currentNode = (BinaryNode)currentNode.getRightChild();
}
Implementation code for some operations of
BST
BST
else
{ // newEntry matches currentEntry: return and replace
// currentEntry
result = (Comparable) currentEntry;
currentNode.setData(newEntry);
found = true;
} // end if
} // end while
if (!found)
{ // add new entry as a leaf child of parentNode
if (direction == 'L')
parentNode.setLeftChild(new BinaryNode(newEntry));
else
parentNode.setRightChild(new BinaryNode(newEntry));
} // end if