我有很多個(gè)(假設(shè)10萬(wàn)個(gè))數(shù)據(jù)要保存起來(lái),以后還需要從保存的這些數(shù)據(jù)中檢索是否存在某
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),安陸企業(yè)網(wǎng)站建設(shè),安陸品牌網(wǎng)站建設(shè),網(wǎng)站定制,安陸網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,安陸網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
個(gè)數(shù)據(jù),(我想說(shuō)出二叉樹(shù)的好處,該怎么說(shuō)呢?那就是說(shuō)別人的缺點(diǎn)),假如存在數(shù)組中,
那么,碰巧要找的數(shù)字位于99999那個(gè)地方,那查找的速度將很慢,因?yàn)橐獜牡?個(gè)依次往
后取,取出來(lái)后進(jìn)行比較。平衡二叉樹(shù)(構(gòu)建平衡二叉樹(shù)需要先排序,我們這里就不作考慮
了)可以很好地解決這個(gè)問(wèn)題,但二叉樹(shù)的遍歷(前序,中序,后序)效率要比數(shù)組低很多,
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.println("happen" +this.value);
if(value ==this.value)
{
return true;
}
else if(valuethis.value)
{
if(right ==null)returnfalse;
return right.find(value);
}else
{
if(left ==null)returnfalse;
return left.find(value);
}
}
public void preList()
{
System.out.print(this.value+ ",");
if(left!=null)left.preList();
if(right!=null) right.preList();
}
public void middleList()
{
if(left!=null)left.preList();
System.out.print(this.value+ ",");
if(right!=null)right.preList();
}
public void afterList()
{
if(left!=null)left.preList();
if(right!=null)right.preList();
System.out.print(this.value+ ",");
}
public static voidmain(String [] args)
{
int [] data =new int[20];
for(inti=0;idata.length;i++)
{
data[i] = (int)(Math.random()*100)+ 1;
System.out.print(data[i] +",");
}
System.out.println();
Node root = new Node();
root.value = data[0];
for(inti=1;idata.length;i++)
{
root.store(data[i]);
}
root.find(data[19]);
root.preList();
System.out.println();
root.middleList();
System.out.println();
root.afterList();
}
}
計(jì)算機(jī)科學(xué)中,二叉樹(shù)是每個(gè)結(jié)點(diǎn)最多有兩個(gè)子樹(shù)的有序樹(shù)。通常子樹(shù)的根被稱作“左子樹(shù)”(left
subtree)和“右子樹(shù)”(right
subtree)。二叉樹(shù)常被用作二叉查找樹(shù)和二叉堆或是二叉排序樹(shù)。
二叉樹(shù)的每個(gè)結(jié)點(diǎn)至多只有二棵子樹(shù)(不存在度大于2的結(jié)點(diǎn)),二叉樹(shù)的子樹(shù)有左右之分,次序不能顛倒。二叉樹(shù)的第i層至多有2的
i
-1次方個(gè)結(jié)點(diǎn);深度為k的二叉樹(shù)至多有2^(k)
-1個(gè)結(jié)點(diǎn);對(duì)任何一棵二叉樹(shù)T,如果其終端結(jié)點(diǎn)數(shù)(即葉子結(jié)點(diǎn)數(shù))為n0,度為2的結(jié)點(diǎn)數(shù)為n2,則n0
=
n2
+
1。
樹(shù)是由一個(gè)或多個(gè)結(jié)點(diǎn)組成的有限集合,其中:
⒈必有一個(gè)特定的稱為根(ROOT)的結(jié)點(diǎn);
二叉樹(shù)
⒉剩下的結(jié)點(diǎn)被分成n=0個(gè)互不相交的集合T1、T2、......Tn,而且,
這些集合的每一個(gè)又都是樹(shù)。樹(shù)T1、T2、......Tn被稱作根的子樹(shù)(Subtree)。
樹(shù)的遞歸定義如下:(1)至少有一個(gè)結(jié)點(diǎn)(稱為根)(2)其它是互不相交的子樹(shù)
1.樹(shù)的度——也即是寬度,簡(jiǎn)單地說(shuō),就是結(jié)點(diǎn)的分支數(shù)。以組成該樹(shù)各結(jié)點(diǎn)中最大的度作為該樹(shù)的度,如上圖的樹(shù),其度為2;樹(shù)中度為零的結(jié)點(diǎn)稱為葉結(jié)點(diǎn)或終端結(jié)點(diǎn)。樹(shù)中度不為零的結(jié)點(diǎn)稱為分枝結(jié)點(diǎn)或非終端結(jié)點(diǎn)。除根結(jié)點(diǎn)外的分枝結(jié)點(diǎn)統(tǒng)稱為內(nèi)部結(jié)點(diǎn)。
2.樹(shù)的深度——組成該樹(shù)各結(jié)點(diǎn)的最大層次。
3.森林——指若干棵互不相交的樹(shù)的集合,如上圖,去掉根結(jié)點(diǎn)A,其原來(lái)的二棵子樹(shù)T1、T2、T3的集合{T1,T2,T3}就為森林;
4.有序樹(shù)——指樹(shù)中同層結(jié)點(diǎn)從左到右有次序排列,它們之間的次序不能互換,這樣的樹(shù)稱為有序樹(shù),否則稱為無(wú)序樹(shù)。
樹(shù)的表示
樹(shù)的表示方法有許多,常用的方法是用括號(hào):先將根結(jié)點(diǎn)放入一對(duì)圓括號(hào)中,然后把它的子樹(shù)由左至右的順序放入括號(hào)中,而對(duì)子樹(shù)也采用同樣的方法處理;同層子樹(shù)與它的根結(jié)點(diǎn)用圓括號(hào)括起來(lái),同層子樹(shù)之間用逗號(hào)隔開(kāi),最后用閉括號(hào)括起來(lái)。如右圖可寫(xiě)成如下形式:
二叉樹(shù)
(a(
b(d,e),
c(
f(
,g(h,i)
),
)))
二叉樹(shù)的相關(guān)操作,包括創(chuàng)建,中序、先序、后序(遞歸和非遞歸),其中重點(diǎn)的是java在先序創(chuàng)建二叉樹(shù)和后序非遞歸遍歷的的實(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)建二叉樹(shù)
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)建二叉樹(shù)
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;//表示最近一次訪問(wèn)的節(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());
}
}
}
}
//樹(shù)的節(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();
//
}
}
//******************************************************************************************************//
//*****本程序包括簡(jiǎn)單的二叉樹(shù)類的實(shí)現(xiàn)和前序,中序,后序,層次遍歷二叉樹(shù)算法,*******//
//******以及確定二叉樹(shù)的高度,制定對(duì)象在樹(shù)中的所處層次以及將樹(shù)中的左右***********//
//******孩子節(jié)點(diǎn)對(duì)換位置,返回葉子節(jié)點(diǎn)個(gè)數(shù)刪除葉子節(jié)點(diǎn),并輸出所刪除的葉子節(jié)點(diǎn)**//
//*******************************CopyRight By phoenix*******************************************//
//************************************Jan 12,2008*************************************************//
//****************************************************************************************************//
public class BinTree {
public final static int MAX=40;
private Object data; //數(shù)據(jù)元數(shù)
private BinTree left,right; //指向左,右孩子結(jié)點(diǎn)的鏈
BinTree []elements = new BinTree[MAX];//層次遍歷時(shí)保存各個(gè)節(jié)點(diǎn)
int front;//層次遍歷時(shí)隊(duì)首
int rear;//層次遍歷時(shí)隊(duì)尾
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();
}//前序遍歷二叉樹(shù)
public static void preOrder(BinTree parent){
if(parent == null)
return;
System.out.print(parent.data+" ");
preOrder(parent.left);
preOrder(parent.right);
}//中序遍歷二叉樹(shù)
public void inOrder(BinTree parent){
if(parent == null)
return;
inOrder(parent.left);
System.out.print(parent.data+" ");
inOrder(parent.right);
}//后序遍歷二叉樹(shù)
public void postOrder(BinTree parent){
if(parent == null)
return;
postOrder(parent.left);
postOrder(parent.right);
System.out.print(parent.data+" ");
}// 層次遍歷二叉樹(shù)
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;}
}
}//返回樹(shù)的葉節(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é)果返回樹(shù)的高度
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ì)象不在樹(shù)中,結(jié)果返回-1;否則結(jié)果返回該對(duì)象在樹(shù)中所處的層次,規(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;
}
//將樹(shù)中的每個(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;
}// 將樹(shù)中的所有節(jié)點(diǎn)移走,并輸出移走的節(jié)點(diǎn)
public void defoliate()
{
String innerNode = "";
if(this == null)
return;
//若本節(jié)點(diǎn)是葉節(jié)點(diǎn),則將其移走
if(left==nullright == null)
{
System.out.print(this + " ");
data = null;
return;
}
//移走左子樹(shù)若其存在
if(left!=null){
left.defoliate();
left = null;
}
//移走本節(jié)點(diǎn),放在中間表示中跟移走...
innerNode += this + " ";
data = null;
//移走右子樹(shù)若其存在
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("前序遍歷二叉樹(shù)結(jié)果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("中序遍歷二叉樹(shù)結(jié)果: ");
tree.inOrder(tree);
System.out.println();
System.out.println("后序遍歷二叉樹(shù)結(jié)果: ");
tree.postOrder(tree);
System.out.println();
System.out.println("層次遍歷二叉樹(shù)結(jié)果: ");
tree.LayerOrder(tree);
System.out.println();
System.out.println("F所在的層次: "+tree.level("F"));
System.out.println("這棵二叉樹(shù)的高度: "+tree.height());
System.out.println("--------------------------------------");
tree.reflect();
System.out.println("交換每個(gè)節(jié)點(diǎn)的孩子節(jié)點(diǎn)后......");
System.out.println("前序遍歷二叉樹(shù)結(jié)果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("中序遍歷二叉樹(shù)結(jié)果: ");
tree.inOrder(tree);
System.out.println();
System.out.println("后序遍歷二叉樹(shù)結(jié)果: ");
tree.postOrder(tree);
System.out.println();
System.out.println("層次遍歷二叉樹(shù)結(jié)果: ");
tree.LayerOrder(tree);
System.out.println();
System.out.println("F所在的層次: "+tree.level("F"));
System.out.println("這棵二叉樹(shù)的高度: "+tree.height());
}
java構(gòu)造二叉樹(shù),可以通過(guò)鏈表來(lái)構(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();}//前序遍歷二叉樹(shù)public static void preOrder(BinTree parent){ if(parent == null) return; System.out.print(parent.data+" "); preOrder(parent.left); preOrder(parent.right);}//中序遍歷二叉樹(shù)public void inOrder(BinTree parent){ if(parent == null) return; inOrder(parent.left); System.out.print(parent.data+" "); inOrder(parent.right);}//后序遍歷二叉樹(shù)public void postOrder(BinTree parent){ if(parent == null) return; postOrder(parent.left); postOrder(parent.right); System.out.print(parent.data+" ");}// 層次遍歷二叉樹(shù) 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;} }}//返回樹(shù)的葉節(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é)果返回樹(shù)的高度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ì)象不在樹(shù)中,結(jié)果返回-1;否則結(jié)果返回該對(duì)象在樹(shù)中所處的層次,規(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; }//將樹(shù)中的每個(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;}// 將樹(shù)中的所有節(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; } //移走左子樹(shù)若其存在 if(left!=null){ left.defoliate(); left = null; } //移走本節(jié)點(diǎn),放在中間表示中跟移走... String innerNode += this + " "; data = null; //移走右子樹(shù)若其存在 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("前序遍歷二叉樹(shù)結(jié)果: "); tree.preOrder(tree); System.out.println(); System.out.println("中序遍歷二叉樹(shù)結(jié)果: "); tree.inOrder(tree); System.out.println(); System.out.println("后序遍歷二叉樹(shù)結(jié)果: "); tree.postOrder(tree); System.out.println(); System.out.println("層次遍歷二叉樹(shù)結(jié)果: "); tree.LayerOrder(tree); System.out.println(); System.out.println("F所在的層次: "+tree.level("F")); System.out.println("這棵二叉樹(shù)的高度: "+tree.height()); System.out.println("--------------------------------------"); tree.reflect(); System.out.println("交換每個(gè)節(jié)點(diǎn)的孩子節(jié)點(diǎn)后......"); System.out.println("前序遍歷二叉樹(shù)結(jié)果: "); tree.preOrder(tree); System.out.println(); System.out.println("中序遍歷二叉樹(shù)結(jié)果: "); tree.inOrder(tree); System.out.println(); System.out.println("后序遍歷二叉樹(shù)結(jié)果: "); tree.postOrder(tree); System.out.println(); System.out.println("層次遍歷二叉樹(shù)結(jié)果: "); tree.LayerOrder(tree); System.out.println(); System.out.println("F所在的層次: "+tree.level("F")); System.out.println("這棵二叉樹(shù)的高度: "+tree.height());
首先我想問(wèn)為什么要用LinkedList 來(lái)建立二叉樹(shù)呢? LinkedList 是線性表,
樹(shù)是樹(shù)形的, 似乎不太合適。
其實(shí)也可以用數(shù)組完成,而且效率更高.
關(guān)鍵是我覺(jué)得你這個(gè)輸入本身就是一個(gè)二叉樹(shù)啊,
String input = "ABCDE F G";
節(jié)點(diǎn)編號(hào)從0到8. 層次遍歷的話:
對(duì)于節(jié)點(diǎn)i.
leftChild = input.charAt(2*i+1); //做子樹(shù)
rightChild = input.charAt(2*i+2);//右子樹(shù)
如果你要將帶有節(jié)點(diǎn)信息的樹(shù)存到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è)置左右子樹(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è)二叉樹(shù). 而第0個(gè)元素就是樹(shù)根,思路大體是這樣吧。