我很难理解这一点。我的家庭作业如下(我输入我的家庭作业,让你完全理解我的目标,而不是让我为我做作业):
完成本章介绍的Linked Binary Tree类的实现。 具体来说,完成getRight,contains,isEmpty,toString的实现, 预购和后期订单操作。
我写了所有我知道怎么写的方法(虽然它们可能都错了),但是------&gt; 如何编写预订方法?&lt; - -----这是我到目前为止的代码(预订方法部分位于底部): (我在 Google 上多次研究,很多次在 Stackoverflow 上进行了研究<仍然尚未找到答案我确实直接去找我的教授,但我也想得到第二个意见。他还没有回复。这里的主要问题是找到像我一样使用代码的人,即迭代器类和节点类等)
//*******************************************************************
// LinkedBinaryTree.java Java Foundations
//
// Implements a binary tree using a linked representation.
//*******************************************************************
package javafoundations;
import java.util.Iterator;
import javafoundations.*;
import javafoundations.exceptions.*;
public class LinkedBinaryTree<T> implements BinaryTree<T>
{
protected BTNode<T> root;
//-----------------------------------------------------------------
// Creates an empty binary tree.
//-----------------------------------------------------------------
public LinkedBinaryTree()
{
root = null;
}
//-----------------------------------------------------------------
// Creates a binary tree with the specified element as its root.
//-----------------------------------------------------------------
public LinkedBinaryTree (T element)
{
root = new BTNode<T>(element);
}
//-----------------------------------------------------------------
// Creates a binary tree with the two specified subtrees.
//-----------------------------------------------------------------
public LinkedBinaryTree (T element, LinkedBinaryTree<T> left,
LinkedBinaryTree<T> right)
{
root = new BTNode<T>(element);
root.setLeft(left.root);
root.setRight(right.root);
}
//-----------------------------------------------------------------
// Returns the element stored in the root of the tree. Throws an
// EmptyCollectionException if the tree is empty.
//-----------------------------------------------------------------
public T getRootElement()
{
if (root == null)
throw new EmptyCollectionException ("Get root operation "
+ "failed. The tree is empty.");
return root.getElement();
}
//-----------------------------------------------------------------
// Returns the left subtree of the root of this tree.
//-----------------------------------------------------------------
public LinkedBinaryTree<T> getLeft()
{
if (root == null)
throw new EmptyCollectionException ("Get left operation "
+ "failed. The tree is empty.");
LinkedBinaryTree<T> result = new LinkedBinaryTree<T>();
result.root = root.getLeft();
return result;
}
//-----------------------------------------------------------------
// Returns the element in this binary tree that matches the
// specified target. Throws a ElementNotFoundException if the
// target is not found.
//-----------------------------------------------------------------
public T find (T target)
{
BTNode<T> node = null;
if (root != null)
node = root.find(target);
if (node == null)
throw new ElementNotFoundException("Find operation failed. "
+ "No such element in tree.");
return node.getElement();
}
//-----------------------------------------------------------------
// Returns the number of elements in this binary tree.
//-----------------------------------------------------------------
public int size()
{
int result = 0;
if (root != null)
result = root.count();
return result;
}
//-----------------------------------------------------------------
// Populates and returns an iterator containing the elements in
// this binary tree using an inorder traversal.
//-----------------------------------------------------------------
public Iterator<T> inorder()
{
ArrayIterator<T> iter = new ArrayIterator<T>();
if (root != null)
root.inorder (iter);
return iter;
}
//-----------------------------------------------------------------
// Populates and returns an iterator containing the elements in
// this binary tree using a levelorder traversal.
//-----------------------------------------------------------------
public Iterator<T> levelorder()
{
LinkedQueue<BTNode<T>> queue = new LinkedQueue<BTNode<T>>();
ArrayIterator<T> iter = new ArrayIterator<T>();
if (root != null)
{
queue.enqueue(root);
while (!queue.isEmpty())
{
BTNode<T> current = queue.dequeue();
iter.add (current.getElement());
if (current.getLeft() != null)
queue.enqueue(current.getLeft());
if (current.getRight() != null)
queue.enqueue(current.getRight());
}
}
return iter;
}
//-----------------------------------------------------------------
// Satisfies the Iterable interface using an inorder traversal.
//-----------------------------------------------------------------
public Iterator<T> iterator()
{
return inorder();
}
//-----------------------------------------------------------------
// The following methods are left as programming projects.
//-----------------------------------------------------------------
public LinkedBinaryTree<T> getRight()
{
if(root == null)
{
throw new EmptyCollectionException ("Get right operation "
+ "failed. The tree is empty.");
}
LinkedBinaryTree<T> result = new LinkedBinaryTree<T>();
result.root = root.getRight();
return result;
}
public Boolean Contains(T item)
{
BTNode<T> result;
result = root;
if(root == null)
{
throw new EmptyCollectionException ("\'Contains\' operation "
+ "failed. The tree is empty.");
}
if(root == item)
{
return true;
}
while(result.getElement() != item)
{
result = result.getRight();
}
while(result.getElement() != item)
{
result = result.getLeft();
}
if(root == null && result.getElement() != item)
{
return false;
}
return true;
}
public boolean isEmpty()
{
if(root == null)
{
return true;
}
else
{
return false;
}
}
public String toString()
{
return ("There are " + root.count() + " items in this tree.");
}
public Iterator<T> preorder()
{
}
// public Iterator<T> postorder() { }
}
如果你需要看到它们,这里是其他类的链接: LinkedQueue BTNode
答案 0 :(得分:1)
预购顺序如下:
我确信通过这几个步骤,您将能够实施预购方法。
这个伪代码:
function preOrder(Node node)
//1. visit current node
show(node->data)
//2. if exists left node, visit pre order of left node
Node left = node->left
if (left is not null)
preOrder(left)
//3. if exist right node, visit pre order of right node.
Node right = node->left
if (right is not null)
preOrder(right)
由于这是作业,因此实施取决于您。