java如何实现这个二叉树?

如何用java实现二叉树~

import java.util.List;
import java.util.LinkedList;

public class Bintrees {
private int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
private static List nodeList = null;

private static class Node {
Node leftChild;
Node rightChild;
int data;

Node(int newData) {
leftChild = null;
rightChild = null;
data = newData;
}
}

// 创建二叉树
public void createBintree() {
nodeList = new LinkedList();

// 将数组的值转换为node
for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {
nodeList.add(new Node(array[nodeIndex]));
}

// 对除最后一个父节点按照父节点和孩子节点的数字关系建立二叉树
for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {
nodeList.get(parentIndex).leftChild = nodeList.get(parentIndex * 2 + 1);
nodeList.get(parentIndex).rightChild = nodeList.get(parentIndex * 2 + 2);
}

// 最后一个父节点
int lastParentIndex = array.length / 2 - 1;

// 左孩子
nodeList.get(lastParentIndex).leftChild = nodeList.get(lastParentIndex * 2 + 1);

// 如果为奇数,建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).rightChild = nodeList.get(lastParentIndex * 2 + 2);
}
}

// 前序遍历
public static void preOrderTraverse(Node node) {
if (node == null) {
return;
}
System.out.print(node.data + " ");
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}

// 中序遍历
public static void inOrderTraverse(Node node) {
if (node == null) {
return;
}

inOrderTraverse(node.leftChild);
System.out.print(node.data + " ");
inOrderTraverse(node.rightChild);
}

// 后序遍历
public static void postOrderTraverse(Node node) {
if (node == null) {
return;
}

postOrderTraverse(node.leftChild);
postOrderTraverse(node.rightChild);
System.out.print(node.data + " ");
}

public static void main(String[] args) {
Bintrees binTree = new Bintrees();
binTree.createBintree();
Node root = nodeList.get(0);

System.out.println("前序遍历:");
preOrderTraverse(root);
System.out.println();

System.out.println("中序遍历:");
inOrderTraverse(root);
System.out.println();

System.out.println("后序遍历:");
postOrderTraverse(root);
}
}


输出结果:
前序遍历:
1 2 4 8 9 5 3 6 7
中序遍历:
8 4 9 2 5 1 6 3 7
后序遍历:
8 9 4 5 2 6 7 3 1

/**
* 二叉树测试二叉树顺序存储在treeLine中,递归前序创建二叉树。另外还有能
* 够前序、中序、后序、按层遍历二叉树的方法以及一个返回遍历结果asString的
* 方法。
*/

public class BitTree {
public static Node2 root;
public static String asString;

//事先存入的数组,符号#表示二叉树结束。
public static final char[] treeLine = {'a','b','c','d','e','f','g',' ',' ','j',' ',' ','i','#'};

//用于标志二叉树节点在数组中的存储位置,以便在创建二叉树时能够找到节点对应的数据。
static int index;

//构造函数
public BitTree() {
System.out.print("测试二叉树的顺序表示为:");
System.out.println(treeLine);
this.index = 0;
root = this.setup(root);
}

//创建二叉树的递归程序
private Node2 setup(Node2 current) {
if (index >= treeLine.length) return current;
if (treeLine[index] == '#') return current;
if (treeLine[index] == ' ') return current;
current = new Node2(treeLine[index]);
index = index * 2 + 1;
current.left = setup(current.left);
index ++;
current.right = setup(current.right);
index = index / 2 - 1;
return current;
}

//二叉树是否为空。
public boolean isEmpty() {
if (root == null) return true;
return false;
}

//返回遍历二叉树所得到的字符串。
public String toString(int type) {
if (type == 0) {
asString = "前序遍历:";
this.front(root);
}
if (type == 1) {
asString = "中序遍历:";
this.middle(root);
}
if (type == 2) {
asString = "后序遍历:";
this.rear(root);
}
if (type == 3) {
asString = "按层遍历:";
this.level(root);
}
return asString;
}

//前序遍历二叉树的循环算法,每到一个结点先输出,再压栈,然后访问它的左子树,
//出栈,访问其右子树,然后该次循环结束。
private void front(Node2 current) {
StackL stack = new StackL((Object)current);
do {
if (current == null) {
current = (Node2)stack.pop();
current = current.right;
} else {
asString += current.ch;
current = current.left;
}
if (!(current == null)) stack.push((Object)current);
} while (!(stack.isEmpty()));
}

//中序遍历二叉树
private void middle(Node2 current) {
if (current == null) return;
middle(current.left);
asString += current.ch;
middle(current.right);
}

//后序遍历二叉树的递归算法
private void rear(Node2 current) {
if (current == null) return;
rear(current.left);
rear(current.right);
asString += current.ch;
}

}


