Java中在二叉树中插入元素
我已经编写了一个代码,在java的二叉树中插入一个元素。以下是执行相同操作的函数:Java中在二叉树中插入元素,java,algorithm,binary-tree,insertion,Java,Algorithm,Binary Tree,Insertion,我已经编写了一个代码,在java的二叉树中插入一个元素。以下是执行相同操作的函数: public void insert(int data) { root = insert(root, data); } private Node insert(Node node, int data) { if (node == null) node = new Node(data); else
public void insert(int data)
{
root = insert(root, data);
}
private Node insert(Node node, int data)
{
if (node == null)
node = new Node(data);
else
{
if (node.getRight() == null)
node.right = insert(node.right, data);
else
node.left = insert(node.left, data);
}
return node;
}
然而,当我穿过这棵树时,我得到的答案是错误的。以下是遍历函数(预序):
好的,正如所建议的,这里是Node类的定义:
public class Node {
public int data;
public Node left, right;
/* Constructor */
public Node() {
left = null;
right = null;
data = 0;
}
/* Constructor */
public Node(int d, Node l, Node r) {
data = d;
left = l;
right = r;
}
//Constructor
public Node(int d) {
data = d;
}
/* Function to set link to next Node */
public void setLeft(Node l) {
left = l;
}
/* Function to set link to previous Node */
public void setRight(Node r) {
right = r;
}
/* Function to set data to current Node */
public void setData(int d) {
data = d;
}
/* Function to get link to next node */
public Node getLeft() {
return left;
}
/* Function to get link to previous node */
public Node getRight() {
return right;
}
/* Function to get data from current Node */
public int getData() {
return data;
}
}
我已经多次重新检查了遍历算法,它工作得非常好。我认为问题在于插入算法。有什么建议吗?您的方法返回给定的节点,但您的方法必须返回插入的节点,即node.right或node.left如果我理解正确,您希望在“层”中填充二叉树。例如,只有当深度3是“完整的二叉树”时,您才想在深度4中放入一些内容 然后问题是基于DFS的insert算法的整个逻辑。换句话说,它在一侧越来越深地插入元素,而不是在两侧构建完整的二叉树 如果你仔细观察你的插入算法,你会发现一旦你跳过“右”子树,你将永远不会回到它——即使“左”子树已经是完整的二叉树。这将导致树在左侧越来越深,但在右侧不会生长 说编程语言。你可以:
(node.right != null) && (node.left != null) => insert (node.left)
但您不能这样做(开始插入node.left)。如果node.left同时有子节点,而node.right没有子节点,该怎么办?即使应该在node.right中插入,也会尝试向左插入
所以你真正需要做的是基于BFS的插入。这意味着您将遍历树以“在层中”插入。这里的队列应该是您的新朋友:-)(不是堆栈/递归):
public void插入(int数据){
if(root==null){
根=新节点(数据);
返回;
}
Queue nodesToProcess=new LinkedList();
nodesToProcess.add(root);
while(true){
Node actualNode=nodesToProcess.poll();
//左边的子项优先于右边的子项
if(actualNode.left==null){
actualNode.left=新节点(数据);
返回;
}
if(actualNode.right==null){
actualNode.right=新节点(数据);
返回;
}
//我已经设置了两个孩子,如果需要,我会在以后处理他们
nodesToProcess.add(actualNode.left);
nodesToProcess.add(actualNode.right);
}
}
请查看下面给出的经过深思熟虑的答案,谢谢。
(node.right != null) && (node.left != null) => insert (node.left)
public void insert(int data) {
if (root == null) {
root = new Node(data);
return;
}
Queue<Node> nodesToProcess = new LinkedList<>();
nodesToProcess.add(root);
while (true) {
Node actualNode = nodesToProcess.poll();
// Left child has precedence over right one
if (actualNode.left == null) {
actualNode.left = new Node(data);
return;
}
if (actualNode.right == null) {
actualNode.right = new Node(data);
return;
}
// I have both children set, I will process them later if needed
nodesToProcess.add(actualNode.left);
nodesToProcess.add(actualNode.right);
}
}