二叉树-链式存储-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();
}

}




转载于:https://www.cnblogs.com/wrh526/archive/2012/02/16/2354638.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值