java語言實現的一個簡單的二叉樹實例

本人今天上午剛剛練習二叉樹編寫的程序。

首先是一個javabean:


package com.wisedu.test1;

public class TBean {
private int data;

public int getData() {
return data;
}

public void setData(int data) {
this.data = data;
}
}


然後是節點:

package com.wisedu.test1;

public class BinTNode {

private TBean tBean;
private BinTNode childLeft;
private BinTNode childRight;
private BinTNode parent;

BinTNode(){
tBean = new TBean();
}

public BinTNode getParent() {
return parent;
}
public void setParent(BinTNode parent) {
this.parent = parent;
}
public TBean getTBean() {
return tBean;
}
public void setTBean(TBean bean) {
tBean = bean;
}

public BinTNode getChildLeft() {
return childLeft;
}
public void setChildLeft(BinTNode childLeft) {
this.childLeft = childLeft;
}

public BinTNode getChildRight() {
return childRight;
}
public void setChildRight(BinTNode childRight) {
this.childRight = childRight;
}

}

最後是二叉樹:
package com.wisedu.test1;

public class BinTree {

private BinTNode rootNode;

public BinTNode getRootNode() {
return rootNode;
}

public void setRootNode(BinTNode rootNode) {
this.rootNode = rootNode;
}

BinTree(){}

BinTree(int data){
TBean tBean = new TBean();
tBean.setData(data);
rootNode = new BinTNode();
rootNode.setTBean(tBean);
}

/**
* 將樹置空
*/
public static BinTree destroy(BinTree binTree){
return null;
}

/**
* 插入數據
*/
public void insert(BinTNode rootNode,BinTNode binTNode){
//如果根節點爲空,就直接賦值給根節點
if(rootNode == null){
rootNode = binTNode;
}else{
//如果比雙親節點數值小的話,就作爲左孩子節點,要是大的話,就作爲右孩子節點。
if(binTNode.getTBean().getData()<rootNode.getTBean().getData()){
if(rootNode.getChildLeft() == null){
rootNode.setChildLeft(binTNode);
binTNode.setParent(rootNode);
}else{
insert(rootNode.getChildLeft(),binTNode);
}
}else {
if(binTNode.getTBean().getData()>rootNode.getTBean().getData()){
if(rootNode.getChildRight() == null){
rootNode.setChildRight(binTNode);
binTNode.setParent(rootNode);
}else{
insert(rootNode.getChildRight(),binTNode);
}
}else{
System.out.println("在該二叉樹中已經有了該對象");
}
}
}
}

/**
* 將樹的所有節點清空
*/
public void clearTree(BinTNode rootNode){
if(rootNode != null){
if(rootNode.getTBean() == null){
if(rootNode.getChildLeft() != null){
clearTree(rootNode.getChildLeft());
}
if(rootNode.getChildRight() != null){
clearTree(rootNode.getChildRight());
}
}else{
rootNode.setTBean(null);
if(rootNode.getChildLeft() != null){
clearTree(rootNode.getChildLeft());
}
if(rootNode.getChildRight() != null){
clearTree(rootNode.getChildRight());
}
}
}else{
System.out.println("該樹已經是空了啊!");
}
}

/**
* 前序遍歷
* @param rooBinTNode
* @return
*/
public String preOrderTraverse(BinTNode rootNode){

StringBuffer strBuffer = new StringBuffer();

if(rootNode == null){
return strBuffer.toString();
}else{
if(rootNode.getTBean() != null){
strBuffer.append(rootNode.getTBean().getData()+",");
}else{
strBuffer.append("null,");
}
}
strBuffer.append(preOrderTraverse(rootNode.getChildLeft()));
strBuffer.append(preOrderTraverse(rootNode.getChildRight()));

return strBuffer.toString();
}


public int treeDepth(BinTNode rootNode){

int depth = 0;
int leftDepth = 0;
int rightDepth = 0;

if(rootNode == null){
return 0;
}else{
leftDepth=treeDepth(rootNode.getChildLeft())+1;
rightDepth=treeDepth(rootNode.getChildRight())+1;
}
depth = (leftDepth>rightDepth?leftDepth:rightDepth);
return depth;
}

//測試
public static void main(String[] args){
//利用構造函數初始化一個根節點
BinTree tree = new BinTree(100);

//創建兩個新節點
BinTNode node1 = new BinTNode();
TBean tBean1 = new TBean();
tBean1.setData(22);
node1.setTBean(tBean1);

BinTNode node2 = new BinTNode();
TBean tBean2 = new TBean();
tBean2.setData(200);
node2.setTBean(tBean2);

BinTNode node3 = new BinTNode();
TBean tBean3 = new TBean();
tBean3.setData(50);
node3.setTBean(tBean3);

//將這兩個節點都插入到根爲tree中
tree.insert(tree.getRootNode(),node1);
tree.insert(tree.getRootNode(), node2);
tree.insert(tree.getRootNode(), node3);

//獲取該樹的根節點
BinTNode rootNode = tree.getRootNode();

//置空
// tree.clearTree(rootNode);


//打印tree中所有節點中的值

String re = tree.preOrderTraverse(rootNode);
int depth = tree.treeDepth(rootNode);
System.out.println(depth);
System.out.println(re);
}

}


程序剛剛運行成功的。大家放心參考…
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章