二叉樹java代碼 二叉樹 java

java一個關(guān)于二叉樹的簡單編程題

定義一個結(jié)點類:

網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及定制網(wǎng)站建設(shè)服務(wù),專注于成都定制網(wǎng)站,高端網(wǎng)頁制作,對成都水處理設(shè)備等多個行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計,網(wǎng)站優(yōu)化推廣哪家好,專業(yè)成都網(wǎng)站營銷優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。

public class Node {

private int value;

private Node leftNode;

private Node rightNode;

public Node getRightNode() {

return rightNode;

}

public void setRightNode(Node rightNode) {

this.rightNode = rightNode;

}

public int getValue() {

return value;

}

public void setValue(int value) {

this.value = value;

}

public Node getLeftNode() {

return leftNode;

}

public void setLeftNode(Node leftNode) {

this.leftNode = leftNode;

}

}

初始化結(jié)點樹:

public void initNodeTree()

{

int nodeNumber;

HashMapString, Integer map = new HashMapString, Integer();

Node nodeTree = new Node();

Scanner reader = new Scanner(System.in);

nodeNumber = reader.nextInt();

for(int i = 0; i nodeNumber; i++) {

int value = reader.nextInt();

String str = reader.next();

map.put(str, value);

}

if (map.containsKey("#")) {

int value = map.get("#");

nodeTree.setValue(value);

setChildNode(map, value, nodeTree);

}

preTraversal(nodeTree);

}

private void setChildNode(HashMapString, Integer map, int nodeValue, Node parentNode) {

int value = 0;

if (map.containsKey("L" + nodeValue)) {

value = map.get("L" + nodeValue);

Node leftNode = new Node();

leftNode.setValue(value);

parentNode.setLeftNode(leftNode);

setChildNode(map, value, leftNode);

}

if (map.containsKey("R" + nodeValue)) {

value = map.get("R" + nodeValue);

Node rightNode = new Node();

rightNode.setValue(value);

parentNode.setRightNode(rightNode);

setChildNode(map, value, rightNode);

}

}

前序遍歷該結(jié)點樹:

public void preTraversal(Node nodeTree) {

if (nodeTree != null) {

System.out.print(nodeTree.getValue() + "\t");

preTraversal(nodeTree.getLeftNode());

preTraversal(nodeTree.getRightNode());

}

}

java如何創(chuàng)建一顆二叉樹

計算機科學中,二叉樹是每個結(jié)點最多有兩個子樹的有序樹。通常子樹的根被稱作“左子樹”(left subtree)和“右子樹”(right subtree)。二叉樹常被用作二叉查找樹和二叉堆或是二叉排序樹。

二叉樹的每個結(jié)點至多只有二棵子樹(不存在度大于2的結(jié)點),二叉樹的子樹有左右之分,次序不能顛倒。二叉樹的第i層至多有2的 i -1次方個結(jié)點;深度為k的二叉樹至多有2^(k) -1個結(jié)點;對任何一棵二叉樹T,如果其終端結(jié)點數(shù)(即葉子結(jié)點數(shù))為n0,度為2的結(jié)點數(shù)為n2,則n0 = n2 + 1。

樹是由一個或多個結(jié)點組成的有限集合,其中:

⒈必有一個特定的稱為根(ROOT)的結(jié)點;

二叉樹

⒉剩下的結(jié)點被分成n=0個互不相交的集合T1、T2、......Tn,而且, 這些集合的每一個又都是樹。樹T1、T2、......Tn被稱作根的子樹(Subtree)。

樹的遞歸定義如下:(1)至少有一個結(jié)點(稱為根)(2)其它是互不相交的子樹

1.樹的度——也即是寬度,簡單地說,就是結(jié)點的分支數(shù)。以組成該樹各結(jié)點中最大的度作為該樹的度,如上圖的樹,其度為2;樹中度為零的結(jié)點稱為葉結(jié)點或終端結(jié)點。樹中度不為零的結(jié)點稱為分枝結(jié)點或非終端結(jié)點。除根結(jié)點外的分枝結(jié)點統(tǒng)稱為內(nèi)部結(jié)點。

2.樹的深度——組成該樹各結(jié)點的最大層次。

