花间一壶酒

导航

二叉树-链式存储-Java实现(未完待续)

package com.wrh.lab.dataStructure.tree;

/**
* the node of the binary tree
*
@author wrh
*
*/

public class BinaryNode<E> {
private E element; //the element
private BinaryNode<E> left; //the left point
private BinaryNode<E> right; //the right point

/**
* build an node
*
@param element
*
@param left
*
@param right
*/

public BinaryNode(E element, BinaryNode<E> left, BinaryNode<E> right) {
this.element = element;
this.left = left;
this.right = right;
}

public E getElement() {
return element;
}

public void setElement(E element) {
this.element = element;
}

public BinaryNode<E> getLeft() {
return left;
}

public void setLeft(BinaryNode<E> left) {
this.left = left;
}

public BinaryNode<E> getRight() {
return right;
}

public void setRight(BinaryNode<E> right) {
this.right = right;
}

public boolean isLeaf() {
boolean isLeaf = false;
if (null == left && null == right) {
isLeaf = true;
} else {
isLeaf = false;
}
return isLeaf;
}


}

 

package com.wrh.lab.dataStructure.tree;

public interface LinkedBinaryTree<E> {

/**
* insert a node to the left child of the binary tree
*/

public void insertToLeft(E element);

/**
* insert a node to the right child of the binary tree
*/

public void insertToRight(E element);

/**
* preorder traversal the binary tree
*/

public void preOrder(BinaryNode<E> root);

/**
* inorder traversal the binary tree
*/

public void inOrder(BinaryNode<E> root);

/**
* postorder traversal the binary tree
*/

public void postOrder(BinaryNode<E> root);
}
package com.wrh.lab.dataStructure.treeImpl;

import com.wrh.lab.dataStructure.stackAndQueue.Stack;
import com.wrh.lab.dataStructure.stackAndQueueImpl.LinkedStackImpl;
import com.wrh.lab.dataStructure.tree.BinaryNode;
import com.wrh.lab.dataStructure.tree.LinkedBinaryTree;

public class LinkedBinaryTreeImpl<E> implements LinkedBinaryTree<E> {
//private BinaryNode<E> left;
//private BinaryNode<E> right;
private BinaryNode<E> root; //the root node of the bianry tree
private E element;


public LinkedBinaryTreeImpl(E element) {
root = new BinaryNode<E>(element, null, null);
}

public BinaryNode<String> create() {
// create the tree by hands
BinaryNode<String> root = new BinaryNode<String>("A", null, null);

// LinkedBinaryTree<String> root = new
// LinkedBinaryTreeImpl<String>("A");
BinaryNode<String> b = new BinaryNode<String>("B", null, null);

BinaryNode<String> c = new BinaryNode<String>("C", null, null);
BinaryNode<String> d = new BinaryNode<String>("D", null, null);
BinaryNode<String> e = new BinaryNode<String>("E", null, null);
root.setLeft(b);
root.setRight(c);
b.setRight(d);
c.setLeft(e);
return root;
}

@Override
public void insertToLeft(E element) {
BinaryNode<E> left = new BinaryNode(element, null, null);
root.setLeft(left);

}

@Override
public void insertToRight(E element) {
BinaryNode<E> right = new BinaryNode(element, null, null);
root.setRight(right);
}

@Override
public void preOrder(BinaryNode<E> root) {
if(null != root) {
System.out.println(root.getElement());
preOrder(root.getLeft());
preOrder(root.getRight());
}


}

@Override
public void inOrder(BinaryNode<E> root) {
if (null != root) {
inOrder(root.getLeft());
System.out.println(root.getElement());
inOrder(root.getRight());
}

}

@Override
public void postOrder(BinaryNode<E> root) {
if (null != root) {
postOrder(root.getLeft());
postOrder(root.getRight());
System.out.println(root.getElement());
}
}

/**
* nonrecursive inorder traverse the tree
*
@param t
*/

public void inOrderTraverse(BinaryNode<E> t) {
Stack<BinaryNode<E>> s = new LinkedStackImpl<BinaryNode<E>>();
BinaryNode<E> p = t;
s.push(p);
while (!s.isEmpty()) {
while (null != p){ //go to the end of the left
s.push(p.getLeft());

p = p.getLeft();
}
s.pop(); //null pointer pop
if (!s.isEmpty()) {

System.out.println(s.getTop().getElement());
//System.out.println(s.pop().getElement());
p = s.pop();

p = p.getRight();
s.push(p);
}

}
t = null;
p = null;
s.clear();
}

}




posted on 2012-02-16 17:29  wrh526  阅读(225)  评论(0编辑  收藏  举报