[英]How do I create a unbalanced binary search tree
public class Tree { Node root;公共类树 { 节点根;
// Tree Node
static class Node {
int data;
Node left, right;
Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
@Override public String toString() {
return String.valueOf(data);
}
}
// Function to insert nodes in level order
public Node insertLevelOrder(int[] arr, Node root,
int i)
{
// Base case for recursion
if (i < arr.length) {
Node temp = new Node(arr[i]);
root = temp;
// insert left child
root.left = insertLevelOrder(arr, root.left,
i + 1);
// insert right child
//root.right = insertLevelOrder(arr, root.right,
// 2 * i + 2);
//System.out.println(root);
}
return root;
}
// Function to print tree nodes in InOrder fashion
public void inOrder(Node root)
{
if (root != null) {
inOrder(root.left);
System.out.print(root.left + " ");
//inOrder(root.right);
}
}
// Driver program to test above function
public static void main(String args[])
{
Tree t2 = new Tree();
int arr[] = {9,5,0,-3,-10};
t2.root = t2.insertLevelOrder(arr, t2.root, 0);
t2.inOrder(t2.root);
}
}
I don know if those nodes are been inserted or not, but the output return me right thing.我不知道这些节点是否已插入,但输出返回给我正确的东西。 I would like insert nodes only to the left child, can I eliminate that code?
我只想向左孩子插入节点,我可以消除该代码吗? root.right = insertLevelOrder(arr, root.right, 2 * i + 2);
root.right = insertLevelOrder(arr, root.right, 2 * i + 2);
And also why this loop doesn't have a sign that "i++", how does int i increase automatically?还有为什么这个循环没有“i++”的标志,int i如何自动增加?
There is a lot of missing things.有很多缺失的东西。 I will not solve everything for you (that is not the purpose of SO), but will give you hints.
我不会为您解决所有问题(这不是 SO 的目的),但会给您提示。
First, you need to have something to build a tree (set left and right node of a node) :首先,你需要有东西来构建一棵树(设置一个节点的左右节点):
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val=x;
}
TreeNode(int v,TreeNode l,TreeNode r) {
this(x);
left = l;
right = l;
}
@Override public String toString() {
return "(" + (left==null?"*":left) + "," + val + "," + (right==null?"*":right) + ")";
}
}
Thus you can then create (and print) a tree like this (for example) :因此,您可以创建(并打印)这样的树(例如):
TreeNode n1 = new TreeNode(1);
TreeNode n3 = new TreeNode(3);
TreeNode n2 = new TreeNode(2,n1,n3);
TreeNode n4 = new TreeNode(4,n3,null);
System.out.println(n4);
Second, write a function to get a node from a given value (left as an exercise, think about it, hint: recursive binary search).其次,编写一个函数从给定的值中获取一个节点(留作练习,想一想,提示:递归二分查找)。
Third you need a function to insert a node (left as an exercise to you), so you need:第三,您需要一个函数来插入节点(留给您作为练习),因此您需要:
I believe that your bug is here:我相信你的错误在这里:
root= new TreeNode(C[0]);
You shouldn't use C[0]
in cases where left
isn't 0.在
left
不是 0 的情况下,您不应该使用C[0]
。
Edit:编辑:
I don know if the elements has been inserted to the BST.
我不知道元素是否已插入 BST。
It seems that your suspicion is correct.看来你的怀疑是对的。 You are never setting
TreeNode.left
or TreeNode.right
to anything, which would be necessary for building a tree.您永远不会将
TreeNode.left
或TreeNode.right
设置为构建树所必需的任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.