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

      

  • 相关阅读:
    D. Beautiful Array
    C. Magic Ship Educational Codeforces Round 60 (Rated for Div. 2)
    CCPC-Wannafly Winter Camp Day3 小清新数论(莫比乌斯反演反演加杜教筛)
    杜教筛
    Algorithms Weekly 3
    Algorithms Weekly 2
    Algorithms Weekly 1
    KNN算法实现数字识别
    2019总结
    2019 Google Kickstart Round H
  • 原文地址:https://www.cnblogs.com/qinyios/p/10971422.html
Copyright © 2011-2022 走看看