- /**
- * filename: BinaryTree.java
- * package:
- * author: Nick Ma
- * email: nick.ma85@yahoo.com
- * date: Nov 1, 2008
- * description: this class implements a binary tree data structure.
- */
- public class BinaryTree {
- // Data Field
- /** the root of the binary tree */
- protected TreeNode root;
- /**
- * description: default constructor
- */
- public BinaryTree() {
- // TODO Auto-generated constructor stub
- this .root = null ;
- }
- /**
- * description: protected constructor just to build a new binary tree
- * with only root node
- */
- protected BinaryTree(TreeNode node) {
- this .root = node;
- }
- /**
- * description: the constructor to build a new binary tree
- * @param data - the info stored in the root node
- * @param leftTree - the left subtree of the root
- * @param rightTree - the right subtree of the root
- */
- public BinaryTree(Object data, BinaryTree leftTree, BinaryTree rightTree) {
- // store the data to the root node
- root.setData(data);
- // set the left subtree of the root
- if (leftTree != null ) {
- root.setLeft(leftTree.root);
- } else {
- root.setLeft( null );
- }
- // set the right subtree of the root
- if (rightTree != null ) {
- root.setRight(rightTree.root);
- } else {
- root.setRight( null );
- }
- }
- /**
- * description: return the left subtree of the root
- * @return - the left subtree
- */
- public BinaryTree getLeftSubTree() {
- if (root != null && root.getLeft() != null ) {
- return new BinaryTree(root.getLeft());
- } else {
- return null ;
- }
- }
- /**
- * description: return the right subtree of the root
- * @return - the right subtree
- */
- public BinaryTree getRightSubTree() {
- if (root != null && root.getRight() != null ) {
- return new BinaryTree(root.getRight());
- } else {
- return null ;
- }
- }
- /**
- * description: determine whether the root has no children or not
- * @return - true if the root has no children
- */
- public boolean isLeaf() {
- return (root != null && root.getLeft() == null && root.getRight() == null );
- }
- /**
- * description: perform a pre-order traversal
- * @param node - the local root
- */
- public void preOrderTraverse(TreeNode node) {
- if (node == null ) {
- // do something when node is null
- } else {
- // do something to root node
- // then traverse to its children
- preOrderTraverse(node.getLeft());
- preOrderTraverse(node.getRight());
- }
- }
- /**
- * description: perform a in-order traversal
- * @param node - the local root
- */
- public void inOrderTraverse(TreeNode node) {
- if (node == null ) {
- // do something when node is null
- } else {
- inOrderTraverse(node.getLeft());
- // do something to root node
- // then traverse to its children
- inOrderTraverse(node.getRight());
- }
- }
- /**
- * description: perform a post-order traversal
- * @param node - the local root
- */
- public void postOrderTraverse(TreeNode node) {
- if (node == null ) {
- // do something when node is null
- } else {
- postOrderTraverse(node.getLeft());
- postOrderTraverse(node.getRight());
- // do something to root node
- // then traverse to its children
- }
- }
- /**
- * description: get the depth of this expression tree
- * @param node - the local root node
- * @return - the depth of the local root
- */
- protected int getDepth(TreeNode node) {
- if (node == null ) {
- return 0 ;
- } else {
- return 1 + Math.max(getDepth(node.getLeft()), getDepth(node.getRight()));
- }
- }
- }
- /**
- * filename: TreeNode.java
- * package:
- * author: Nick Ma
- * email: nick.ma85@yahoo.com
- * date: Nov 1, 2008
- * description: this class encapsulates a tree node.
- */
- public class TreeNode {
- // Data Field
- /** info stored in this root node */
- private Object data;
- /** reference to the left child */
- private TreeNode left;
- /** reference to the right child */
- private TreeNode right;
- /**
- * description: default constructor
- */
- public TreeNode() {
- // TODO Auto-generated constructor stub
- data = null ;
- left = null ;
- right = null ;
- }
- /**
- * description: constructor to initialize its data with no left and right children
- * @param data - the data to store in this node
- */
- public TreeNode(Object data) {
- this .data = data;
- this .left = null ;
- this .right = null ;
- }
- /**
- * description: constructor with fields
- * @param data - the data to store in this node
- * @param left - the left child
- * @param right - the right child
- */
- public TreeNode(Object data, TreeNode left, TreeNode right) {
- this .data = data;
- this .left = left;
- this .right = right;
- }
- /* (non-Javadoc)
- * @see java.lang.Object#toString()
- */
- @Override
- public String toString() {
- // TODO Auto-generated method stub
- return this .data.toString();
- }
- /**
- * @return the data
- */
- public Object getData() {
- return data;
- }
- /**
- * @param data the data to set
- */
- public void setData(Object data) {
- this .data = data;
- }
- /**
- * @return the left
- */
- public TreeNode getLeft() {
- return left;
- }
- /**
- * @param left the left to set
- */
- public void setLeft(TreeNode left) {
- this .left = left;
- }
- /**
- * @return the right
- */
- public TreeNode getRight() {
- return right;
- }
- /**
- * @param right the right to set
- */
- public void setRight(TreeNode right) {
- this .right = right;
- }
- }