public class BinaryNode {
網(wǎng)站的建設(shè)成都創(chuàng)新互聯(lián)公司專注網(wǎng)站定制,經(jīng)驗豐富,不做模板,主營網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁面制作!給你煥然一新的設(shè)計體驗!已為iso認證等企業(yè)提供專業(yè)服務(wù)。
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 copy 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 自動生成構(gòu)造函數(shù)存根
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();
}
}
package tree;
import java.util.LinkedList;
import java.util.List;
/**
* 功能:把一個數(shù)組的值存入二叉樹中,然后進行3種方式的遍歷
*
* 參考資料0:數(shù)據(jù)結(jié)構(gòu)(C語言版)嚴蔚敏
*
* 參考資料1:
*
* 參考資料2:
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
public class BinTreeTraverse2 {
private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static ListNode nodeList = null;
/**
* 內(nèi)部類:節(jié)點
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
private static class Node {
Node leftChild;
Node rightChild;
int data;
Node(int newData) {
leftChild = null;
rightChild = null;
data = newData;
}
}
public void createBinTree() {
nodeList = new LinkedListNode();
// 將一個數(shù)組的值依次轉(zhuǎn)換為Node節(jié)點
for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {
nodeList.add(new Node(array[nodeIndex]));
}
// 對前l(fā)astParentIndex-1個父節(jié)點按照父節(jié)點與孩子節(jié)點的數(shù)字關(guān)系建立二叉樹
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);
}
// 最后一個父節(jié)點:因為最后一個父節(jié)點可能沒有右孩子,所以單獨拿出來處理
int lastParentIndex = array.length / 2 - 1;
// 左孩子
nodeList.get(lastParentIndex).leftChild = nodeList
.get(lastParentIndex * 2 + 1);
// 右孩子,如果數(shù)組的長度為奇數(shù)才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).rightChild = nodeList
.get(lastParentIndex * 2 + 2);
}
}
/**
* 先序遍歷
*
* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已
*
* @param node
* 遍歷的節(jié)點
*/
public static void preOrderTraverse(Node node) {
if (node == null)
return;
System.out.print(node.data + " ");
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}
/**
* 中序遍歷
*
* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已
*
* @param node
* 遍歷的節(jié)點
*/
public static void inOrderTraverse(Node node) {
if (node == null)
return;
inOrderTraverse(node.leftChild);
System.out.print(node.data + " ");
inOrderTraverse(node.rightChild);
}
/**
* 后序遍歷
*
* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已
*
* @param node
* 遍歷的節(jié)點
*/
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) {
BinTreeTraverse2 binTree = new BinTreeTraverse2();
binTree.createBinTree();
// nodeList中第0個索引處的值即為根節(jié)點
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);
}
}
我看了一下,知道lz的錯誤在哪了。
createbintree方法中。以lz的講的先輸入a、再輸入三回車,也就是當前只有一個節(jié)點a。下面就以樓主的數(shù)據(jù)來講一下函數(shù)所走流程
1.可是當樓主輸入a之后,按完第一個回車
當前取得的數(shù)據(jù)a,走else分支,接下來就到myTree.data = str;//為節(jié)點賦值,假設(shè)為父節(jié)點1
myTree.lchild = new BintNode();//為myTree建立了左節(jié)點
createbintree(myTree.lchild);
//遞歸調(diào)用跳到2
myTree.rchild = new BintNode();//創(chuàng)建右節(jié)點
createbintree(myTree.rchild);
//遞歸調(diào)用跳到3
2.輸入第二個回車,數(shù)據(jù)為空,那么走if分支,myTree=null,照理說應(yīng)該將myTree該節(jié)點賦值為空,也就代表沒有該節(jié)點。
其實這個想法是錯誤的。在1中,為父節(jié)點1分配了左孩子,已經(jīng)是new出來的對象。不知道lz對函數(shù)調(diào)用中的形參(也就是函數(shù)名所帶的參數(shù))的值,是怎么理解。有兩種形式的
1)值傳遞:即使是值傳遞,在函數(shù)體中,改變的也只是一個臨時變量的值。并不會影響到實參的值
2)引用傳遞:對象間的傳遞,而這傳遞只是引用關(guān)系的傳遞。就是將該參數(shù)指向該類,如果在函數(shù)體中設(shè)為null,也只是將形參與實際對象間的引用關(guān)系給去掉
本程序中,是屬于引用傳遞,在createbintree將myTree=null,也只是斷掉myTree與外部對象的關(guān)系而已,即父節(jié)點1的左孩子間的關(guān)系,所以父節(jié)點1的左孩子不為null
3.與2同樣的解釋,也可知道右孩子也不為空。
那么在調(diào)用num來計算葉子的個數(shù)時候,是不是根結(jié)點一開始進來,左右孩子都不為null,所以自然最后一個else。那么往下,lz再計算一下就知道結(jié)果為2
建議修改的話:
在createbintree方法中,if ("".equals(str.trim()))為空時,則myTree.data=null
然后在num方法中,計算個數(shù)的話,利用myTree.data==null return 0 ; myTree.lchild.data==null myTree.rchild.data==null return 1 ; else 一樣
當前名稱:二叉索引樹java代碼 二叉搜索樹java實現(xiàn)
當前URL:http://jinyejixie.com/article10/hpcdgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、標簽優(yōu)化、企業(yè)網(wǎng)站制作、移動網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、小程序開發(fā)
聲明:本網(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)