现在的位置: 首页 > 综合 > 正文

Binary Tree

2014年10月03日 ⁄ 综合 ⁄ 共 7202字 ⁄ 字号 评论关闭
 

  1. /**
  2.  * filename: BinaryTree.java
  3.  * package:
  4.  * author: Nick Ma
  5.  * email: nick.ma85@yahoo.com
  6.  * date: Nov 1, 2008
  7.  * description: this class implements a binary tree data structure.
  8.  */
  9. public
     
    class
     BinaryTree {
  10.         
    // Data Field
  11.         
  12.         
    /** the root of the binary tree */
  13.         
    protected
     TreeNode root;
  14.         
    /**
  15.          * description: default constructor
  16.          */
  17.         
    public
     BinaryTree() {
  18.                 
    // TODO Auto-generated constructor stub
  19.                 
    this
    .root = 
    null
    ;
  20.         }
  21.         
  22.         
    /**
  23.          * description: protected constructor just to build a new binary tree 
  24.          * with only root node
  25.          */
  26.         
    protected
     BinaryTree(TreeNode node) {
  27.                 
    this
    .root = node;
  28.         }
  29.         
    /**
  30.          * description: the constructor to build a new binary tree
  31.          * @param data - the info stored in the root node
  32.          * @param leftTree - the left subtree of the root
  33.          * @param rightTree - the right subtree of the root
  34.          */
  35.         
    public
     BinaryTree(Object data, BinaryTree leftTree, BinaryTree rightTree) {
  36.                 
    // store the data to the root node
  37.                 root.setData(data);
  38.                 
  39.                 
    // set the left subtree of the root
  40.                 
    if
    (leftTree != 
    null
    ) {
  41.                         root.setLeft(leftTree.root);
  42.                 } 
    else
     {
  43.                         root.setLeft(
    null
    );
  44.                 }
  45.                 
  46.                 
    // set the right subtree of the root
  47.                 
    if
    (rightTree != 
    null
    ) {
  48.                         root.setRight(rightTree.root);
  49.                 } 
    else
     {
  50.                         root.setRight(
    null
    );
  51.                 }
  52.         }
  53.         
  54.         
    /**
  55.          * description: return the left subtree of the root
  56.          * @return - the left subtree
  57.          */
  58.         
    public
     BinaryTree getLeftSubTree() {
  59.                 
    if
    (root != 
    null
     && root.getLeft() != 
    null
    ) {
  60.                         
    return
     
    new
     BinaryTree(root.getLeft());
  61.                 } 
    else
     {
  62.                         
    return
     
    null
    ;
  63.                 }
  64.         }
  65.         
  66.         
    /**
  67.          * description: return the right subtree of the root
  68.          * @return - the right subtree
  69.          */
  70.         
    public
     BinaryTree getRightSubTree() {
  71.                 
    if
    (root != 
    null
     && root.getRight() != 
    null
    ) {
  72.                         
    return
     
    new
     BinaryTree(root.getRight());
  73.                 } 
    else
     {
  74.                         
    return
     
    null
    ;
  75.                 }
  76.         }
  77.         
  78.         
    /**
  79.          * description: determine whether the root has no children or not
  80.          * @return - true if the root has no children
  81.          */
  82.         
    public
     
    boolean
     isLeaf() {
  83.                 
    return
     (root != 
    null
     && root.getLeft() == 
    null
     && root.getRight() == 
    null
    );
  84.         }
  85.         
  86.         
    /**
  87.          * description: perform a pre-order traversal
  88.          * @param node - the local root
  89.          */
  90.         
    public
     
    void
     preOrderTraverse(TreeNode node) {       
  91.                 
    if
    (node == 
    null
    ) {
  92.                         
    // do something when node is null
  93.                 } 
    else
     {
  94.                         
    // do something to root node
  95.                         
    // then traverse to its children
  96.                         preOrderTraverse(node.getLeft());
  97.                         preOrderTraverse(node.getRight());
  98.                 }
  99.         }
  100.         
  101.         
    /**
  102.          * description: perform a in-order traversal
  103.          * @param node - the local root
  104.          */
  105.         
    public
     
    void
     inOrderTraverse(TreeNode node) {
  106.                 
    if
    (node == 
    null
    ) {
  107.                         
    // do something when node is null
  108.                 } 
    else
     {
  109.                         inOrderTraverse(node.getLeft());
  110.                         
    // do something to root node
  111.                         
    // then traverse to its children
  112.                         inOrderTraverse(node.getRight());
  113.                 }
  114.         }
  115.         
  116.         
    /**
  117.          * description: perform a post-order traversal
  118.          * @param node - the local root
  119.          */
  120.         
    public
     
    void
     postOrderTraverse(TreeNode node) {
  121.                 
    if
    (node == 
    null
    ) {
  122.                         
    // do something when node is null
  123.                 } 
    else
     {
  124.                         postOrderTraverse(node.getLeft());
  125.                         postOrderTraverse(node.getRight());
  126.                         
    // do something to root node
  127.                         
    // then traverse to its children
  128.                 }
  129.         }
  130.         
  131.         
    /**
  132.          * description: get the depth of this expression tree
  133.          * @param node - the local root node
  134.          * @return - the depth of the local root
  135.          */
  136.         
    protected
     
    int
     getDepth(TreeNode node) {
  137.                 
    if
    (node == 
    null
    ) {
  138.                         
    return
     
    0
    ;
  139.                 } 
    else
     {
  140.                         
    return
     
    1
     + Math.max(getDepth(node.getLeft()), getDepth(node.getRight()));
  141.                 }
  142.         }
  143. }

  1. /**
  2.  * filename: TreeNode.java
  3.  * package: 
  4.  * author: Nick Ma
  5.  * email: nick.ma85@yahoo.com
  6.  * date: Nov 1, 2008
  7.  * description: this class encapsulates a tree node.
  8.  */
  9. public
     
    class
     TreeNode {
  10.         
    // Data Field
  11.         
  12.         
    /** info stored in this root node */
  13.         
    private
     Object data;
  14.         
  15.         
    /** reference to the left child */
  16.         
    private
     TreeNode left;
  17.         
  18.         
    /** reference to the right child */
  19.         
    private
     TreeNode right;
  20.         
    /**
  21.          * description: default constructor
  22.          */
  23.         
    public
     TreeNode() {
  24.                 
    // TODO Auto-generated constructor stub
  25.                 data = 
    null
    ;
  26.                 left = 
    null
    ;
  27.                 right = 
    null
    ;
  28.         }
  29.         
    /**
  30.          * description: constructor to initialize its data with no left and right children
  31.          * @param data - the data to store in this node
  32.          */
  33.         
    public
     TreeNode(Object data) {
  34.                 
    this
    .data = data;
  35.                 
    this
    .left = 
    null
    ;
  36.                 
    this
    .right = 
    null
    ;
  37.         }
  38.         
  39.         
    /**
  40.          * description: constructor with fields
  41.          * @param data - the data to store in this node
  42.          * @param left - the left child
  43.          * @param right - the right child
  44.          */
  45.         
    public
     TreeNode(Object data, TreeNode left, TreeNode right) {
  46.                 
    this
    .data = data;
  47.                 
    this
    .left = left;
  48.                 
    this
    .right = right;
  49.         }
  50.         
    /* (non-Javadoc)
  51.          * @see java.lang.Object#toString()
  52.          */
  53.         
    @Override
  54.         
    public
     String toString() {
  55.                 
    // TODO Auto-generated method stub
  56.                 
    return
     
    this
    .data.toString();
  57.         }
  58.         
    /**
  59.          * @return the data
  60.          */
  61.         
    public
     Object getData() {
  62.                 
    return
     data;
  63.         }
  64.         
    /**
  65.          * @param data the data to set
  66.          */
  67.         
    public
     
    void
     setData(Object data) {
  68.                 
    this
    .data = data;
  69.         }
  70.         
    /**
  71.          * @return the left
  72.          */
  73.         
    public
     TreeNode getLeft() {
  74.                 
    return
     left;
  75.         }
  76.         
    /**
  77.          * @param left the left to set
  78.          */
  79.         
    public
     
    void
     setLeft(TreeNode left) {
  80.                 
    this
    .left = left;
  81.         }
  82.         
    /**
  83.          * @return the right
  84.          */
  85.         
    public
     TreeNode getRight() {
  86.                 
    return
     right;
  87.         }
  88.         
    /**
  89.          * @param right the right to set
  90.          */
  91.         
    public
     
    void
     setRight(TreeNode right) {
  92.                 
    this
    .right = right;
  93.         }
  94. }

抱歉!评论已关闭.