-4

我很难理解这一点。我的作业如下(我输入我的作业是为了让你充分了解我的目标,而不是让你为我做作业):

完成本章介绍的链接二叉树类的实现。具体完成getRight、contains、isEmpty、toString、preorder、post order等操作的实现。

我写了所有我知道如何写的方法(尽管它们可能都错了),但是 ------>我如何写预购方法?<-----这是我目前的代码(预购方法部分在底部):(我在谷歌上研究过很多次,在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

4

1 回答 1

1

预购是这样的:

  1. 访问当前节点
  2. 如果存在左节点,则访问左节点的前序。
  3. 如果存在右节点,则访问右节点的前序。

我相信通过这几个步骤,您将能够实施您的预购方法。

这个的伪代码:

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)

由于这是家庭作业,因此实施取决于您。

于 2013-04-27T02:54:44.200 回答