/**
* 二叉树所使用的节点类。包括一个值域两个链域
*/

public class Node2 {
char ch;
Node2 left;
Node2 right;

//构造函数
public Node2(char c) {
this.ch = c;
this.left = null;
this.right = null;
}

//设置节点的值
public void setChar(char c) {
this.ch = c;
}

//返回节点的值
public char getChar() {
return ch;
}

//设置节点的左孩子
public void setLeft(Node2 left) {
this.left = left;
}

//设置节点的右孩子
public void setRight (Node2 right) {
this.right = right;
}

//如果是叶节点返回true
public boolean isLeaf() {
if ((this.left == null) && (this.right == null)) return true;
return false;
}
}


一个作业题,里面有你要的东西。
主函数自己写吧。当然其它地方也有要改的。

我可以给你提供思路,用两个递归进行输出,println放在递归中间。

如何用java实现二叉树?
答:二叉树的二叉链表结点定义 */ typedef char datatype;typedef struct BiTNode { datatype data;struct BiTNode * LChild , * RChild ;} BiTNode , * BiTree ;//数叶子结点的数目 / Author: WadeFelix RenV / include <stdio.h> include <stdlib.h> include "BinaryTree.h"int countLeaf( BiTr...

二叉树的java实现与几种遍历
答:从定义可以看出,二叉树包括:1.空树 2.只有一个根节点 3.只有左子树 4.只有右子树 5.左右子树都存在 有且仅有这5种表现形式 二叉树的遍历分为三种:前序遍历 中序遍历 后序遍历 前序遍历:按照“根左右”,先遍历根节点,再遍历左子树 ,再遍历右子树 中序遍历:按照“左根右“,先...

用JAVA语言实现二叉树的层次遍历的非递归算法及查找算法。
答:方法1:访问T->data后,将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,再先序遍历T的右子树。方法2:访问T->data后,将T->rchild入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T->rchild,出栈,遍历以该指针为根的子树。【算法1】void PreOrder(BiTree T, Statu...

建立一个二叉树,附带查询代码,JAVA代码
答:import java.util.ArrayList;// 树的一个节点 class TreeNode { Object _value = null; // 他的值 TreeNode _parent = null; // 他的父节点,根节点没有PARENT ArrayList _childList = new ArrayList(); // 他的孩子节点 public TreeNode( Object value, TreeNode parent ){ this._parent ...

java 构建二叉树
答:树是树形的, 似乎不太合适。其实也可以用数组完成,而且效率更高.关键是我觉得你这个输入本身就是一个二叉树啊,String input = "ABCDE F G";节点编号从0到8. 层次遍历的话:对于节点i.leftChild = input.charAt(2*i+1); //做子树 rightChild = input.charAt(2*i+2);//右子树 如果你要...

java二叉树的顺序表实现
答:做了很多年的程序员,觉得什么树的设计并不是非常实用。二叉树有顺序存储,当一个insert大量同时顺序自增插入的时候,树就会失去平衡。树的一方为了不让塌陷,会增大树的高度。性能会非常不好。以上是题外话。分析需求在写代码。import java.util.List;import java.util.LinkedList;public class Bintrees {...

用java实现二叉树
答:了)可以很好地解决这个问题,但二叉树的遍历(前序,中序,后序)效率要比数组低很多,public class Node { public int value;public Node left;public Node right;public void store(intvalue)right.value=value;} else { right.store(value);} } } public boolean find(intvalue){ System.out....

java实现二叉树的问题
答:方法。/ public class BitTree { public static Node2 root;public static String asString;//事先存入的数组,符号#表示二叉树结束。public static final char[] treeLine = {'a','b','c','d','e','f','g',' ',' ','j',' ',' ','i','#'};//用于标志二叉树节点在数组中的...

如何用Java的方式设计一个后序线索二叉树的方法?
答:在Java中,你可以定义一个类来表示后序线索二叉树,其中包含有头节点、尾节点和当前节点指针。你可以使用递归或迭代方法遍历整棵树,并创建线索,即存储前驱和后继节点的指针。当访问到叶子节点时,需要将尾节点的指针指向它,尾节点的指针则指向头节点 // 定 ...

用JAVA写二叉树
答:/ [Tree2.java] Create on 2008-10-20 下午03:03:24 Copyright (c) 2008 by iTrusChina./ / author WangXuanmin version 0.10 / public class Tree2Bef { private StringBuffer bef=new StringBuffer();//传入中序遍历和后序遍历,返回前序遍历字串 public String getBef(String mid, String...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网