将二进制的高度添加到插入方法
我正在创建一个程序,将字符(数字/字母)插入二进制树中。到目前为止,我能够产生输出,但这不是我所期望的。这些是我遇到的问题:
插入
方法无法打印树的正确高度
。我不确定在哪里应该插入height ++;
语句以获取正确的输出。insert
方法只能在右侧添加节点。
预期输出:
ht = 3 [k = 3 l = [k = 1 r = [k = 2]] r = [k = 5 l = [k = 4]]
< /p>
我的输出:
ht = 4 [k = 3 r = [k = 1 r = [k = 2 r = [k = 5 r = [k = 4]]]
(全部节点仅添加到正确的“ r”)
这是我的课程:
主要类
BST<Character> bst = new BST<>();
bst.insert('3');
bst.insert('1');
bst.insert('2');
bst.insert('5');
bst.insert('4');
System.out.println("ht=" + bst.height + " " + bst.toString());
bst class - 其中插入
声明
public class BST<T> extends BT<T> {
// insert() method
public void insert(char k)
{
if (root == null) {
root = new BTNode(k);
return;
}
BTNode<T> n = root;
BTNode<T> p = null; // parent
while (n != null) {
p = n;
if (k < n.value) {
n = n.left;
} else {
n = n.right;
}
}
if (k < p.value) {
p.left = new BTNode(k);
} else {
p.right = new BTNode(k);
height++; // adds 1 to height when a new level is made
}
}
}
btnode class
public class BTNode<T> {
T info;
int value, level;
BTNode<T> left, right;
public BTNode(T el) {
this(el, null, null);
}
public BTNode(T el, BTNode<T> l, BTNode<T> r) {
info = el;
left = l;
right = r;
}
}
bt class - toString
宣布
public class BT<T> {
BTNode<T> root = null;
int height = 0;
public BT() {
BTNode<T> node = new BTNode("");
}
// other methods
// toString()
public String toString() {
return toString(root);
}
public String toString(BTNode<T> n) {
String s = "";
if (n == null) {
return "";
}
if (n != null) {
s = "[K=" + n.info;
if (n.left != null) {
s = s + " L=" + toString(n.left) + "]";
}
if (n.right != null) {
s = s + " R=" + toString(n.right) + "]";
}
}
return s;
}
}
希望您可以帮助我,谢谢!
I am creating a program that inserts a character (number/letter) into a binary tree. So far, I'm able to produce an output but it's not what I expected. These are the problems I'm encountering:
The
insert
method is not able to print the correctheight
of the tree. I am not sure where I should insert myheight++;
statement to get the correct output.The
insert
method is only able to add nodes to the right.
Expected Output:
ht=3 [K=3 L=[K=1 R=[K=2]] R=[K=5 L=[K=4]]]
My Output:
ht=4 [K=3 R=[K=1 R=[K=2 R=[K=5 R=[K=4]]]]
(all nodes are only added to the right 'R')
Here are my classes for reference:
Main Class
BST<Character> bst = new BST<>();
bst.insert('3');
bst.insert('1');
bst.insert('2');
bst.insert('5');
bst.insert('4');
System.out.println("ht=" + bst.height + " " + bst.toString());
BST Class - where the insert
method is declared
public class BST<T> extends BT<T> {
// insert() method
public void insert(char k)
{
if (root == null) {
root = new BTNode(k);
return;
}
BTNode<T> n = root;
BTNode<T> p = null; // parent
while (n != null) {
p = n;
if (k < n.value) {
n = n.left;
} else {
n = n.right;
}
}
if (k < p.value) {
p.left = new BTNode(k);
} else {
p.right = new BTNode(k);
height++; // adds 1 to height when a new level is made
}
}
}
BTNode Class
public class BTNode<T> {
T info;
int value, level;
BTNode<T> left, right;
public BTNode(T el) {
this(el, null, null);
}
public BTNode(T el, BTNode<T> l, BTNode<T> r) {
info = el;
left = l;
right = r;
}
}
BT Class - where the toString
method is declared
public class BT<T> {
BTNode<T> root = null;
int height = 0;
public BT() {
BTNode<T> node = new BTNode("");
}
// other methods
// toString()
public String toString() {
return toString(root);
}
public String toString(BTNode<T> n) {
String s = "";
if (n == null) {
return "";
}
if (n != null) {
s = "[K=" + n.info;
if (n.left != null) {
s = s + " L=" + toString(n.left) + "]";
}
if (n.right != null) {
s = s + " R=" + toString(n.right) + "]";
}
}
return s;
}
}
Hope you can help me out, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中有很多问题。我将列出一些即时项目,但您确实需要学习使用交互式调试器和单元测试来解决您所看到的各种问题。
您在比较中参考
value
字段 btnode 中的字段,但永远不会设置它。您应该真正参考info
(这是节点中的实际数据)。但是给定的
信息
是一种通用类型,您无法使用标准比较操作员。相反,您需要将其定义为&lt; t扩展可比&lt; t&gt;&gt;
,然后使用n.info.compareto(k)&gt; 0
。传递到插入的密钥也应为类型
t
,这意味着其他类还需要确保
t
扩展可比性
。。 P>当节点被添加到右侧时,高度才会增加。
仅当节点与根最大值插入更远时,才需要提高高度。如下:
您应该习惯使您的字段私密并通过
Getters
访问它们。在您的情况下,AcompareValue
方法可能很有意义。You have quite a few issues in your code. I'll list a few immediate items but you really will need to learn to use an interactive debugger and unit testing to resolve the sorts of issues you are seeing.
You refer to the
value
field inBTNode
in your comparison but it is never set. You should really be referring toinfo
(which is the actual data in the node).But given
info
is a generic type you can't use standard comparison operators. Instead you'll need to define it as<T extends Comparable<T>>
and then usen.info.compareTo(k) > 0
.The key passed into insert should also be of type
T
Which means the other classes also need to ensure
T
extendsComparable
.Height is only incremented when nodes are added to the right which makes no sense.
Height needs to be increased only when a node is inserted further from the root than the current maximum. Something like the following:
You should get used to making your fields private and accessing them through
getters
. In your case acompareValue
method would likely make sense.