基于 ArrayList 的二叉树 - Java [英] ArrayList Based Binary Tree - Java
问题描述
我目前正在实现基于 ArrayList
的 Java 中的二叉树
.我想弄清楚这将如何完成,但我正在撞墙.class
中有一堆 methods
我应该实现,但每次我尝试一些东西时,它似乎都不起作用.
I am currently in the process of implementing an ArrayList
based binary tree in Java
. I am trying to figure out how this would be done, but I am running into a wall. There are a bunch of methods
in a class
I am supposed to implement, but each time I try something, it doesn't seem to work.
我们有由 Position
标识的 Position 对象
.在这个class
中,我们有一个private
的array list
和一个根变量
,两者都可访问
只通过这个 class
,所以 size()
method
和 isEmpty()
方法很简单.但是,我在实现以下方法时遇到了一些麻烦,例如:hasLeft(Position
, hasRight(Position
left(Position<E>), right(Position<E>),
addRoot(E e)
等等... Left 和 Right 方法只返回 左孩子
和 节点的右孩子
.我熟悉 ArrayList
,但在用它们实现 二叉树类
时不熟悉.
We have Position objects
that are identified by Position<E>
. In this class
we have an array list
that is private
, and a root variable
, both accessible
only by this class
, so the size()
method
, and the isEmpty()
methods are simple. However, I am running into some trouble when it comes to implementing the methods such as: hasLeft(Position<E>)
, hasRight(Position<E>)
left(Position<E>), right(Position<E>),
addRoot(E e)
, etc... The Left and Right methods simply return the left child
and right child of a node
. I am familiar with ArrayList
, but not when it comes to implementing a binary tree class
with them.
我将如何实施这些方法?我被卡住了,如果我能得到任何帮助,我将不胜感激.
How would I go about implementing these methods? I am stuck, and I would appreciate any help I can get.
谢谢!
推荐答案
当您将二叉树编写为数组时,您正在构建通常称为堆的东西.堆的文档相当齐全,本文将为您提供有关它们如何实现的大量详细信息:
When you write binary trees as an Array you are building what's typically called a heap. Heaps are fairly well documented and this article will give you lots of detail about how they are implemented:
http://en.wikipedia.org/wiki/Binary_heap
这篇关于基于 ArrayList 的二叉树 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!