zoukankan      html  css  js  c++  java
  • 二叉树模型代码

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

      

  • 相关阅读:
    ubuntu apache2配置
    Oauth2协议解读(一)
    OAuth2协议解读(二)
    flask 环境搭建
    BeautifulSoup初识
    Oauth2解读
    5路由控制
    ​1Web应用
    3Web框架
    2Http协议
  • 原文地址:https://www.cnblogs.com/qinyios/p/10971422.html
Copyright © 2011-2022 走看看