欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java中樹的代碼,java b樹代碼

建立一個二叉樹,附帶查詢代碼,JAVA代碼

import java.util.ArrayList;

創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)裕安,十多年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220

// 樹的一個節(jié)點

class TreeNode {

Object _value = null; // 他的值

TreeNode _parent = null; // 他的父節(jié)點,根節(jié)點沒有PARENT

ArrayList _childList = new ArrayList(); // 他的孩子節(jié)點

public TreeNode( Object value, TreeNode parent ){

this._parent = parent;

this._value = value;

}

public TreeNode getParent(){

return _parent;

}

public String toString() {

return _value.toString();

}

}

public class Tree {

// 給出寬度優(yōu)先遍歷的值數(shù)組,構(gòu)建出一棵多叉樹

// null 值表示一個層次的結(jié)束

// "|" 表示一個層次中一個父親節(jié)點的孩子輸入結(jié)束

// 如:給定下面的值數(shù)組:

// { "root", null, "left", "right", null }

// 則構(gòu)建出一個根節(jié)點,帶有兩個孩子("left","right")的樹

public Tree( Object[] values ){

// 創(chuàng)建根

_root = new TreeNode( values[0], null );

// 創(chuàng)建下面的子節(jié)點

TreeNode currentParent = _root; // 用于待創(chuàng)建節(jié)點的父親

//TreeNode nextParent = null;

int currentChildIndex = 0; // 表示 currentParent 是他的父親的第幾個兒子

//TreeNode lastNode = null; // 最后一個創(chuàng)建出來的TreeNode,用于找到他的父親

for ( int i = 2; i values.length; i++ ){

// 如果null ,表示下一個節(jié)點的父親是當前節(jié)點的父親的第一個孩子節(jié)點

if ( values[i] == null ){

currentParent = (TreeNode)currentParent._childList.get(0);

currentChildIndex = 0;

continue;

}

// 表示一個父節(jié)點的所有孩子輸入完畢

if ( values[i].equals("|") ){

if ( currentChildIndex+1 currentParent._childList.size() ){

currentChildIndex++;

currentParent = (TreeNode)currentParent._parent._childList.get(currentChildIndex);

}

continue;

}

TreeNode child = createChildNode( currentParent, values[i] );

}

}

TreeNode _root = null;

public TreeNode getRoot(){

return _root;

}

/**

// 按寬度優(yōu)先遍歷,打印出parent子樹所有的節(jié)點

private void printSteps( TreeNode parent, int currentDepth ){

for ( int i = 0; i parent._childList.size(); i++ ){

TreeNode child = (TreeNode)parent._childList.get(i);

System.out.println(currentDepth+":"+child);

}

if ( parent._childList.size() != 0 ) System.out.println(""+null);// 為了避免葉子節(jié)點也會打印null

//打印 parent 同層的節(jié)點的孩子

if ( parent._parent != null ){ // 不是root

int i = 1;

while ( i parent._parent._childList.size() ){// parent 的父親還有孩子

TreeNode current = (TreeNode)parent._parent._childList.get(i);

printSteps( current, currentDepth );

i++;

}

}

// 遞歸調(diào)用,打印所有節(jié)點

for ( int i = 0; i parent._childList.size(); i++ ){

TreeNode child = (TreeNode)parent._childList.get(i);

printSteps( child, currentDepth+1 );

}

}

// 按寬度優(yōu)先遍歷,打印出parent子樹所有的節(jié)點

public void printSteps(){

System.out.println(""+_root);

System.out.println(""+null);

printSteps(_root, 1 );

}**/

// 將給定的值做為 parent 的孩子,構(gòu)建節(jié)點

private TreeNode createChildNode( TreeNode parent, Object value ){

TreeNode child = new TreeNode( value , parent );

parent._childList.add( child );

return child;

}

public static void main(String[] args) {

Tree tree = new Tree( new Object[]{ "root", null,

"left", "right", null,

"l1","l2","l3", "|", "r1","r2",null } );

//tree.printSteps();

System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(0) );

System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(1) );

System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(0) )._childList.get(2) );

System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(1) )._childList.get(0) );

System.out.println(""+ ( (TreeNode)tree.getRoot()._childList.get(1) )._childList.get(1) );

}

}

java:二叉樹添加和查詢方法

package arrays.myArray;

