首页 > 其他 > 详细

二叉树算法的java实现

时间:2014-03-03 07:23:53      阅读:316      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
package Alg;

public class BSTreeNode {
    private BSTreeNode leftChild = null;
    private BSTreeNode rightChild = null;
    private BSTreeNode parent = null;
    private int value = Integer.MIN_VALUE;

    public static BSTreeNode createNode(int val) {
        return new BSTreeNode(val);
    }
    
    private BSTreeNode(int val) {
        leftChild = null;
        rightChild = null;
        parent = null;
        value = val;
    }
    
    public BSTreeNode search(int val) {
        if (val == value)
            return this;
        if (val > value ) {
            if (this.getRightChild() != null)
                return this.getRightChild().search(val);
        }
        else {
            if (this.getLeftChild() != null)
                return this.getLeftChild().search(val);
        }
        return this;
    }
    
    public boolean isLeaf() {
        if (getLeftChild() == null && getRightChild() == null)
            return true;
        return false;
    }
    
    public boolean isRoot() {
        if (getParent() == null)
            return true;
        return false;
    }
    
    public BSTreeNode getLeftChild() {
        return leftChild;
    }

    public void setLeftChild(BSTreeNode leftChild) {
        this.leftChild = leftChild;
    }

    public BSTreeNode getRightChild() {
        return rightChild;
    }

    public void setRightChild(BSTreeNode rightChild) {
        this.rightChild = rightChild;
    }

    public BSTreeNode getParent() {
        return parent;
    }

    public void setParent(BSTreeNode parent) {
        this.parent = parent;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}
View Code

 

二叉树算法的java实现,布布扣,bubuko.com

二叉树算法的java实现

原文:http://www.cnblogs.com/northhurricane/p/3576413.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!