導航:首頁 > 編程語言 > java排序二叉樹

java排序二叉樹

發布時間:2024-10-04 09:30:23

1. 求數據結構(java版)實驗樹和二叉樹題目答案

/**
* @param args
之前在大學的時候寫的一個二叉樹演算法,運行應該沒有問題,就看適不適合你的項目了 */
public static void main(String[] args) {

BiTree e = new BiTree(5);
BiTree g = new BiTree(7);
BiTree h = new BiTree(8);
BiTree l = new BiTree(12);
BiTree m = new BiTree(13);
BiTree n = new BiTree(14);
BiTree k = new BiTree(11, n, null);
BiTree j = new BiTree(10, l, m);
BiTree i = new BiTree(9, j, k);
BiTree d = new BiTree(4, null, g);
BiTree f = new BiTree(6, h, i);
BiTree b = new BiTree(2, d, e);
BiTree c = new BiTree(3, f, null);
BiTree tree = new BiTree(1, b, c);
System.out.println("遞歸前序遍歷二叉樹結果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("非遞歸前序遍歷二叉樹結果: ");
tree.iterativePreOrder(tree);
System.out.println();
System.out.println("遞歸中序遍歷二叉樹的結果為:");
tree.inOrder(tree);
System.out.println();
System.out.println("非遞歸中序遍歷二叉樹的結果為:");
tree.iterativeInOrder(tree);
System.out.println();
System.out.println("遞歸後序遍歷二叉樹的結果為:");
tree.postOrder(tree);
System.out.println();
System.out.println("非遞歸後序遍歷二叉樹的結果為:");
tree.iterativePostOrder(tree);
System.out.println();
System.out.println("層次遍歷二叉樹結果: ");
tree.LayerOrder(tree);
System.out.println();
System.out.println("遞歸求二叉樹中所有結點的和為:"+getSumByRecursion(tree));
System.out.println("非遞歸求二叉樹中所有結點的和為:"+getSumByNoRecursion(tree));

System.out.println("二叉樹中,每個節點所在的層數為:");
for (int p = 1; p <= 14; p++)
System.out.println(p + "所在的層為:" + tree.level(p));
System.out.println("二叉樹的高度為:" + height(tree));
System.out.println("二叉樹中節點總數為:" + nodes(tree));
System.out.println("二叉樹中葉子節點總數為:" + leaf(tree));
System.out.println("二叉樹中父節點總數為:" + fatherNodes(tree));
System.out.println("二叉樹中只擁有一個孩子的父節點數:" + oneChildFather(tree));
System.out.println("二叉樹中只擁有左孩子的父節點總數:" + leftChildFather(tree));
System.out.println("二叉樹中只擁有右孩子的父節點總數:" + rightChildFather(tree));
System.out.println("二叉樹中同時擁有兩個孩子的父節點個數為:" + doubleChildFather(tree));
System.out.println("--------------------------------------");
tree.exChange();
System.out.println("交換每個節點的左右孩子節點後......");
System.out.println("遞歸前序遍歷二叉樹結果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("非遞歸前序遍歷二叉樹結果: ");
tree.iterativePreOrder(tree);
System.out.println();
System.out.println("遞歸中序遍歷二叉樹的結果為:");
tree.inOrder(tree);
System.out.println();
System.out.println("非遞歸中序遍歷二叉樹的結果為:");
tree.iterativeInOrder(tree);
System.out.println();
System.out.println("遞歸後序遍歷二叉樹的結果為:");
tree.postOrder(tree);
System.out.println();
System.out.println("非遞歸後序遍歷二叉樹的結果為:");
tree.iterativePostOrder(tree);
System.out.println();
System.out.println("層次遍歷二叉樹結果: ");
tree.LayerOrder(tree);
System.out.println();

System.out.println("遞歸求二叉樹中所有結點的和為:"+getSumByRecursion(tree));
System.out.println("非遞歸求二叉樹中所有結點的和為:"+getSumByNoRecursion(tree));

System.out.println("二叉樹中,每個節點所在的層數為:");
for (int p = 1; p <= 14; p++)
System.out.println(p + "所在的層為:" + tree.level(p));
System.out.println("二叉樹的高度為:" + height(tree));
System.out.println("二叉樹中節點總數為:" + nodes(tree));
System.out.println("二叉樹中葉子節點總數為:" + leaf(tree));
System.out.println("二叉樹中父節點總數為:" + fatherNodes(tree));
System.out.println("二叉樹中只擁有一個孩子的父節點數:" + oneChildFather(tree));
System.out.println("二叉樹中只擁有左孩子的父節點總數:" + leftChildFather(tree));
System.out.println("二叉樹中只擁有右孩子的父節點總數:" + rightChildFather(tree));
System.out.println("二叉樹中同時擁有兩個孩子的父節點個數為:" + doubleChildFather(tree));
}
}

2. 如何用Java的方式設計一個後序線索二叉樹的方法

在Java中,你可以定義一哪激弊個類來表示後序線索二叉樹,其中包含有頭節點、尾節點和當前節點指針。你可以使用遞歸或迭代方法遍歷整棵樹,並創建線索,即存儲前驅和後繼節點的指針。當訪問到葉子節點時,需要將尾節點的指針指向它,尾節點鉛隱的指李族針則指向頭節點
// 定

3. java二叉樹遍歷問題

二叉樹具有以下重要性質:
性質1 二叉樹第i層上的結點數目最多為2i-1(i≥1)。
證明:用數學歸納法證明:
歸納基礎:i=1時,有2i-1=20=1。因為第1層上只有一個根結點,所以命題成立。
歸納假設:假設對所有的j(1≤j<i)命題成立,即第j層上至多有2j-1個結點,證明j=i時命題亦成立。
歸納步驟:根據歸納假設,第i-1層上至多有2i-2個結點。由於二叉樹的每個結點至多有兩個孩子,故第i層上的結點數至多是第i-1層上的最大結點數的2倍。即j=i時,該層上至多有2×2i-2=2i-1個結點,故命題成立。

性質2 深度為k的二叉樹至多有2k-1個結點(k≥1)。
證明:在具有相同深度的二叉樹中,僅當每一層都含有最大結點數時,其樹中結點數最多。因此利用性質1可得,深度為k的二叉樹的結點數至多為:
20+21+…+2k-1=2k-1
故命題正確。

性質3 在任意-棵二叉樹中,若終端結點的個數為n0,度為2的結點數為n2,則no=n2+1。
證明:因為二叉樹中所有結點的度數均不大於2,所以結點總數(記為n)應等於0度結點數、1度結點(記為n1)和2度結點數之和:
n=no+n1+n2 (式子1)
另一方面,1度結點有一個孩子,2度結點有兩個孩子,故二叉樹中孩子結點總數是:
nl+2n2
樹中只有根結點不是任何結點的孩子,故二叉樹中的結點總數又可表示為:
n=n1+2n2+1 (式子2)
由式子1和式子2得到:
no=n2+1

滿二叉樹和完全二叉樹是二叉樹的兩種特殊情形。
1、滿二叉樹(FullBinaryTree)
一棵深度為k且有2k-1個結點的二又樹稱為滿二叉樹。
滿二叉樹的特點:
(1) 每一層上的結點數都達到最大值。即對給定的高度,它是具有最多結點數的二叉樹。
(2) 滿二叉樹中不存在度數為1的結點,每個分支結點均有兩棵高度相同的子樹,且樹葉都在最下一層上。
圖(a)是一個深度為4的滿二叉樹。

2、完全二叉樹(Complete BinaryTree)
若一棵二叉樹至多隻有最下面的兩層上結點的度數可以小於2,並且最下一層上的結點都集中在該層最左邊的若干位置上,則此二叉樹稱為完全二叉樹。
特點:
(1) 滿二叉樹是完全二叉樹,完全二叉樹不一定是滿二叉樹。
(2) 在滿二叉樹的最下一層上,從最右邊開始連續刪去若干結點後得到的二叉樹仍然是一棵完全二叉樹。
(3) 在完全二叉樹中,若某個結點沒有左孩子,則它一定沒有右孩子,即該結點必是葉結點。
如圖(c)中,結點F沒有左孩子而有右孩子L,故它不是一棵完全二叉樹。
圖(b)是一棵完全二叉樹。

性質4 具有n個結點的完全二叉樹的深度為

證明:設所求完全二叉樹的深度為k。由完全二叉樹定義可得:
深度為k得完全二叉樹的前k-1層是深度為k-1的滿二叉樹,一共有2k-1-1個結點。
由於完全二叉樹深度為k,故第k層上還有若干個結點,因此該完全二叉樹的結點個數:
n>2k-1-1。
另一方面,由性質2可得:
n≤2k-1,
即:2k-1-l<n≤2k-1
由此可推出:2k-1≤n<2k,取對數後有:
k-1≤lgn<k
又因k-1和k是相鄰的兩個整數,故有
,
由此即得:

注意:
的證明

4. 二叉樹的java實現與幾種遍歷

二叉樹的定義

二叉樹(binary tree)是結點的有限集合,這個集合或者空,或者由一個根及兩個互不相交的稱為這個根的左子樹或右子樹構成.

從定義可以看出,二叉樹包括:1.空樹 2.只有一個根節點 3.只有左子樹 4.只有右子樹 5.左右子樹都存在 有且僅有這5種表現形式

二叉樹的遍歷分為三種:前序遍歷 中序遍歷 後序遍歷

其中前,後,中指的是每次遍歷時候的根節點被遍歷的順序

具體實現看下圖:

5. Java數據結構二叉樹深度遞歸調用演算法求內部演算法過程詳解

二叉樹
1
2 3
4 5 6 7
這個二叉樹的深度是3,樹的深度是最大結點所在的層,這里是3.

應該計算所有結點層數,選擇最大的那個。

根據上面的二叉樹代碼,遞歸過程是:

f(1)=f(2)+1 > f(3) +1 ? f(2) + 1 : f(3) +1

f(2) 跟f(3)計算類似上面,要計算左右結點,然後取大者

所以計算順序是f(4.left) = 0, f(4.right) = 0

f(4) = f(4.right) + 1 = 1

然後計算f(5.left) = 0,f(5.right) = 0

f(5) = f(5.right) + 1 =1

f(2) = f(5) + 1 =2

f(1.left) 計算完畢,計算f(1.right) f(3) 跟計算f(2)的過程一樣。

得到f(3) = f(7) +1 = 2

f(1) = f(3) + 1 =3

if(depleft>depright){
returndepleft+1;
}else{
returndepright+1;
}

只有left大於right的時候採取left +1,相等是取right

6. 寫一個java層次遍歷二叉樹,簡單點就可以,我要的是代碼,不是純文字說明

public class BinaryNode {
Object element;
BinaryNode left;
BinaryNode right;

}

import java.util.*;

public class Queue {

protected LinkedList list;

// Postcondition: this Queue object has been initialized.
public Queue() {

list = new LinkedList();

} // default constructor

// Postcondition: the number of elements in this Queue object has been
// returned.
public int size() {

return list.size();

} // method size

// Postcondition: true has been returned if this Queue object has no
// elements. Otherwise, false has been returned.
public boolean isEmpty() {

return list.isEmpty();

} // method isEmpty

// Postconditon: A of element has been inserted at the back of this
// Queue object. The averageTime (n) is constant and
// worstTime (n) is O (n).
public void enqueue(Object element) {

list.addLast(element);

} // method enqueue

// Precondition: this Queue object is not empty. Otherwise,
// NoSuchElementException will be thrown.
// Postcondition: The element that was at the front of this Queue object -
// just before this method was called -- has been removed
// from this Queue object and returned.
public Object dequeue() {

return list.removeFirst();

} // method dequeue

// Precondition: this Queue object is not empty. Otherwise,
// NoSuchElementException will be thrown.
// Postcondition: the element at index 0 in this Queue object has been
// returned.
public Object front() {

return list.getFirst();

} // method front

} // Queue class

import java.io.IOException;

public class BinaryTree {
BinaryNode root;

public BinaryTree() {
super();
// TODO 自動生成構造函數存根
root=this.createPre();
}

public BinaryNode createPre()
//按照先序遍歷的輸入方法,建立二叉樹
{
BinaryNode t=null;
char ch;
try {
ch = (char)System.in.read();

if(ch==' ')
t=null;
else
{
t=new BinaryNode();
t.element=(Object)ch;
t.left=createPre();
t.right=createPre();
}
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
return t;
}

public void inOrder()
{
this.inOrder(root);
}

public void inOrder(BinaryNode t)
//中序遍歷二叉樹
{
if(t!=null)
{
inOrder(t.left);
System.out.print(t.element);
inOrder(t.right);
}
}

public void postOrder()
{
this.postOrder(root);
}

public void postOrder(BinaryNode t)
//後序遍歷二叉樹
{
if(t!=null)
{
postOrder(t.left);
System.out.print(t.element);
postOrder(t.right);
}
}

public void preOrder()
{
this.preOrder(root);
}
public void preOrder(BinaryNode t)
//前序遍歷二叉樹
{
if(t!=null)
{
System.out.print(t.element);
preOrder(t.left);
preOrder(t.right);
}
}

public void breadthFirst()
{
Queue treeQueue=new Queue();
BinaryNode p;
if(root!=null)
treeQueue.enqueue(root);
while(!treeQueue.isEmpty())
{
System.out.print(((BinaryNode)(treeQueue.front())).element);
p=(BinaryNode)treeQueue.dequeue();
if(p.left!=null)
treeQueue.enqueue(p.left);
if(p.right!=null)
treeQueue.enqueue(p.right);
}
}
}

public class BinaryTreeTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成方法存根
BinaryTree tree = new BinaryTree();

System.out.println("先序遍歷:");
tree.preOrder();
System.out.println();

System.out.println("中序遍歷:");
tree.inOrder();
System.out.println();

System.out.println("後序遍歷:");
tree.postOrder();
System.out.println();

System.out.println("層次遍歷:");
tree.breadthFirst();
System.out.println();
}

}

