Java 添加和生成二叉搜索树
我在弄清楚如何向二进制搜索树中添加或插入节点时遇到了一些麻烦。目前,我有以下代码:Java 添加和生成二叉搜索树,java,Java,我在弄清楚如何向二进制搜索树中添加或插入节点时遇到了一些麻烦。目前,我有以下代码: public void add(int v) { Node n = new Node(v); if(root==null) root = n; else { Node m = root; while(...) { //not sure what to check
public void add(int v) {
Node n = new Node(v);
if(root==null)
root = n;
else {
Node m = root;
while(...) { //not sure what to check
if(v < m.value)
m = m.left;
else
m = m.right;
}
if(...) //not sure what to check
m.left = n;
else
m.right = n;
}
}
使用当前方法,您需要一个额外的变量来跟踪父节点
public void add(int v) {
Node n = new Node(v);
if(root==null)
root = n;
else {
Node m = root,k;
while(m!=null) {
if(v < m.value){
k=m;
m = m.left;
}
else if(v > m.value){
k=m;
m = m.right;
}else{
return; //v already exists
}
}
if(v < k.value)
k.left=n;
else
k.right=n;
}
}
publicsvoidadd(intv){
节点n=新节点(v);
if(root==null)
根=n;
否则{
节点m=根,k;
while(m!=null){
如果(vm.value){
k=m;
m=m,对;
}否则{
return;//v已经存在
}
}
如果(v
对于关于范围的第二个问题,正如DrYap所指出的,在一个范围内生成数字,并在插入到二叉树时调用add(),直到找到一个没有相应子节点的节点。然后在该位置插入新节点
至于从一个范围填充数字,您可以使用与数组相同的方法生成它们,但您可以
add
方法来代替插入数组。当向BST添加值时,您可以横穿树,直到找到一个空点,然后在该点插入一个新节点
首先我们从退化的情况开始。即,没有节点且根为null/空
伪代码:
if root is null then
root = new node(value);
return;
end if
所以,我们在这里所做的就是构建树的根节点。它不包含叶节点
现在,所有后续插入都要求我们横切树
所以首先我们需要一个起点,它将是根节点。然后,我们还需要知道我们是否已经到达了一个可以将新值插入到树中的空白点。这需要跟踪两个变量;一个用来保存我们正在检查的当前节点,另一个用来保存该节点的父节点
伪代码:
# initialize tracking variables.
checkNode = root;
parentNode = checkNode;
while checkNode is null do
parent = checkNode; # want to save this for when we find a spot to store our new value
if valueOf(checkNode) equals value then return # the value is already stored
if value < valueOf(checkNode) then
# the new value is less than the value stored in this node so continue down the left branch
checkNode = checkNode.left
else
checkNode = checkNode.right # otherwise continue down the right branch
end while
# at this point checkNode will be null and parent will be set to a node that can store a new node with our value.
if value < valueOf(parent) then
parent.left = node(value) # store a new node in the left of the found parent node
else
parent.right = node(value)
#初始化跟踪变量。
checkNode=root;
parentNode=checkNode;
当checkNode为null时,执行以下操作:
父节点=检查节点;#想把它保存起来,以备我们找到存储新值的位置时使用吗
如果valueOf(checkNode)等于value,则返回#该值已存储
如果值
我们所做的是将要添加的值与我们正在检查的节点的值进行比较,然后沿着树向下横切。我们还将跟踪正在检查的节点的父节点,因为这是我们最终插入新节点的地方。这是因为当checkNode为null时,我们将结束搜索
# initialize tracking variables.
checkNode = root;
parentNode = checkNode;
while checkNode is null do
parent = checkNode; # want to save this for when we find a spot to store our new value
if valueOf(checkNode) equals value then return # the value is already stored
if value < valueOf(checkNode) then
# the new value is less than the value stored in this node so continue down the left branch
checkNode = checkNode.left
else
checkNode = checkNode.right # otherwise continue down the right branch
end while
# at this point checkNode will be null and parent will be set to a node that can store a new node with our value.
if value < valueOf(parent) then
parent.left = node(value) # store a new node in the left of the found parent node
else
parent.right = node(value)