递归排序二叉树

时间:2018-09-16 18:45:28

标签: java recursion binary-tree

我需要有关此代码的帮助。它似乎陷入了无限循环,但没有显示stackoverflow错误或nullpointer错误。 bt是二叉树,而db是数据库。我得到了通过递归搜索数组(db)来创建不平衡树的任务。

public void createSortedBT(){
    if(bt.isEmpty()){
        bt.setContent(db.getCountry(db.getArray().length-1));
        createSortedBT2(db.getArray().length-1, bt);
    }
}

private void createSortedBT2(int index, BinaryTree<Country> cNode){
    while(index >= 0){           
        if(db.getCountry(index).isLess(cNode.getContent())){
            if(cNode.getLeftTree().isEmpty()){
                cNode.getLeftTree().setContent(db.getCountry(index));
            }else{
                createSortedBT2(index, cNode.getLeftTree());
            }
        }

        if(db.getCountry(index).isGreater(cNode.getContent())){
            if(cNode.getRightTree().isEmpty()){
                cNode.getRightTree().setContent(db.getCountry(index));
            }else{
                createSortedBT2(index, cNode.getRightTree());
            }
        }
        index--;
        cNode = bt;
    }
}

0 个答案:

没有答案
相关问题