7. 如果想成為一個程序員應該學習什麼知識

聲明:以後計算機也就沒什麼前景了 只能成為一個輔助工具
(1) 熟練掌握基本演算法和數據結構,用C語言編製程序;
(2) 掌握數據結構、程序變速器和操作系統的基礎知識;
(3) 了解軟體工程、資料庫、多媒體和網路的基礎知識;
(4) 掌握數制、機內代碼及其算術運算和邏輯運算的基礎知識;
(5) 了解計算機的體系結構和主要部件的基礎知識。
1.1按照軟體設計說明書,能熟練使用下列程序語言編製程序
1.1.1C(美國標准)
1.2理解給定程序的功能,能發現程序中的錯誤並加以糾正
1.3具有良好的程序編制風格
1.4基本演算法
1.4.1查找、更新、排序、合並及字元處理
1.4.2迭代、插值、數值積分、矩陣計算、議程求解和搜索求解
1.5程序編制方法
1.5.1分支、循環、子程序(過程和函數)、遞歸
1.5.2輸入輸出和文件的處理
模塊2:基礎知識(程序員級)
2.1軟體基礎知識
2.1.1數據結構基礎
數組、記錄、列表(list)、隊列、棧(stack)、堆(heap)、樹和二叉樹的定義、存儲和操作
2.1.2程序語言基礎知識
·匯編、編譯、解釋系統的基礎知識
·程序語言的數據類型
·程序語言的控制結構
2.1.3操作系統基礎知識
·操作系統的類型和功能
·操作系統的層次結構和進程概念
·作業、處理機、存儲、文件和設備等管理的基礎知識
2.1.4軟體工程基礎知識
·軟體生命周期各階段的任務
·結構化分析和設計方法
·面向對象基本概念
·軟體開發工具與環境基礎知識
2.1.5資料庫基礎知識
·資料庫模型
·資料庫系統的結構
·SQL的使用初步
·常用資料庫管理系統的知識
2.1.6多媒體基礎知識
·多媒體基本概念
·常用圖象文件
·簡單圖形(線段和圓)的計算機圖示
2.2硬體基礎知識
2.2.1數制及其轉換
·二進制、十進制和十六進制等常用數制及其相互轉換
2.2.2機內代碼
·原碼、補碼、反碼、移碼
·定點數與浮點數的機內表示
·ASCII碼及漢字編碼等常用的編碼
·奇偶校驗碼、海明碼、循環冗餘碼(CRC)等常用校驗方法
2.2.3算術運算和邏輯運算
·計算機中的二進制數運算方法
·邏輯代數的基本運算和邏輯表達式的化簡
2.2.4計算機的體系結構和主要部件
·中央處理器CPU、存儲器和輸入/輸出設備
·匯流排結構、指令流和數據流
2.2.5指令系統
·常用的定址方式
·指令的格式、分類及功能
·指令的執行過程
2.3網路基礎知識
2.3.1網路的功能、分類與組成
2.3.2網路協議與標准
2.3.3網路結構與通信
2.3.4網路的安全性
2.3.5Client-Server結構
2.3.6Internet和Intranet初步

閱讀全文

與java排序二叉樹相關的資料

熱點內容
北漂程序員互聯網 瀏覽:240
程序員實現不了一些效果 瀏覽:790
php框架的編譯 瀏覽:737
地基處理加密技巧 瀏覽:199
戰地為什麼總是斷開伺服器 瀏覽:256
ios解壓縮rar 瀏覽:960
如何用java做一個web伺服器 瀏覽:150
電子製冷和壓縮製冷哪個好 瀏覽:940
餐飲潮汕丸子簡介在app怎麼寫 瀏覽:786
特斯拉app怎麼綁定多輛車 瀏覽:417
aed伺服器是什麼 瀏覽:402
imagemagick壓縮gif 瀏覽:915
iphonex方舟編譯器 瀏覽:652
kepware的伺服器端點如何設置 瀏覽:370
用python自製掃雷 瀏覽:334
xboxones手柄如何配對安卓 瀏覽:490
湖南郴州java程序員培訓機構 瀏覽:135
服從命令成語 瀏覽:229
gcc編譯鏈 瀏覽:831
android人工智慧 瀏覽:10