码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树模型代码

时间:2019-06-04 09:16:07      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:vat   highlight   ==   void   树模型   sys   ddn   des   nod   

package test;


public class tree2 {
private int data;
private tree2 lChild;
private tree2 rChild;

public int getNodes(){
return (this.lChild == null ? 0 :
this.lChild.getNodes( ) )
+ ( this.rChild == null
? 0 : this.rChild.getNodes( ) ) + 1;
}
public int sum(){
return ( this.lChild == null ? 0 :
this.lChild.sum( ) )
+ ( this.rChild == null ? 0 :
this.rChild.sum( ) ) + this.data;
}

public void fTraverse( ) {
System.out.print( this.data+" " );
if( this.lChild != null ) {
this.lChild.fTraverse( );
}
if( this.rChild != null ) {
this.rChild.fTraverse( );
}
}
public void mTraverse( ) {
if( this.lChild != null ) {
this.lChild.mTraverse( );
}
System.out.print( this.data+" " );
if( this.rChild != null ) {
this.rChild.mTraverse( );
}
}
public void lTraverse( ) {
if( this.lChild != null ) {
this.lChild.lTraverse( );
}
if( this.rChild != null ) {
this.rChild.lTraverse( );
}
System.out.print( this.data+" " );
}

public void addNode(tree2 node ) {
if ( node.data< this.data ) {
if ( this.lChild == null ) {
this.lChild = node;
} else {
this.lChild.addNode( node );
}
}else if(node.data> this.data){
if ( this.rChild == null ) {
this.rChild = node;
} else {
this.rChild.addNode( node );
}
}

}
public tree2(int data) {
this.data = data;
}

public int getData() {
return data;
}

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

public tree2 getlChild() {
return lChild;
}

public void setlChild(tree2 lChild) {
this.lChild = lChild;
}

public tree2 getrChild() {
return rChild;
}

public void setrChild(tree2 rChild) {
this.rChild = rChild;
}
}

  

二叉树模型代码

标签:vat   highlight   ==   void   树模型   sys   ddn   des   nod   

原文地址:https://www.cnblogs.com/qinyios/p/10971422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!