import?java.awt.*;
成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站,成都做網(wǎng)站公司-成都創(chuàng)新互聯(lián)公司已向1000+企業(yè)提供了,網(wǎng)站設(shè)計(jì),網(wǎng)站制作,網(wǎng)絡(luò)營銷等服務(wù)!設(shè)計(jì)與技術(shù)結(jié)合,多年網(wǎng)站推廣經(jīng)驗(yàn),合理的價(jià)格為您打造企業(yè)品質(zhì)網(wǎng)站。
import?javax.swing.*;
class?TreeDemo?extends?JFrame
{
public?TreeDemo()
{
setSize(400,300);
setTitle("演示怎樣使用JTree");
show();
JScrollPane?jPanel=new?JScrollPane();
getContentPane().add(jPanel);
JTree?jtree=new?JTree();
jPanel.getViewport().add(jtree,null);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public?class?Example5_25
{
public?static?void?main(String[]?args)
{
TreeDemo?frame=new?TreeDemo();
}
}
其中JScrollPane是一個(gè)帶滾動(dòng)條的面板類。
將對(duì)象加入到帶滾動(dòng)條的面板類中,在將已建的數(shù)放入到其中。
就可建立一個(gè)系統(tǒng)默認(rèn)的樹結(jié)構(gòu)。
java構(gòu)造二叉樹,可以通過鏈表來構(gòu)造,如下代碼:
public?class?BinTree?{
public?final?static?int?MAX=40;
BinTree?[]elements?=?new?BinTree[MAX];//層次遍歷時(shí)保存各個(gè)節(jié)點(diǎn)
int?front;//層次遍歷時(shí)隊(duì)首
int?rear;//層次遍歷時(shí)隊(duì)尾
private?Object?data;?//數(shù)據(jù)元數(shù)
private?BinTree?left,right;?//指向左,右孩子結(jié)點(diǎn)的鏈
public?BinTree()
{
}
public?BinTree(Object?data)
{?//構(gòu)造有值結(jié)點(diǎn)
this.data?=?data;
left?=?right?=?null;
}
public?BinTree(Object?data,BinTree?left,BinTree?right)
{?//構(gòu)造有值結(jié)點(diǎn)
this.data?=?data;
this.left?=?left;
this.right?=?right;
}
public?String?toString()
{
return?data.toString();
}
//前序遍歷二叉樹
public?static?void?preOrder(BinTree?parent){?
if(parent?==?null)
return;
System.out.print(parent.data+"?");
preOrder(parent.left);
preOrder(parent.right);
}
//中序遍歷二叉樹
public?void?inOrder(BinTree?parent){
if(parent?==?null)
return;
inOrder(parent.left);
System.out.print(parent.data+"?");
inOrder(parent.right);
}
//后序遍歷二叉樹
public?void?postOrder(BinTree?parent){
if(parent?==?null)
return;
postOrder(parent.left);
postOrder(parent.right);
System.out.print(parent.data+"?");
}
//?層次遍歷二叉樹?
public?void?LayerOrder(BinTree?parent)
{?
elements[0]=parent;
front=0;rear=1;
while(frontrear)
{
try
{
if(elements[front].data!=null)
{
System.out.print(elements[front].data?+?"?");
if(elements[front].left!=null)
elements[rear++]=elements[front].left;
if(elements[front].right!=null)
elements[rear++]=elements[front].right;
front++;
}
}catch(Exception?e){break;}
}
}
//返回樹的葉節(jié)點(diǎn)個(gè)數(shù)
public?int?leaves()
{
if(this?==?null)
return?0;
if(left?==?nullright?==?null)
return?1;
return?(left?==?null???0?:?left.leaves())+(right?==?null???0?:?right.leaves());
}
//結(jié)果返回樹的高度
public?int?height()
{
int?heightOfTree;
if(this?==?null)
return?-1;
int?leftHeight?=?(left?==?null???0?:?left.height());
int?rightHeight?=?(right?==?null???0?:?right.height());
heightOfTree?=?leftHeightrightHeight?rightHeight:leftHeight;
return?1?+?heightOfTree;
}
//如果對(duì)象不在樹中,結(jié)果返回-1;否則結(jié)果返回該對(duì)象在樹中所處的層次,規(guī)定根節(jié)點(diǎn)為第一層
public?int?level(Object?object)
{
int?levelInTree;
if(this?==?null)
return?-1;
if(object?==?data)
return?1;//規(guī)定根節(jié)點(diǎn)為第一層
int?leftLevel?=?(left?==?null?-1:left.level(object));
int?rightLevel?=?(right?==?null?-1:right.level(object));
if(leftLevel0rightLevel0)
return?-1;
levelInTree?=?leftLevelrightLevel?rightLevel:leftLevel;
return?1+levelInTree;
}
//將樹中的每個(gè)節(jié)點(diǎn)的孩子對(duì)換位置
public?void?reflect()
{
if(this?==?null)
return;
if(left?!=?null)
left.reflect();
if(right?!=?null)
right.reflect();
BinTree?temp?=?left;
left?=?right;
right?=?temp;
}
//?將樹中的所有節(jié)點(diǎn)移走,并輸出移走的節(jié)點(diǎn)
public?void?defoliate()
{
if(this?==?null)
return;
//若本節(jié)點(diǎn)是葉節(jié)點(diǎn),則將其移走
if(left==nullright?==?null)
{
System.out.print(this?+?"?");
data?=?null;
return;
}
//移走左子樹若其存在
if(left!=null){
left.defoliate();
left?=?null;
}
//移走本節(jié)點(diǎn),放在中間表示中跟移走...
String?innerNode?+=?this?+?"?";
data?=?null;
//移走右子樹若其存在
if(right!=null){
right.defoliate();
right?=?null;
}
}
/**
*?@param?args
*/
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
BinTree?e?=?new?BinTree("E");
BinTree?g?=?new?BinTree("G");
BinTree?h?=?new?BinTree("H");
BinTree?i?=?new?BinTree("I");
BinTree?d?=?new?BinTree("D",null,g);
BinTree?f?=?new?BinTree("F",h,i);
BinTree?b?=?new?BinTree("B",d,e);
BinTree?c?=?new?BinTree("C",f,null);
BinTree?tree?=?new?BinTree("A",b,c);
System.out.println("前序遍歷二叉樹結(jié)果:?");
tree.preOrder(tree);
System.out.println();
System.out.println("中序遍歷二叉樹結(jié)果:?");
tree.inOrder(tree);
System.out.println();
System.out.println("后序遍歷二叉樹結(jié)果:?");
tree.postOrder(tree);
System.out.println();
System.out.println("層次遍歷二叉樹結(jié)果:?");
tree.LayerOrder(tree);
System.out.println();
System.out.println("F所在的層次:?"+tree.level("F"));
System.out.println("這棵二叉樹的高度:?"+tree.height());
System.out.println("--------------------------------------");
tree.reflect();
System.out.println("交換每個(gè)節(jié)點(diǎn)的孩子節(jié)點(diǎn)后......");
System.out.println("前序遍歷二叉樹結(jié)果:?");
tree.preOrder(tree);
System.out.println();
System.out.println("中序遍歷二叉樹結(jié)果:?");
tree.inOrder(tree);
System.out.println();
System.out.println("后序遍歷二叉樹結(jié)果:?");
tree.postOrder(tree);
System.out.println();
System.out.println("層次遍歷二叉樹結(jié)果:?");
tree.LayerOrder(tree);
System.out.println();
System.out.println("F所在的層次:?"+tree.level("F"));
System.out.println("這棵二叉樹的高度:?"+tree.height());
}
首先我想問為什么要用LinkedList 來建立二叉樹呢? LinkedList 是線性表,
樹是樹形的, 似乎不太合適。
其實(shí)也可以用數(shù)組完成,而且效率更高.
關(guān)鍵是我覺得你這個(gè)輸入本身就是一個(gè)二叉樹啊,
String input = "ABCDE F G";
節(jié)點(diǎn)編號(hào)從0到8. 層次遍歷的話:
對(duì)于節(jié)點(diǎn)i.
leftChild = input.charAt(2*i+1); //做子樹
rightChild = input.charAt(2*i+2);//右子樹
如果你要將帶有節(jié)點(diǎn)信息的樹存到LinkedList里面, 先建立一個(gè)節(jié)點(diǎn)類:
class Node{
public char cValue;
public Node leftChild;
public Node rightChild;
public Node(v){
this.cValue = v;
}
}
然后遍歷input,建立各個(gè)節(jié)點(diǎn)對(duì)象.
LinkedList tree = new LinkedList();
for(int i=0;i input.length;i++)
LinkedList.add(new Node(input.charAt(i)));
然后為各個(gè)節(jié)點(diǎn)設(shè)置左右子樹:
for(int i=0;iinput.length;i++){
((Node)tree.get(i)).leftChild = (Node)tree.get(2*i+1);
((Node)tree.get(i)).rightChild = (Node)tree.get(2*i+2);
}
這樣LinkedList 就存儲(chǔ)了整個(gè)二叉樹. 而第0個(gè)元素就是樹根,思路大體是這樣吧。
計(jì)算機(jī)科學(xué)中,二叉樹是每個(gè)結(jié)點(diǎn)最多有兩個(gè)子樹的有序樹。通常子樹的根被稱作“左子樹”(left
subtree)和“右子樹”(right
subtree)。二叉樹常被用作二叉查找樹和二叉堆或是二叉排序樹。
二叉樹的每個(gè)結(jié)點(diǎn)至多只有二棵子樹(不存在度大于2的結(jié)點(diǎn)),二叉樹的子樹有左右之分,次序不能顛倒。二叉樹的第i層至多有2的
i
-1次方個(gè)結(jié)點(diǎn);深度為k的二叉樹至多有2^(k)
-1個(gè)結(jié)點(diǎn);對(duì)任何一棵二叉樹T,如果其終端結(jié)點(diǎn)數(shù)(即葉子結(jié)點(diǎn)數(shù))為n0,度為2的結(jié)點(diǎn)數(shù)為n2,則n0
=
n2
+
1。
樹是由一個(gè)或多個(gè)結(jié)點(diǎn)組成的有限集合,其中:
⒈必有一個(gè)特定的稱為根(ROOT)的結(jié)點(diǎn);
二叉樹
⒉剩下的結(jié)點(diǎn)被分成n=0個(gè)互不相交的集合T1、T2、......Tn,而且,
這些集合的每一個(gè)又都是樹。樹T1、T2、......Tn被稱作根的子樹(Subtree)。
樹的遞歸定義如下:(1)至少有一個(gè)結(jié)點(diǎn)(稱為根)(2)其它是互不相交的子樹
1.樹的度——也即是寬度,簡單地說,就是結(jié)點(diǎn)的分支數(shù)。以組成該樹各結(jié)點(diǎn)中最大的度作為該樹的度,如上圖的樹,其度為2;樹中度為零的結(jié)點(diǎn)稱為葉結(jié)點(diǎn)或終端結(jié)點(diǎn)。樹中度不為零的結(jié)點(diǎn)稱為分枝結(jié)點(diǎn)或非終端結(jié)點(diǎn)。除根結(jié)點(diǎn)外的分枝結(jié)點(diǎn)統(tǒng)稱為內(nèi)部結(jié)點(diǎn)。
2.樹的深度——組成該樹各結(jié)點(diǎn)的最大層次。
3.森林——指若干棵互不相交的樹的集合,如上圖,去掉根結(jié)點(diǎn)A,其原來的二棵子樹T1、T2、T3的集合{T1,T2,T3}就為森林;
4.有序樹——指樹中同層結(jié)點(diǎn)從左到右有次序排列,它們之間的次序不能互換,這樣的樹稱為有序樹,否則稱為無序樹。
樹的表示
樹的表示方法有許多,常用的方法是用括號(hào):先將根結(jié)點(diǎn)放入一對(duì)圓括號(hào)中,然后把它的子樹由左至右的順序放入括號(hào)中,而對(duì)子樹也采用同樣的方法處理;同層子樹與它的根結(jié)點(diǎn)用圓括號(hào)括起來,同層子樹之間用逗號(hào)隔開,最后用閉括號(hào)括起來。如右圖可寫成如下形式:
二叉樹
(a(
b(d,e),
c(
f(
,g(h,i)
),
)))
二叉樹的相關(guān)操作,包括創(chuàng)建,中序、先序、后序(遞歸和非遞歸),其中重點(diǎn)的是java在先序創(chuàng)建二叉樹和后序非遞歸遍歷的的實(shí)現(xiàn)。
package com.algorithm.tree;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;
public class Tree {
private Node root;
public Tree() {
}
public Tree(Node root) {
this.root = root;
}
//創(chuàng)建二叉樹
public void buildTree() {
Scanner scn = null;
try {
scn = new Scanner(new File("input.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
root = createTree(root,scn);
}
//先序遍歷創(chuàng)建二叉樹
private Node createTree(Node node,Scanner scn) {
String temp = scn.next();
if (temp.trim().equals("#")) {
return null;
} else {
node = new Node((T)temp);
node.setLeft(createTree(node.getLeft(), scn));
node.setRight(createTree(node.getRight(), scn));
return node;
}
}
//中序遍歷(遞歸)
public void inOrderTraverse() {
inOrderTraverse(root);
}
public void inOrderTraverse(Node node) {
if (node != null) {
inOrderTraverse(node.getLeft());
System.out.println(node.getValue());
inOrderTraverse(node.getRight());
}
}
//中序遍歷(非遞歸)
public void nrInOrderTraverse() {
StackNode stack = new StackNode();
Node node = root;
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
node = stack.pop();
System.out.println(node.getValue());
node = node.getRight();
}
}
//先序遍歷(遞歸)
public void preOrderTraverse() {
preOrderTraverse(root);
}
public void preOrderTraverse(Node node) {
if (node != null) {
System.out.println(node.getValue());
preOrderTraverse(node.getLeft());
preOrderTraverse(node.getRight());
}
}
//先序遍歷(非遞歸)
public void nrPreOrderTraverse() {
StackNode stack = new StackNode();
Node node = root;
while (node != null || !stack.isEmpty()) {
while (node != null) {
System.out.println(node.getValue());
stack.push(node);
node = node.getLeft();
}
node = stack.pop();
node = node.getRight();
}
}
//后序遍歷(遞歸)
public void postOrderTraverse() {
postOrderTraverse(root);
}
public void postOrderTraverse(Node node) {
if (node != null) {
postOrderTraverse(node.getLeft());
postOrderTraverse(node.getRight());
System.out.println(node.getValue());
}
}
//后續(xù)遍歷(非遞歸)
public void nrPostOrderTraverse() {
StackNode stack = new StackNode();
Node node = root;
Node preNode = null;//表示最近一次訪問的節(jié)點(diǎn)
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
node = stack.peek();
if (node.getRight() == null || node.getRight() == preNode) {
System.out.println(node.getValue());
node = stack.pop();
preNode = node;
node = null;
} else {
node = node.getRight();
}
}
}
//按層次遍歷
public void levelTraverse() {
levelTraverse(root);
}
public void levelTraverse(Node node) {
QueueNode queue = new LinkedBlockingQueueNode();
queue.add(node);
while (!queue.isEmpty()) {
Node temp = queue.poll();
if (temp != null) {
System.out.println(temp.getValue());
queue.add(temp.getLeft());
queue.add(temp.getRight());
}
}
}
}
//樹的節(jié)點(diǎn)
class Node {
private Node left;
private Node right;
private T value;
public Node() {
}
public Node(Node left,Node right,T value) {
this.left = left;
this.right = right;
this.value = value;
}
public Node(T value) {
this(null,null,value);
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
測(cè)試代碼:
package com.algorithm.tree;
public class TreeTest {
/**
* @param args
*/
public static void main(String[] args) {
Tree tree = new Tree();
tree.buildTree();
System.out.println("中序遍歷");
tree.inOrderTraverse();
tree.nrInOrderTraverse();
System.out.println("后續(xù)遍歷");
//tree.nrPostOrderTraverse();
tree.postOrderTraverse();
tree.nrPostOrderTraverse();
System.out.println("先序遍歷");
tree.preOrderTraverse();
tree.nrPreOrderTraverse();
//
}
}
import?java.util.Stack;//導(dǎo)入棧包
public?class?newtree?{
private?newtree?lchild;//?聲明數(shù)據(jù)成員
private?newtree?rchild;
private?char?data;
private?newtree?root;
public?newtree(newtree?l,?newtree?r,?char?data)?{//?有參構(gòu)造函數(shù)進(jìn)行成員賦值
lchild?=?l;
rchild?=?r;
this.data?=?data;
}
public?newtree()?{//?無參構(gòu)造函數(shù)創(chuàng)建樹
newtree?f?=?new?newtree(null,?null,?'f');
newtree?g?=?new?newtree(null,?null,?'g');
newtree?d?=?new?newtree(null,?null,?'d');
newtree?e?=?new?newtree(null,?null,?'e');
newtree?b?=?new?newtree(d,?e,?'b');
newtree?c?=?new?newtree(f,?g,?'c');
newtree?a?=?new?newtree(b,?c,?'a');
this.root=a;
}
public?void?visit(newtree?p)?{/*?輸出數(shù)據(jù)?*/
System.out.print(p.data);//?訪問結(jié)點(diǎn)
}
@SuppressWarnings("unchecked")
public?void?InOrder()?{/*?輸入數(shù)據(jù)?*/
newtree?p=this.root;//你建了一棵樹要把根節(jié)點(diǎn)賦值進(jìn)去啊
Stack?s?=?new?Stack();
while?(p?!=?null?||?!s.isEmpty())?/*?處理數(shù)據(jù):進(jìn)行中序遍歷?*/
{
if?(p?!=?null)?{
s.push(p);
p?=?p.lchild;
}?else?{
p?=?(newtree)?s.pop();
p.visit(p);//this指的是當(dāng)前的類對(duì)象
p?=?p.rchild;
}
}
}
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
newtree?h?=?new?newtree();//?聲明變量,變量賦值
h.InOrder();
}
}
//根據(jù)你的代碼改了一個(gè)
import?java.util.Stack;//導(dǎo)入棧包
public?class?newtree?{
public?Tree?createTree()?{//?無參構(gòu)造函數(shù)創(chuàng)建樹
Tree?f?=?new?Tree(null,?null,?'f');
Tree?g?=?new?Tree(null,?null,?'g');
Tree?d?=?new?Tree(null,?null,?'d');
Tree?e?=?new?Tree(null,?null,?'e');
Tree?b?=?new?Tree(d,?e,?'b');
Tree?c?=?new?Tree(f,?g,?'c');
Tree?a?=?new?Tree(b,?c,?'a');
return?a;
}
public?void?InOrder(Tree?p)?{/*?輸入數(shù)據(jù)?*/
StackTree?s?=?new?StackTree();
while?(p?!=?null?||?!s.isEmpty())?{?/*?處理數(shù)據(jù):進(jìn)行中序遍歷?*/
if?(p?!=?null)?{
s.push(p);
p?=?p.lchild;
}?else?{
p?=?s.pop();
System.out.print(p.data);
p?=?p.rchild;
}
}
}
public?void?inOrder1(Tree?p)?{
if?(p?==?null)
return;
inOrder1(p.lchild);
System.out.print(p.data);
inOrder1(p.rchild);
}
public?static?void?main(String[]?args)?{
newtree?h?=?new?newtree();//?聲明變量,變量賦值
h.InOrder(h.createTree());
System.out.println();
h.inOrder1(h.createTree());
}
}
class?Tree?{
Tree?lchild;//?聲明數(shù)據(jù)成員
Tree?rchild;
char?data;
Tree(Tree?lchild,?Tree?rchild,?char?data)?{
this.lchild?=?lchild;
this.rchild?=?rchild;
this.data?=?data;
}
}