3.森林——指若干棵互不相交的樹的集合,如上圖,去掉根結(jié)點A,其原來的二棵子樹T1、T2、T3的集合{T1,T2,T3}就為森林;

4.有序樹——指樹中同層結(jié)點從左到右有次序排列,它們之間的次序不能互換,這樣的樹稱為有序樹,否則稱為無序樹。

樹的表示

樹的表示方法有許多,常用的方法是用括號:先將根結(jié)點放入一對圓括號中,然后把它的子樹由左至右的順序放入括號中,而對子樹也采用同樣的方法處理;同層子樹與它的根結(jié)點用圓括號括起來,同層子樹之間用逗號隔開,最后用閉括號括起來。如右圖可寫成如下形式:

二叉樹

(a( b(d,e), c( f( ,g(h,i) ), )))

JAVA 2差數(shù) 代碼 有人知道怎么寫么

你說的是二叉樹吧·····

/**

* 二叉樹測試二叉樹順序存儲在treeLine中,遞歸前序創(chuàng)建二叉樹。另外還有能

* 夠前序、中序、后序、按層遍歷二叉樹的方法以及一個返回遍歷結(jié)果asString的

* 方法。

*/

public class BitTree {

public static Node2 root;

public static String asString;

//事先存入的數(shù)組,符號#表示二叉樹結(jié)束。

public static final char[] treeLine = {'a','b','c','d','e','f','g',' ',' ','j',' ',' ','i','#'};

//用于標志二叉樹節(jié)點在數(shù)組中的存儲位置,以便在創(chuàng)建二叉樹時能夠找到節(jié)點對應(yīng)的數(shù)據(jù)。

static int index;

//構(gòu)造函數(shù)

public BitTree() {

System.out.print("測試二叉樹的順序表示為:");

System.out.println(treeLine);

this.index = 0;

root = this.setup(root);

}

//創(chuàng)建二叉樹的遞歸程序

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 = "前序遍歷:\t";

this.front(root);

}

if (type == 1) {

asString = "中序遍歷:\t";

this.middle(root);

}

if (type == 2) {

asString = "后序遍歷:\t";

this.rear(root);

}

if (type == 3) {

asString = "按層遍歷:\t";

this.level(root);

}

return asString;

}

//前序遍歷二叉樹的循環(huán)算法,每到一個結(jié)點先輸出,再壓棧,然后訪問它的左子樹,

//出棧,訪問其右子樹,然后該次循環(huán)結(jié)束。

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;

}

}

/**

* 二叉樹所使用的節(jié)點類。包括一個值域兩個鏈域

*/

public class Node2 {

char ch;

Node2 left;

Node2 right;

//構(gòu)造函數(shù)

public Node2(char c) {

this.ch = c;

this.left = null;

this.right = null;

}

//設(shè)置節(jié)點的值

public void setChar(char c) {

this.ch = c;

}

//返回節(jié)點的值

public char getChar() {

return ch;

}

//設(shè)置節(jié)點的左孩子

public void setLeft(Node2 left) {

this.left = left;

}

//設(shè)置節(jié)點的右孩子

public void setRight (Node2 right) {

this.right = right;

}

//如果是葉節(jié)點返回true

public boolean isLeaf() {

if ((this.left == null) (this.right == null)) return true;

return false;

}

}

一個作業(yè)題,里面有你要的東西。

主函數(shù)自己寫吧。當然其它地方也有要改的。

用java實現(xiàn)二叉樹

我有很多個(假設(shè)10萬個)數(shù)據(jù)要保存起來,以后還需要從保存的這些數(shù)據(jù)中檢索是否存在某

個數(shù)據(jù),(我想說出二叉樹的好處,該怎么說呢?那就是說別人的缺點),假如存在數(shù)組中,

那么,碰巧要找的數(shù)字位于99999那個地方,那查找的速度將很慢,因為要從第1個依次往

后取,取出來后進行比較。平衡二叉樹(構(gòu)建平衡二叉樹需要先排序,我們這里就不作考慮

了)可以很好地解決這個問題,但二叉樹的遍歷(前序,中序,后序)效率要比數(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();

}

}

文章題目:二叉樹java代碼 二叉樹 java
網(wǎng)站鏈接:http://muchs.cn/article4/dococoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站設(shè)計、網(wǎng)站內(nèi)鏈外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站改版、手機網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化