【发布时间】:2018-10-09 15:49:50
【问题描述】:
我们得到了一个需要编写代码的任务:
- 二叉搜索树
- 树必须完整,而不是完美(这意味着所有不在最低级别或次低级别的节点都应该有 2 个孩子,而最低层的节点应尽可能靠左)
- 我们需要以级别顺序插入到树中 1234563
级别订单插入将是:
4, 2, 6, 1, 3, 5, 7, 0只取 Array 的中间并将其作为 root 是行不通的。如果您有一个包含 1 到 9 个元素的数组,那么您将有 4 作为根(Java 中的 int 值,double 为 4.5),右侧将有 5 个元素,左侧有 4 个元素。这不是一棵完整的树。甚至不是一棵完美的树。
我的代码只能在左侧或右侧插入,具体取决于它是否大于 og 小于根,没有级别顺序插入。 Anytype x 参数是要插入的值,而BinaryNode t 是我们在树中的当前节点(这就是我们需要在左侧或右侧插入新值时比较的方式)
private BinaryNode<AnyType> insert( AnyType x, BinaryNode<AnyType> 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<AnyType extends Comparable<? super AnyType>>
{
/**
* 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<AnyType> insert( AnyType x, BinaryNode<AnyType> 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<AnyType> 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<AnyType> 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<AnyType>
{
// Constructors
BinaryNode( AnyType theElement )
{
this( theElement, null, null );
}
BinaryNode( AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt )
{
element = theElement;
left = lt;
right = rt;
}
AnyType element; // The data in the node
BinaryNode<AnyType> left; // Left child
BinaryNode<AnyType> right; // Right child
}
/** The tree root. */
private BinaryNode<AnyType> root;
// Test program
public static void main( String [ ] args )
{
BinarySearchTree<Integer> t = new BinarySearchTree<>( );
t.insert(2);
t.insert(1);
t.insert(3);
t.printTree();
}
}
【问题讨论】:
-
您错过了项目的重点。没有水平订单插入之类的东西。诀窍是您应该能够以任何顺序插入元素,但树会在插入元素时对树进行排序和平衡。
-
不,我的讲师告诉我需要按级别顺序插入它们。还有一种叫做级别顺序插入的东西,看看这个二叉树(不是二叉搜索树),它使用级别顺序插入来制作一棵完整的树:geeksforgeeks.org/construct-complete-binary-tree-given-array
-
这似乎是一个二进制堆问题。如果父节点的索引为 n 则子节点位于 2*n+1 (左节点)和 2*n+2 (右节点),您也可以将其反转并找出父节点。因此,您可以遍历您的列表并一次性构建树。
-
不适用于二叉搜索树。左节点永远不能是 2*n+1,这会使它大于其父节点,因此不在左侧,它应该在右侧。如果我们说的是排序数组。在未排序的数组上,它只会更不可预测。
-
不确定您的意思...您正在调用 insert on (4, 2, 6, 1, 3, 5, 7, 0) 对吗?所以从根 4 开始,索引为 0 。左边是 0*2+1= 索引 1,右边是 0*2+2=2 索引 2。你继续前进。
标签: java recursion binary-tree binary-search-tree