public class BinaryTree {

private Node root;

// 添加數(shù)據(jù)

public void add(int data) {

// 遞歸調(diào)用

if (null == root)

root = new Node(data, null, null);

else

addTree(root, data);

}

private void addTree(Node rootNode, int data) {

// 添加到左邊

if (rootNode.data data) {

if (rootNode.left == null)

rootNode.left = new Node(data, null, null);

else

addTree(rootNode.left, data);

} else {

// 添加到右邊

if (rootNode.right == null)

rootNode.right = new Node(data, null, null);

else

addTree(rootNode.right, data);

}

}

// 查詢數(shù)據(jù)

public void show() {

showTree(root);

}

private void showTree(Node node) {

if (node.left != null) {

showTree(node.left);

}

System.out.println(node.data);

if (node.right != null) {

showTree(node.right);

}

}

}

class Node {

int data;

Node left;

Node right;

public Node(int data, Node left, Node right) {

this.data = data;

this.left = left;

this.right = right;

}

}

java 求二叉樹的葉子結(jié)點,下面的代碼不知道哪里出錯了!

我看了一下,知道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編打出5行圣誕樹,求教每一步詳細思想。下面是代碼

按照你的要求加詳細注釋的圣誕樹Java程序如下:(編程思想在注釋中說明)

public?class?ShengDanShu2?{

//這個程序的編程思想是利用對for循環(huán)變量i的控制達到一層循環(huán)代替雙層循環(huán)的目的

public?static?void?main(String[]?args)?{????

int???n=5;???//初始化打印圣誕樹層數(shù)變量n

int???a=0;???//初始化打印前置空格數(shù)變量a

int???b=0;???//初始化打印星號數(shù)變量b

for(int?i=1;i?=n;i++){???//打印n層圣誕樹

if(a!=(n-i)){????//如果前置空格數(shù)不等于n-i

System.out.print("?");?//打印一個空格

a++;????//前置空格數(shù)加一???

i=i-1;????//i變量減一??目的是固定住i變量不變直到a==n-i

}else?if(b!=(2*i-1)){???//如果星號數(shù)不等于2*i-1

System.out.print("*");??//打印一個星號

b++;????//星號數(shù)加一

i=i-1;???//i變量減一??目的是固定住i變量不變直到b==2*i-1

}else?if(a==(n-i)??b==(2*i-1)){//當以上兩個條件都滿足時,換行初始化a和b為0???

System.out.println();??//打印換行?

a=0;???//對新的一行重新初始化前置空格數(shù)變量a

b=0;??//對新的一行重新初始化打印星號數(shù)變量b

//這里沒有控制for循環(huán)的i變量減一,因為這時i變量加一,開始新一行。

}???

}???

}?????

}

運行結(jié)果:

*

***

*****

*******

*********

你有沒有java實現(xiàn)樹的全部代碼

java提供了源代碼的。在你安裝目錄下(jdk下),你會看到bin,jre,等目錄,還有一個是src.zip就是java源代碼了。里面就有你要的東西了。

在線等,計算機高手,java深度搜索樹代碼

//偽代碼。我文本框里直接寫的

void dfs(treeNodeT a)

{

iteretor itr=a.children();

while (itr.hasNext())

{

dfs((treeNode)itr.next());//遞歸調(diào)用

}

}

就是這樣了。每次迭代的查詢子節(jié)點,

如果子節(jié)點還有子節(jié)點就繼續(xù)向下找,一直找到最深。

直到?jīng)]有了就彈棧,看看上一級還有沒有其他的子節(jié)點。

有就遍歷他的第二個子節(jié)點,沒有就彈。

這樣的話就是深度優(yōu)先搜索了。

java中如何建立一個java樹,請詳解?

import?java.awt.*;

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是一個帶滾動條的面板類。

將對象加入到帶滾動條的面板類中,在將已建的數(shù)放入到其中。

就可建立一個系統(tǒng)默認的樹結(jié)構(gòu)。

文章名稱:java中樹的代碼,java b樹代碼
文章位置:http://chinadenli.net/article45/dseicei.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司企業(yè)網(wǎng)站制作企業(yè)建站外貿(mào)建站標簽優(yōu)化關(guān)鍵詞優(yōu)化

廣告

聲明:本網(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)

營銷型網(wǎng)站建設(shè)