二叉树模型代码

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;
}
}

  

posted @ 2019-06-04 08:46  黑魔法os  阅读(493)  评论(0编辑  收藏  举报