我们得到了一个需要编写代码的作业:
二叉搜索树
树必须是完整的,而不是完美的(这意味着所有不在最低级别或第二低级别的节点都应有2个子节点,而最低级别的节点应尽可能远)
我们需要按级别顺序插入树中
因此,如果我有一个包含元素{0, 1, 2, 3, 4, 5, 6, 7}的数组,root应该是4,2, 1, 3, 0在左侧和6, 5, 7右侧。
级别订单插入为: 4, 2, 6, 1, 3, 5, 7, 0
仅将Array的中间部分放在根目录是行不通的。如果有1到9个元素的数组,则将有4个作为根(java中的int值,double为4.5),右侧将有5个元素,左侧有4个元素。这不是一棵完整的树。甚至不是完美的树。
我的代码只能向左或向右插入,具体取决于它是否
大于或小于根,而不插入级别顺序。该Anytype x
参数是要插入的价值,同时BinaryNode t为当前
节点,我们在树(这就是我们的比较,如果我们需要新的插入
值向左或向右)
private BinaryNode insert( AnyType x, BinaryNode t )
{
if( t == null )
return new BinaryNode<>( x, null, null );
int compareResult = x.compareTo( t.element );
if( compareResult < 0 )
t.left = insert( x, t.left );
else if( compareResult > 0 )
t.right = insert( x, t.right );
else
; // Duplicate; do nothing
return t;
}
如何按级别顺序插入并仍然维护二进制搜索树?
我应该使用某种形式的递归吗?
我的整个程序
import java.nio.BufferUnderflowException;
import java.util.*;
import static java.lang.Math.pow;
/**
* Implements an unbalanced binary search tree.
* Note that all "matching" is based on the compareTo method.
* @author Mark Allen Weiss
*/
public class BinarySearchTree>
{
/**
* Construct the tree.
*/
public BinarySearchTree( )
{
root = null;
}
/**
* Insert into the tree; duplicates are ignored.
* @param x the item to insert.
*/
public void insert( AnyType x )
{
root = insert( x, root );
}
/**
* Test if the tree is logically empty.
* @return true if empty, false otherwise.
*/
public boolean isEmpty( )
{
return root == null;
}
/**
* Print the tree contents in sorted order.
*/
public void printTree( )
{
if( isEmpty( ) )
System.out.println( "Empty tree" );
else
printTree( root );
}
/**
* Internal method to insert into a subtree.
* @param x the item to insert.
* @param t the node that roots the subtree.
* @return the new root of the subtree.
*/
private BinaryNode insert( AnyType x, BinaryNode t )
{
if( t == null )
return new BinaryNode<>( x, null, null );
int compareResult = x.compareTo( t.element );
if( compareResult < 0 )
t.left = insert( x, t.left );
else if( compareResult > 0 )
t.right = insert( x, t.right );
else
; // Duplicate; do nothing
return t;
}
/**
* Internal method to print a subtree in sorted order.
* @param t the node that roots the subtree.
*/
private void printTree( BinaryNode t )
{
if( t != null )
{
printTree( t.left );
System.out.println( t.element );
printTree( t.right );
}
}
/**
* Internal method to compute height of a subtree.
* @param t the node that roots the subtree.
*/
private int height( BinaryNode t )
{
if( t == null )
return -1;
else
return 1 + Math.max( height( t.left ), height( t.right ) );
}
// Basic node stored in unbalanced binary search trees
private static class BinaryNode
{
// Constructors
BinaryNode( AnyType theElement )
{
this( theElement, null, null );
}
BinaryNode( AnyType theElement, BinaryNode lt, BinaryNode rt )
{
element = theElement;
left = lt;
right = rt;
}
AnyType element; // The data in the node
BinaryNode left; // Left child
BinaryNode right; // Right child
}
/** The tree root. */
private BinaryNode root;
// Test program
public static void main( String [ ] args )
{
BinarySearchTree t = new BinarySearchTree<>( );
t.insert(2);
t.insert(1);
t.insert(3);
t.printTree();
}
}