프로젝트 : "장에서 소개 한 재귀 적 접근법을 사용하여 이진 트리의 구현 만들기이 방법에서는 각 노드가 이진 트리입니다. 따라서 이진 트리에는 요소에 대한 참조가 포함되어 있습니다 루트에 저장 될뿐만 아니라 왼쪽 및 오른쪽 하위 트리에 대한 참조가 포함됩니다. 또한 부모에 대한 참조를 포함 할 수도 있습니다. "재귀 적 접근을 사용하는 이진 트리 구현
질문 :이 장에서는 이진 트리 구현 (아래 코드 참조)을 제공하며이 프로젝트에서 저의 책에서 구현이 다르게 수행되기를 원하는지 잘 모르겠습니다. 내가 볼 수있는 몇 가지 방법과 같은 몇 가지 누락 된 세부 정보를 작성하고 또한 부모 참조를 추가하는 것입니다. 이게 그것이 묻는 것이 아니지만 나는 최종 프로젝트로 알고 있습니다. `
BTNode class:
`
//*******************************************************************
// BTNode.java Java Foundations
//
// Represents a node in a binary tree with a left and right child.
// Therefore this class also represents the root of a subtree.
//*******************************************************************
package javafoundations;
public class BTNode<T>
{
protected T element;
protected BTNode<T> left, right;
//-----------------------------------------------------------------
// Creates a new tree node with the specified data.
//-----------------------------------------------------------------
public BTNode (T element)
{
this.element = element;
left = right = null;
}
//-----------------------------------------------------------------
// Returns the element stored in this node.
//-----------------------------------------------------------------
public T getElement()
{
return element;
}
//-----------------------------------------------------------------
// Sets the element stored in this node.
//-----------------------------------------------------------------
public void setElement (T element)
{
this.element = element;
}
//-----------------------------------------------------------------
// Returns the left subtree of this node.
//-----------------------------------------------------------------
public BTNode<T> getLeft()
{
return left;
}
//-----------------------------------------------------------------
// Sets the left child of this node.
//-----------------------------------------------------------------
public void setLeft (BTNode<T> left)
{
this.left = left;
}
//-----------------------------------------------------------------
// Returns the right subtree of this node.
//-----------------------------------------------------------------
public BTNode<T> getRight()
{
return right;
}
//-----------------------------------------------------------------
// Sets the right child of this node.
//-----------------------------------------------------------------
public void setRight (BTNode<T> right)
{
this.right = right;
}
//-----------------------------------------------------------------
// Returns the element in this subtree that matches the
// specified target. Returns null if the target is not found.
//-----------------------------------------------------------------
public BTNode<T> find (T target)
{
BTNode<T> result = null;
if (element.equals(target))
result = this;
else
{
if (left != null)
result = left.find(target);
if (result == null && right != null)
result = right.find(target);
}
return result;
}
//-----------------------------------------------------------------
// Returns the number of nodes in this subtree.
//-----------------------------------------------------------------
public int count()
{
int result = 1;
if (left != null)
result += left.count();
if (right != null)
result += right.count();
return result;
}
//-----------------------------------------------------------------
// Performs an inorder traversal on this subtree, updating the
// specified iterator.
//-----------------------------------------------------------------
public void inorder (ArrayIterator<T> iter)
{
if (left != null)
left.inorder (iter);
iter.add (element);
if (right != null)
right.inorder (iter);
}
//-----------------------------------------------------------------
// The following methods are left as programming projects.
//-----------------------------------------------------------------
// public void preorder(ArrayIterator<T> iter) { }
// public void postorder(ArrayIterator<T> iter) { }
}
`
왜 내 질문에 투표합니까? 정직한 질문입니다. 프로그래밍 프로젝트는 같은 장에서 코드가 주어진 책에서 물어 보듯이 이해가되지 않습니다. 아마도 단순한 것일 수 있습니다. Idk. –
나는 나무를 이해한다 (수학적 의미와 컴퓨터 과학의 의미에서 그렇다). 코드를 읽고 정확히 무엇인지 알 수 있습니다. 각 노드가 데이터에 대한 참조와 하위 노드에 대한 참조를 가지고 있으며이 경우 부모 노드에 대한 참조를 원한다는 점에서 연결된 목록과 유사하다는 것을 알고 있습니다. –