gpt4 book ai didi

Java二叉搜索树插入方法不起作用

转载 作者:行者123 更新时间:2023-12-02 11:50:11 25 4
gpt4 key购买 nike

我的 BST 插入方法有问题。它只是将我的姓名列表插入到左侧,而不是向右递归并适当平衡。这就是我的输出的样子。此姓名列表有 125 个姓名,因此我将其剪短以适合此窗口。在输出下面,我放入了 BST java 程序的 insert 方法的 java 代码,以及 main 方法的一部分。我希望有人能看到我的问题,因为我和我的导师找不到问题。

预购遍历:沃尔特:空 |-劳伦斯:空 |-Ken:空 |-詹妮弗:null |-大卫:空 |-沃尔特:null |-菲尔:空 |-斯科蒂:空 |-托德:空 |-伦纳德:空 |-卡拉:空 |-米歇尔:空 |-吉尔:空 |-史蒂文:空 |-永利:null |-劳埃德:空

public void insertLeft(E key, T value) {
Node<E, T> node = new Node<E, T>(key, value);
if (root == null) {
root = node;
} else {
Node current = root;
while (current.left != null) {
current = current.left;
}
current.left = node;
}
}

//User friendly THE ONLY THING THAT DOES NOT WORK!!!!!!!
public boolean insert(E key, T value) {
return insert(root, key, value);
}

public boolean insert(Node position, E key, T value) {
if (position == null) {
root = new Node<E, T>(key, value);
return true;
}
int comparison = position.key.compareTo(key);
if (comparison > 0) {
if (position.left == null) {;
position.left = new Node<E, T>(key, value);
return true;
}
return insert(position.left, key, value);
} else if (comparison < 0) {
if (position.right == null) {
position.right = new Node<E, T>(key, value);
return true;
}
return insert(position.right, key, value);
} else {
return false;
}
}

public static void main(String[] args) { Main main = new Main();

    BinaryTree bst = new BinaryTree();
//ADD DATA Oshiro stuff
String namesList = "Walt Lawrence Ken Jennifer David Walter Phil Scotty "
+ "Todd Leonard Kara Michelle Jill Steven Wynn Lloyd Brandon Gary"
+ " Jim Dale Joyce Don Tom Christine Rachel Jeff Raymond Kelli"
+ " Charles Kevin Brant Joseph Michael Kelly Jessie Suzie Sally"
+ " Christian Terry John Art Francis Riki Evelyn Tony Ikaika Joe"
+ " Ann Neil Daniel Willie James Jeremy Aislynn Larry Celeste"
+ " Paige Dennis Fred Rosa Ryan George Gabe Lance Carolyn Mariah"
+ " Hal Christina Christopher Mark Stephen Stanley Sharon Hannah"
+ " Gregory Barry Kawika Greg Derek Philip Alfredo Jillian Joedie"
+ " Anthony Kyle Bradley Masa Clyde Robert Zachary Jaron Fernando"
+ " Kosuke Becky Dora Rheada Ashley Dustin Joshuah Ricardo Pete"
+ " Katrina Arwin Mica Arlene Venus Jenny Nicole Jeylyn Trisha"
+ " Theresa Eric Terry Trenton Marcus Tristan Rueben Melvin"
+ " Kurtis Mary";
//Use name for names and nameLength for number of names
String[] nameData = namesList.split("\\s");
boolean[] nameInsert = new boolean[nameData.length];
for (String dataAdd : nameData) {
bst.insertLeft(dataAdd, null);

}
System.out.println("\nThe Oshiro NamesList has been added to the tree, printing tree:");
bst.traverse(1);
System.out.println(bst.toString());

Scanner userInput = new Scanner(System.in);
System.out.println("Options available:"
+ "\n[0]Add the names to the tree"
+ "\n[1]Delete a name from the tree"
+ "\n[2]Print out contents of tree in alphabetical order"
+ "\n[3]Print out contents of tree in reverse alphabetical order"
+ "\n[4]Search the tree for a specific name and return number of probes"
+ "\n[5]Destroy the tree"
+ "\n[6]Balance the tree"
+ "\n[7]Draw the tree as loaded"
+ "\n[8]Draw the tree completely balanced"
+ "\n[9]Draw the tree"
+ "\n[10]Exit");
int selection;

最佳答案

树向左生长,因为这就是您在代码中告诉它的内容

for(String dataAdd : nameData)
bst.insertLeft(dataAdd, null);

insertLeft,顾名思义,只在左侧插入,将树更像是一个链表

public void insertLeft(E key, T value)
{
Node<E, T> node = new Node<E, T>(key, value);
if(root == null)
root = node;
else
{
Node current = root;
while(current.left != null)
current = current.left;
current.left = node;
}
}

insertLeft 方法的结构运行良好,在遍历树以添加到树之前,检查基本情况树是否为空 (root == null)左

如果您希望它作为排序树的插入(Binary S每个T树),您可能希望对其进行一些编辑以考虑排序顺序

public void insertSorted(E key, T value)
{
Node<E, T> node = new Node<E, T>(key, value);
if(root == null) //keep the same base case
root = node;
else
{
Node current = root;
while(true)
if(node < current) //check left
if(current.left == null) //set left
{
current.left = node;
break;
}
else //iterate over left
current = current.left;
else //check right
if(current.right == null) //set right
{
current.right = node;
break;
}
else //iterate over right
current = current.right;
}
}

请注意,要在调用 insertSorted 后对树进行排序,树必须已经排序

另请注意,空树是排序的,就像只有 1 个节点的树一样

关于Java二叉搜索树插入方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47931935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com