二叉搜索树-Java

二叉搜索树-Java,java,binary-search-tree,Java,Binary Search Tree,嘿,伙计们,我对深度(查找二叉树的深度)和打印树(按顺序打印树)有问题。这是我的密码 import java.util.LinkedList; import java.util.Queue; public class BSTDictionary<T1, T2>{ private static class Node<T>{ public Node<T> left; public Node<T> right; public

嘿,伙计们,我对深度(查找二叉树的深度)和打印树(按顺序打印树)有问题。这是我的密码

import java.util.LinkedList;
import java.util.Queue;

public class BSTDictionary<T1, T2>{

private static class Node<T>{
    public Node<T> left;
    public Node<T> right;
    public T data;
    public Node(T data)
    {
        this.data = data;
    }
    public Node<T> getLeft()
    {
        return this.left;
    }
    public void setLeft(Node<T> left)
    {
        this.left = left;
    }
    public Node<T> getRight()
    {
        return this.right;
    }
    public void setRight(Node<T> right)
    {
        this.right = right;
    }
}

public int depth(Node root){

    if(root == null) {
        return 0;
    }
    else return 1 + Math.max((root.getLeft(), root.getRight());
}

public void printTree(){

    Node<?> n;
    Queue<Node<?>> nodequeue = new LinkedList<Node<?>>();
    while (!nodequeue.isEmpty())
    {
        Node<?> next = nodequeue.remove();
        System.out.print(next.data + " ");
        if (next.getLeft() != null)
        {
            nodequeue.add(next.getLeft());
        }
        if (next.getRight() != null)
        {
            nodequeue.add(next.getRight());
        }
    }}}
import java.util.LinkedList;
导入java.util.Queue;
公共类词典{
私有静态类节点{
公共节点左;
公共节点权;
公共数据;
公共节点(T数据)
{
这个数据=数据;
}
公共节点getLeft()
{
把这个还给我;
}
公共void setLeft(节点左侧)
{
this.left=左;
}
公共节点getRight()
{
归还这个。对;
}
公共无效设置权限(节点权限)
{
这个。右=右;
}
}
公共整数深度(节点根){
if(root==null){
返回0;
}
否则返回1+Math.max((root.getLeft(),root.getRight());
}
公共void printree(){
节点n;
队列>();
而(!nodequeue.isEmpty())
{
Node next=nodequeue.remove();
System.out.print(next.data+“”);
if(next.getLeft()!=null)
{
nodequeue.add(next.getLeft());
}
if(next.getRight()!=null)
{
nodequeue.add(next.getRight());
}
}}}
这是我为测试这些方法而提供的测试类

    public class DictionaryAdvancedTest {
protected static String[] entries = new String[26 * 26];

protected static void fill() {
    // Insert 26 * 26 entries
    for (int i = 0; i < 26; i++)
        for (int j = 0; j < 26; j++) {
            StringBuffer s = new StringBuffer();
            s.append((char) ((int) 'A' + i));
            s.append((char) ((int) 'A' + j));
            entries[i * 26 + j] = s.toString();
        }
} // fill method

public static void main(String[] args) {
    BSTDictionary<String, SortableString> dict1 = new BSTDictionary<String, SortableString>();
    AVLDictionary<String, SortableString> dict2 = new AVLDictionary<String, SortableString>();

    // Insert lots of entries
    fill();
    for (int i = 0; i < 26 * 26; i++) {
        int e;
        do {
            e = (int) (Math.random() * (26 * 26));
        } while (entries[e] == null);

        dict1.insert(new SortableString(entries[e]), entries[e]);
        dict2.insert(new SortableString(entries[e]), entries[e]);
        entries[e] = null;
    }

    // print the two dictionaries
    dict1.printTree();
    dict2.printTree();
    // print the depth
    System.out.println("The initial BST tree has a maximum depth of "
            + dict1.depth());
    System.out.println("The initial AVL tree has a maximum depth of "
            + dict2.depth());

    // Delete half the entries
    fill();
    for (int i = 0; i < 13 * 26; i++) {
        int e;
        do {
            e = (int) (Math.random() * (26 * 26));
        } while (entries[e] == null);

        dict1.delete(new SortableString(entries[e]));
        dict2.delete(new SortableString(entries[e]));
    }

    System.out
            .println("After deletes, the BST tree has a maximum depth of "
                    + dict1.depth());
    System.out
            .println("After deletes, the AVL tree has a maximum depth of "
                    + dict2.depth());

    // Add a quarter the entries
    fill();
    for (int i = 0; i < 6 * 26; i++) {
        int e;
        do {
            e = (int) (Math.random() * (26 * 26));
        } while (entries[e] == null);

        dict1.insert(new SortableString(entries[e]), entries[e]);
        dict2.insert(new SortableString(entries[e]), entries[e]);
    }

    System.out
            .println("After insertions, the BST tree has a maximum depth of "
                    + dict1.depth());
    System.out
            .println("After insertions, the AVL tree has a maximum depth of "
                    + dict2.depth());

    // Search for a few random entries
    fill();
    for (int i = 0; i < 6; i++) {
        int e;
        do {
            e = (int) (Math.random() * (26 * 26));
        } while (entries[e] == null);

        System.out.print("Searching for " + entries[e] + ": ");
        if (dict1.search(new SortableString(entries[e])) == null) {
            System.out.print("Not found in Dict1, ");
        } else {
            System.out.print("Found in Dict1, ");
        }
        if (dict2.search(new SortableString(entries[e])) == null) {
            System.out.println("not found in Dict2.");
        } else {
            System.out.println("found in Dict2.");
        }
    }
}} 
公共类字典高级测试{
受保护的静态字符串[]项=新字符串[26*26];
受保护的静态空隙填充(){
//插入26*26个条目
对于(int i=0;i<26;i++)
对于(int j=0;j<26;j++){
StringBuffer s=新的StringBuffer();
s、 附加((char)((int)'A'+i));
s、 附加((char)((int)'A'+j));
条目[i*26+j]=s.toString();
}
}//填充方法
公共静态void main(字符串[]args){
bstdirectionary dict1=新的bstdirectionary();
AVLDictionary dict2=新的AVLDictionary();
//插入大量条目
填充();
对于(int i=0;i<26*26;i++){
INTE;
做{
e=(int)(Math.random()*(26*26));
}while(条目[e]==null);
插入(新的排序字符串(条目[e]),条目[e]);
插入(新的SortableString(条目[e]),条目[e]);
条目[e]=null;
}
//打印这两本词典
dict1.printree();
dict2.printree();
//打印深度

System.out.println(“初始BST树的最大深度为” +1.深度(); System.out.println(“初始AVL树的最大深度为” +深度(); //删除一半的条目 填充(); 对于(int i=0;i<13*26;i++){ INTE; 做{ e=(int)(Math.random()*(26*26)); }while(条目[e]==null); 删除(新的SortableString(条目[e]); 删除(新的SortableString(条目[e]); } 系统输出 .println(“删除后,BST树的最大深度为” +1.深度(); 系统输出 .println(“删除后,AVL树的最大深度为” +深度(); //在条目中添加四分之一 填充(); 对于(int i=0;i<6*26;i++){ INTE; 做{ e=(int)(Math.random()*(26*26)); }while(条目[e]==null); 插入(新的排序字符串(条目[e]),条目[e]); 插入(新的SortableString(条目[e]),条目[e]); } 系统输出 .println(“插入后,BST树的最大深度为” +1.深度(); 系统输出 .println(“插入后,AVL树的最大深度为” +深度(); //搜索一些随机条目 填充(); 对于(int i=0;i<6;i++){ INTE; 做{ e=(int)(Math.random()*(26*26)); }while(条目[e]==null); 系统输出打印(“搜索“+条目[e]+”:”; if(dict1.search(新的SortableString(条目[e]))==null){ 系统输出打印(“未在Dict1中找到”); }否则{ 系统输出打印(“在Dict1中找到”); } if(dict2.search(新的SortableString(条目[e]))==null){ System.out.println(“未在Dict2中找到”); }否则{ System.out.println(“在Dict2中找到”); } } }}
谢谢大家:)

将最后一行替换为
else返回1+Math.max(depth(root.getLeft()),depth(root.getRight())

其余的信息来自搜索功能,您的问题中并没有包含这些信息

将最后一行替换为
else返回1+Math.max(depth(root.getLeft()),depth(root.getRight())

其余的信息来自搜索功能,您的问题中并没有包含这些信息

将最后一行替换为
else返回1+Math.max(depth(root.getLeft()),depth(root.getRight())

其余的信息来自搜索功能,您的问题中并没有包含这些信息

将最后一行替换为
else返回1+Math.max(depth(root.getLeft()),depth(root.getRight())


其余消息来自搜索函数,您的问题中未包含该函数。

返回1+Math.max((root.getLeft(),root.getRight())时出现编译错误

有一个额外的(root.getLeft)返回的节点不是int类型

我不确定如何获得树深度..只获取root.getLeft,因为它只会将节点返回到左侧。根据我的理解,我们需要获取节点数,然后向其中添加1

我也在努力和你一起学习英国夏令时。如果我发现了其他的东西,我会让你知道。如果我的理解有问题,你也请让我知道


快乐学习!

返回1+Math.max((root.getLeft(),root.getRight())时出现编译错误

还有一个额外的(和root.ge)
public int depth(Node root){

if(root == null) {
    return 0;
}
else return 1 + Math.max((root.getLeft(), root.getRight());
}