java中的二进制树实现
我有这个代码来创建和遍历二进制树java中的二进制树实现,java,algorithm,binary-tree,Java,Algorithm,Binary Tree,我有这个代码来创建和遍历二进制树 class Node { Integer data; Node left; Node right; Node() { data = null; left = null; right = null; } } class BinaryTree { Node head; Scanner input = new Scanner(System.in);
class Node
{
Integer data;
Node left;
Node right;
Node()
{
data = null;
left = null;
right = null;
}
}
class BinaryTree
{
Node head;
Scanner input = new Scanner(System.in);
BinaryTree()
{
head = null;
}
public void createNode(Node temp, Integer value)
{
Node newnode= new Node();
value = getData();
newnode.data = value;
temp = newnode;
if(head==null)
{
head = temp;
}
System.out.println("If left child exits for ("+value+") enter y else n");
if(input.next().charAt(0)=='y')
{
createNode(temp.left, value);
}
System.out.println("If right child exits for ("+value+") enter y else n");
if(input.next().charAt(0)=='y')
{
createNode(temp.right, value);
}
}
public Integer getData()
{
out.println("Enter the value to insert:");
return (Integer)input.nextInt();
}
public void print()
{
inorder(head);
}
public void inorder(Node node)
{
if(node!=null)
{
inorder(node.left);
System.out.println(node.data);
inorder(node.right);
}
else
return;
}
}
class BinaryTreeWorker
{
static BinaryTree treeObj = null;
static Scanner input = new Scanner(System.in);
public static void displaymenu()
{
int choice;
do{
out.print("\n Basic operations on a tree:");
out.print("\n 1. Create tree \n 2. Insert \n 3. Search value \n 4. print list\n Else. Exit \n Choice:");
choice = input.nextInt();
switch(choice)
{
case 1:
treeObj = createBTree();
break;
case 2:
treeObj.createNode(null, null);
break;
case 3:
//searchnode();
break;
case 4:
treeObj.print();
break;
default:
return;
}
}while(true);
}
public static BinaryTree createBTree()
{
return new BinaryTree();
}
public static void main(String[] args)
{
displaymenu();
}
}
它编译并运行。但我认为顺序遍历有问题
我创造了下面的树
2
1 3
2.
1 3
但它只打印2个。您的问题在于
public void createNodes(Node temp,T data)
函数。传入一个与类变量temp同名的参数。首先,我认为不需要类变量本身。第二,在这个方法中,赋值给temp只会产生局部效应-您在temp
参数中丢失了信息,但是设置temp不会影响它在被调用方法中的值。我建议您重写该方法,使其返回指向新创建节点的指针,并将该指针分配给本地temp
的left
和right
。这样,更改将传播出去。我已尝试以您的方式解决问题,并已将解决方案粘贴到下面。。虽然我还没有彻底测试过它,所以在某些边缘条件下它可能会失败。。但我已经测试了一个案例。如果在某些情况下失败,请告诉我。我希望其他人能帮助我更好地回答这个问题。我同意这个解决方案并不是编码二叉树的最理想的方法,但是如果有人只是在练习的话,它不会伤害到你
import java.util.Scanner;
class Node
{
Integer data;
Node left;
Node right;
Node()
{
data = null;
left = null;
right = null;
}
}
class BinaryTree
{
Node head;
Scanner input = new Scanner(System.in);
BinaryTree()
{
head = null;
}
public void createNode(Node temp,Node newnode)
{
if(head==null)
{
System.out.println("No value exist in tree, the value just entered is set to Root");
head = newnode;
return;
}
if(temp==null)
temp = head;
System.out.println("where you want to insert this value, l for left of ("+temp.data+") ,r for right of ("+temp.data+")");
char inputValue=input.next().charAt(0);
if(inputValue=='l'){
if(temp.left==null)
{
temp.left=newnode;
System.out.println("value got successfully added to left of ("+temp.data+")");
return;
}else {
System.out.println("value left to ("+temp.data+") is occupied 1by ("+temp.left.data+")");
createNode(temp.left,newnode);
}
}
else if(inputValue=='r')
{
if(temp.right==null)
{
temp.right=newnode;
System.out.println("value got successfully added to right of ("+temp.data+")");
return;
}else {
System.out.println("value right to ("+temp.data+") is occupied by ("+temp.right.data+")");
createNode(temp.right,newnode);
}
}else{
System.out.println("incorrect input plz try again , correctly");
return;
}
}
public Node generateTree(){
int [] a = new int[10];
int index = 0;
while(index<a.length){
a[index]=getData();
index++;
}
if(a.length==0 ){
return null;
}
Node newnode= new Node();
/*newnode.left=null;
newnode.right=null;*/
return generateTreeWithArray(newnode,a,0);
}
public Node generateTreeWithArray(Node head,int [] a,int index){
if(index >= a.length)
return null;
System.out.println("at index "+index+" value is "+a[index]);
if(head==null)
head= new Node();
head.data = a[index];
head.left=generateTreeWithArray(head.left,a,index*2+1);
head.right=generateTreeWithArray(head.right,a,index*2+2);
return head;
}
public Integer getData()
{
System.out.println("Enter the value to insert:");
return (Integer)input.nextInt();
}
public void print()
{
inorder(head);
}
public void inorder(Node node)
{
if(node!=null)
{
inorder(node.left);
System.out.println(node.data);
inorder(node.right);
}
else
return;
}
}
public class BinaryTreeWorker
{
static BinaryTree treeObj = null;
static Scanner input = new Scanner(System.in);
public static void displaymenu()
{
int choice;
do{
System.out.print("\n Basic operations on a tree:");
System.out.print("\n 1. Create tree \n 2. Insert \n 3. Search value \n 4. print list\n 5. generate a tree \n Else. Exit \n Choice:");
choice = input.nextInt();
switch(choice)
{
case 1:
treeObj = createBTree();
break;
case 2:
Node newnode= new Node();
newnode.data = getData();
newnode.left=null;
newnode.right=null;
treeObj.createNode(treeObj.head,newnode);
break;
case 3:
//searchnode();
break;
case 4:
System.out.println("inorder traversal of list gives follows");
treeObj.print();
break;
case 5:
Node tempHead = treeObj.generateTree();
System.out.println("inorder traversal of list with head = ("+tempHead.data+")gives follows");
treeObj.inorder(tempHead);
break;
default:
return;
}
}while(true);
}
public static Integer getData()
{
System.out.println("Enter the value to insert:");
return (Integer)input.nextInt();
}
public static BinaryTree createBTree()
{
return new BinaryTree();
}
public static void main(String[] args)
{
displaymenu();
}
}
import java.util.Scanner;
类节点
{
整数数据;
左淋巴结;
节点权;
节点()
{
数据=空;
左=空;
右=空;
}
}
类二叉树
{
节点头;
扫描仪输入=新扫描仪(System.in);
二叉树()
{
head=null;
}
public void createNode(节点临时、节点新节点)
{
if(head==null)
{
System.out.println(“树中不存在值,刚输入的值设置为Root”);
头=新节点;
返回;
}
if(temp==null)
温度=水头;
System.out.println(“要插入此值的位置,l表示(“+temp.data+”)的左侧,r表示(“+temp.data+”)的右侧);
char inputValue=input.next().charAt(0);
如果(inputValue=='l'){
如果(左临时==null)
{
temp.left=newnode;
System.out.println(“值成功添加到(“+temp.data+”)的左侧);
返回;
}否则{
System.out.println(“左至(“+temp.data+”)的值被(“+temp.left.data+”)占用1);
createNode(临时左,新节点);
}
}
else if(inputValue=='r')
{
如果(临时右==null)
{
temp.right=newnode;
System.out.println(“值已成功添加到(“+temp.data+”)的右侧”);
返回;
}否则{
System.out.println(“对(“+temp.data+”)的值权限被(“+temp.right.data+”)占用);
createNode(临时右侧,新节点);
}
}否则{
System.out.println(“输入错误,请重试,正确”);
返回;
}
}
公共节点generateTree(){
int[]a=新的int[10];
int指数=0;
while(索引=a.length)
返回null;
System.out.println(“at index”+index+”值为“+a[index]);
if(head==null)
头=新节点();
head.data=a[指数];
head.left=generateTreeWithArray(head.left,a,索引*2+1);
head.right=generateTreeWithArray(head.right,a,索引*2+2);
回流头;
}
公共整数getData()
{
System.out.println(“输入要插入的值:”);
返回(整数)input.nextInt();
}
公开作废印刷品()
{
顺序(头);
}
公共无效索引(节点)
{
如果(节点!=null)
{
顺序(node.left);
System.out.println(节点数据);
顺序(node.right);
}
其他的
返回;
}
}
公共类二进制树浏览器
{
静态二进制树treeObj=null;
静态扫描仪输入=新扫描仪(System.in);
公共静态无效显示菜单()
{
智力选择;
做{
System.out.print(“\n树上的基本操作:”);
System.out.print(“\n 1.创建树\n 2.插入\n 3.搜索值\n 4.打印列表\n 5.生成树\n其他。退出\n选项:”);
choice=input.nextInt();
开关(选择)
{
案例1:
treeObj=createBTree();
打破
案例2:
Node newnode=新节点();
newnode.data=getData();
newnode.left=null;
newnode.right=null;
createNode(treeObj.head,newnode);
打破
案例3:
//searchnode();
打破
案例4:
System.out.println(“列表的顺序遍历给出如下结果”);
treeObj.print();
打破
案例5:
节点tempHead=treeObj.generateTree();
System.out.println(“头=(“+tempHead.data+”)的列表的顺序遍历给出如下结果”);
treeObj.inoorder(tempHead);
打破
违约:
返回;
}
}虽然(正确);
}
公共静态整数getData()
{
System.out.println(“输入要插入的值:”);
返回(整数)input.nextInt();
}
公共静态二进制树createBTree()
{
返回新的二进制树();
}
公共静态void main(字符串[]args)
{
显示菜单();
}
}
[更新]:更新代码以使用数组生成二叉树。这将减少用户交互。另一个t
public void inorder()
{
inorder(root);
}
protected void visit(BSTNode<T> p)
{
System.out.println("Node: " + p.el + "Left Side:" + (p.left!=null?p.left.el:"null") +
"Right Side:" + (p.right!=null?p.right.el:"null"));
}
public Node createNode()
{
Integer value = getData();
Node temp = new Node(value);
if(head==null)
{
head = temp;
}
System.out.println("Do you want to add left branch on node("+value+")? Enter y/n");
if(input.next().charAt(0)=='y')
{
temp.left=createNode();
}
System.out.println("Do you want to add right branch on node("+value+")? Enter y/n");
if(input.next().charAt(0)=='y')
{
temp.right=createNode();
}
return temp;
}
Basic operations on a tree:
1. Create tree
2. Insert
3. Search value
4. print list
Else. Exit
Choice:1
Basic operations on a tree:
1. Create tree
2. Insert
3. Search value
4. print list
Else. Exit
Choice:2
Enter the value to insert:
10
Do you want to add left branch on node(10)? Enter y/n
y
Enter the value to insert:
20
Do you want to add left branch on node(20)? Enter y/n
n
Do you want to add right branch on node(20)? Enter y/n
n
Do you want to add right branch on node(10)? Enter y/n
y
Enter the value to insert:
30
Do you want to add left branch on node(30)? Enter y/n
n
Do you want to add right branch on node(30)? Enter y/n
n
Basic operations on a tree:
1. Create tree
2. Insert
3. Search value
4. print list
Else. Exit
Choice:4
20
10
30
public Node createNode(Node temp, Integer value)
{
Node newnode = new Node();
value = getData();
newnode.data = value;
temp = newnode;
if(head == null)
{
head = temp;
}
System.out.println("If left child exits for ("+value+") enter y else n");
if(input.next().charAt(0) == 'y')
{
newnode.left = createNode(newnode.left, value);
}
System.out.println("If right child exits for ("+value+") enter y else n");
if(input.next().charAt(0) == 'y')
{
newnode.right = createNode(newnode.right, value);
}
return newnode;
}
package com.nitin.tree;
public class Tree
{
private Node parent;
private int data;
private int size = 0;
public Tree() {
parent = new Node(data);
}
public void add(int data) {
if (size == 0) {
parent.data = data;
size++;
} else {
add(parent, new Node(data));
}
}
private void add(Node root, Node newNode) {
if (root == null) {
return;
}
if (newNode.data < root.data) {
if (root.left == null) {
root.left = newNode;
size++;
} else {
add(root.left, newNode);
}
} else {
if (root.right == null) {
root.right = newNode;
size++;
} else {
add(root.right, newNode);
}
}
}
public int getLow() {
Node current = parent;
while (current.left != null) {
current = current.left;
}
return current.data;
}
public int getHigh() {
Node current = parent;
while (current.right != null) {
current = current.right;
}
return current.data;
}
private void in(Node node) {
if (node != null) {
in(node.left);
System.out.print(node.data + " ");
in(node.right);
}
}
private void pre(Node node) {
if (node != null) {
System.out.print(node.data + " ");
pre(node.left);
pre(node.right);
}
}
private void post(Node node) {
if (node != null) {
post(node.left);
post(node.right);
System.out.print(node.data + " ");
}
}
public void preorder() {
System.out.print("Preorder Traversal->");
pre(parent);
System.out.println();
}
public void postorder() {
System.out.print("Postorder Traversal->");
post(parent);
System.out.println();
}
public void inorder() {
System.out.print("Inorder Traversal->");
in(parent);
System.out.println();
}
private class Node {
Node left;
Node right;
int data;
public Node(int data) {
this.data = data;
}
}
public String toString() {
Node current = parent;
System.out.print("Traverse From Left ");
while (current.left != null && current.right != null) {
System.out.print(current.data + "->[" + current.left.data + " " + current.right.data + "] ");
current = current.left;
}
System.out.println();
System.out.print("Traverse From Right ");
current = parent;
while (current.left != null && current.right != null) {
System.out.print(current.data + "->[" + current.left.data + " " + current.right.data + "] ");
current = current.right;
}
return "";
}
public static void main(String af[]) {
Tree t = new Tree();
t.add(40);
t.add(25);
t.add(78);
t.add(10);
t.add(32);
t.add(50);
t.add(93);
t.add(3);
t.add(17);
t.add(30);
t.add(38);
System.out.println(t.getLow());
System.out.println(t.getHigh());
System.out.println("Size-" + t.size);
System.out.println(t);
t.inorder();
t.preorder();
t.postorder();
}
}