【发布时间】:2021-09-01 10:16:12
【问题描述】:
我有这段代码,但我发现很难以递归方式编写它(在二叉搜索树中插入方法)
public void insert(E element){
BTNode<E> node=root;
boolean stop=false;
while(!stop){
if(element.compareTo(node.getElement())<0){
if(node.getLeft()==null){
node.setLeft(new BTNode<E>(element));
stop=true;
} else{
node=node.getLeft();
}
}
else{
if(node.getRight()==null){
node.setRight(new BTNode<E>(element));
stop=true;
} else{
node=node.getRight();
}
}
}
}
【问题讨论】:
标签: java algorithm data-structures binary-search-tree computer-science