基于ArrayList的二叉树 - Java的 [英] ArrayList Based Binary Tree - Java

查看:428
本文介绍了基于ArrayList的二叉树 - Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在用Java 实现基于的ArrayList 二叉树的过程。我试图找出如何做到这一点做,但我遇到了一堵墙。有一个一堆方法我应该执行,但每次我尝试一些时间,这不是'吨似乎工作。

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.

我们有位置对象位置&LT标识; E> 。在这种我们有一个数组列表私人根变量,无论访问仅此,所以尺寸() 的isEmpty()的方法很简单。然而,我遇到了,当涉及到实施方法,如一些麻烦: hasLeft(位置< E>) hasRight(位置< E&GT ;) 左(位置< E>),右(位置< E>) addRoot(E E)等..左,右方法简单地返回左子节点的右子。我所熟悉的的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.

我将如何去实现这些方法?我坚持,我会AP preciate任何帮助,我可以得到的。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