Java 使用队列列表查找二叉树中的高度
我的getHeight()方法有问题,它没有返回树的正确高度。我真的认为这个问题与我的QueueList类有关,但我无法解决这个问题。我愿意接受关于如何更好地实现二进制搜索的建议。下面是我的QueueList类Java 使用队列列表查找二叉树中的高度,java,Java,我的getHeight()方法有问题,它没有返回树的正确高度。我真的认为这个问题与我的QueueList类有关,但我无法解决这个问题。我愿意接受关于如何更好地实现二进制搜索的建议。下面是我的QueueList类 package lab5; import java.util.NoSuchElementException; public class QueueList<E> implements Queue<E> { private Node<E> front
package lab5;
import java.util.NoSuchElementException;
public class QueueList<E> implements Queue<E> {
private Node<E> front;
private Node<E> rear;
private int size = 0;
public QueueList (E it){
front = rear = new Node<E>(it);
size++;
}
public QueueList(){
front = rear = new Node<E>();
size=0;
}
public boolean isEmpty() {
return size==0;
}
public void enqueue(E it) {
rear.setNext(rear);
rear.setElement(it);
size++;
}
public void clear() {
front=new Node<E>();
rear=new Node<E>();
front.setNext(null);
rear.setNext(null);
front.setNext(null);
size=0;
}
public int size() {
return size;
}
public E front() {
return front.getElement();
}
public E dequeue() {
Node<E> temp = front;
if(isEmpty()){
throw new NoSuchElementException();
}
else{
front = front.getNext();
size--;
return temp.getElement();
}
}
}
lab5包装;
导入java.util.NoSuchElementException;
公共类QueueList实现队列{
专用节点前端;
专用节点后方;
私有整数大小=0;
公共队列列表(E it){
前=后=新节点(it);
大小++;
}
公共队列列表(){
前=后=新节点();
尺寸=0;
}
公共布尔值为空(){
返回大小==0;
}
公共无效排队(E it){
后。设置下一步(后);
后置元件(it);
大小++;
}
公共空间清除(){
front=新节点();
后=新节点();
front.setNext(null);
设置下一步(空);
front.setNext(null);
尺寸=0;
}
公共整数大小(){
返回大小;
}
公共电子前线({
返回front.getElement();
}
公共E出列(){
节点温度=前;
if(isEmpty()){
抛出新的NoTouchElementException();
}
否则{
front=front.getNext();
大小--;
返回temp.getElement();
}
}
}
这是我的二叉搜索树类,我的所有BSTNode方法都正常工作,所以假设这不是问题所在
package lab5;
public class BinarySearchTree<E extends Comparable<E>> {
private BSTNode root;
private int size;
public BinarySearchTree() {
root = null;
size = 0;
}
public BinarySearchTree(BSTNode node) {
root = node;
size = 1;
}
/**
* searches for a node that contains it.
*
* if it finds it, it returns that node
*
* else it returns null
*
* @param it
* - the element to look for
*
* @return the node that contains it
*
*/
public BSTNode search(E it) {
BSTNode<E> parent = null;
BSTNode<E> child = null;
BSTNode<E> node = root;
while (node != null && node.getElement() != it) {
parent = node;
int compareResult = it.compareTo(node.getElement());
if (compareResult < 0) {
node = node.getLeft();
} else {
node = node.getRight();
}
}
if (node == null) {
return null;
}
return node;
}
/**
* determines if the tree contains the element
*
* @return true if it is in the tree
*
*/
public boolean contains(E it) {
return (search(it) != null);
}
/**
* Add the element to the correct location
*
* all elements to the left are less than the parent
*
* all elements to the rights are greater than the parent
*
* Do not allow duplicates
*
* @param it
* the element to insert
*
*/
public void insert(E it) {
BSTNode<E> newNode = new BSTNode<E>(it);
if (root == null) {
root = newNode;
return;
}
BSTNode<E> parent = null;
BSTNode<E> node = root;
while (node != null) {
parent = node;
int compareResult = it.compareTo(node.getElement());
if (compareResult < 0) {
node = node.getLeft();
} else if (compareResult > 0) {
node = node.getRight();
} else {
// duplicate
return;
}
}
int res = it.compareTo(parent.getElement());
if (res < 0) {
parent.setLeft(newNode);
} else {
parent.setRight(newNode);
}
size++;
}
/**
* Removes the node that contains it.
*
* If the tree does not contain it, it prints that to
*
* the user and does nothing else.
*
* Otherwise it removes the node and maintains the
*
* BST properties
*
* if removing a node with two children, replace it
*
* with its in order predecessor.
*
* @param the
* element of the node you want to remove.
*
*/
public void remove(E it) {
BSTNode<E> parent = null;
BSTNode<E> child = null;
BSTNode<E> node = root;
// Find the node that contains it
while (node != null && node.getElement() != it) {
parent = node;
int compareResult = it.compareTo(node.getElement());
if (compareResult < 0) {
node = node.getLeft();
} else {
node = node.getRight();
}
}
if (node == null) {
System.out.println("failed to find: " + it + " for removal");
return;
}
if (node.isLeaf()) {
if (parent == null) {
root = null;
} else if (it.compareTo(parent.getElement()) < 0) {
parent.setLeft(null);
} else {
parent.setRight(null);
}
} else if (node.getLeft() == null) {
child = node.getRight();
swapElements(node, child);
node.setLeft(child.getLeft());
node.setRight(child.getRight());
} else if (node.getRight() == null) {
child = node.getLeft();
} else {
child = node.getLeft();
parent = null;
while (child.getRight() != null) {
parent = child;
child = parent.getRight();
}
if (parent == null) {
swapElements(node, child);
node.setLeft(child.getLeft());
} else {
swapElements(node, child);
parent.setRight(child.getLeft());
}
}
size--;
}
/**
* Returns the height of the tree
*
* if tree is empty, height is -1
*
* if tree only has one node, height is 0
*
* @return the integer height of the tree
*
*
*
*/
public int getHeight() {
int height = -1;
QueueList<BSTNode> q = new QueueList<BSTNode>();
if (root == null) {
return height;
}
q.enqueue(root);
while (!q.isEmpty()) {
int nodeCount = q.size();
height++;
while (nodeCount > 0) {
BSTNode<E> node = q.dequeue();
if (node.hasLeft()) {
q.enqueue(node.getLeft());
}
if (node.hasRight()) {
q.enqueue(node.getRight());
}
nodeCount--;
}
}
return height;
}
/**
* Helper method
*
* For removal you need to swap elements of nodes
*
* @param node1
* , node2 the nodes whose contents you are swapping
*
*/
private void swapElements(BSTNode node1, BSTNode node2) {
BSTNode temp = null;
temp.setElement(node1.getElement());
node1.setElement(node2.getElement());
node2.setElement(temp.getElement());
}
/**
* prints each level of the tree on its own line
*
* use your Queue class
*
*/
public void printLevelOrder() {
QueueList<BSTNode> q = new QueueList<BSTNode>();
q.enqueue(root);//You don't need to write the root here, it will be written in the loop
while (q.size() > 0)
{
BSTNode n = q.dequeue();
System.out.println(n.toString()); //Only write the value when you dequeue it
if (n.hasLeft())
{
q.enqueue(n.getLeft());//enqueue the left child
}
if (n.hasRight())
{
q.enqueue(n.getRight());//enque the right child
}
}
}
/**
* prints the tree in a depth-first fashion
*
* use your Stack class
*
*/
public void printByDepth() {
StackList<BSTNode> s = new StackList<BSTNode>();
s.push(root);
while (s.isEmpty() == false) {
BSTNode x = s.pop();
if (x.getRight() != null)
s.push(x.getRight());
if (x.getLeft() != null)
s.push(x.getRight());
System.out.print(" " + x.toString());
}
}
/**
* prints the tree in an inorder fashion.
*
* uses a stack to push left children onto the stack
*
*/
public void printInOrder() {
if (root == null)
return;
StackList s = new StackList();
BSTNode currentNode = root;
while (!s.isEmpty() || currentNode != null) {
if (currentNode != null) {
s.push(currentNode);
currentNode = currentNode.getLeft();
} else {
BSTNode n = null;
n.setElement(s.pop());
System.out.printf("%d ", n.toString());
currentNode = n.getRight();
}
}
}
}
lab5包装;
公共类二进制搜索树{
私有节点根;
私有整数大小;
公共二进制搜索树(){
root=null;
尺寸=0;
}
公共二进制搜索树(BSTNode){
根=节点;
尺寸=1;
}
/**
*搜索包含它的节点。
*
*如果找到它,它将返回该节点
*
*否则返回null
*
*@param it
*-要查找的元素
*
*@返回包含它的节点
*
*/
公共节点搜索(E it){
bstnodeparent=null;
bstnodechild=null;
BSTNode=root;
while(node!=null&&node.getElement()!=it){
父节点=节点;
int compareResult=it.compareTo(node.getElement());
如果(比较结果<0){
node=node.getLeft();
}否则{
node=node.getRight();
}
}
if(node==null){
返回null;
}
返回节点;
}
/**
*确定树是否包含元素
*
*@如果它在树中,则返回true
*
*/
公共布尔包含(E it){
返回(搜索(it)!=null);
}
/**
*将元素添加到正确的位置
*
*左侧的所有元素都小于父元素
*
*权限的所有元素都大于父元素
*
*不允许重复
*
*@param it
*要插入的元素
*
*/
公共空白插入(E it){
BSTNode newNode=新BSTNode(it);
if(root==null){
根=新节点;
返回;
}
bstnodeparent=null;
BSTNode=root;
while(节点!=null){
父节点=节点;
int compareResult=it.compareTo(node.getElement());
如果(比较结果<0){
node=node.getLeft();
}否则如果(比较结果>0){
node=node.getRight();
}否则{
//复制品
返回;
}
}
int res=it.compareTo(parent.getElement());
如果(res<0){
setLeft(newNode);
}否则{
parent.setRight(newNode);
}
大小++;
}
/**
*删除包含它的节点。
*
*如果树不包含它,它会将其打印到
*
*用户不做任何其他事情。
*
*否则,它将删除节点并维护
*
*BST特性
*
*如果删除包含两个子节点的节点,请将其替换
*
*与它的有序的前身。
*
*@param the
*要删除的节点的元素。
*
*/
删除公共空间(E it){
bstnodeparent=null;
bstnodechild=null;
BSTNode=root;
//查找包含它的节点
while(node!=null&&node.getElement()!=it){
父节点=节点;
int compareResult=it.compareTo(node.getElement());
如果(比较结果<0){
node=node.getLeft();
}否则{
node=node.getRight();
}
}
if(node==null){
System.out.println(“未能找到:“+it+”用于删除”);
返回;
}
if(node.isLeaf()){
如果(父项==null){
root=null;
}else if(it.compareTo(parent.getElement())<0){
parent.setLeft(null);
}否则{
parent.setRight(null);
}
}else if(node.getLeft()==null){
child=node.getRight();
Swaplements(节点、子节点);
node.setLeft(child.getLeft());
node.setRight(child.getRight());
}else if(node.getRight()==null){
child=node.getLeft();
}否则{
child=node.getLeft();
parent=null;
while(child.getRight()!=null){
父母=子女;
child=parent.getRight();
}
如果(父项==null){
Swaplements(节点、子节点);
node.setLeft(child.getLeft());
}否则{
Swaplements(节点、子节点);
parent.setRight(child.getLeft());
}
}
大小--;
}
/**
*返回树的高度
*
*如果树为空,则高度为-1
*
*若树只有一个节点,则高度为0
*
*@返回树的整数高度
*
*
*
*/
公共整数getHeight(){
整数高度=-1;
QueueList q=新的QueueList();
if(root==null){
返回高度;
}
q、 排队(根);
而(!q.isEmpty()){
int nodeCount=q.size();
高度++;
而(节点计数>0){
BSTNode node=q.dequeue();
if(node.hasleet()){
q、 排队(node.getLeft());
}
if(node.hasRight()){
q、 排队(node.getRight());
}
节点计数--;
public int getHeight() {
return getHeight(root, 0);
}
private int getHeight(BSTNode node, int currentHeight) {
if (node == null) {
return currentHeight;
}
int rightHeight = getHeight(node.getRight(), currentHeight + 1)
int leftHeight = getHeight(node.getLeft(), currentHeight + 1);
return Math.max(rightHeight, leftHeight);
}