基于上次初学java链表,简单的做了一个二叉树,这次重新做了一个较为完善的二叉树,包括定义树结点,实现二叉树的三种遍历,统计孩子结点,树高等操作,并将他们封装在LinkedBinaryTree类当中,只用创建对象,调用其相关方法即可实现各项操作。
测试二叉树
package 数据结构_二叉树;
public class 实现二叉树的建立及遍历 {
public static void main(String[] args) {
//创建树结点
LinkedBinaryTree node1 = new LinkedBinaryTree('a');
LinkedBinaryTree node2 = new LinkedBinaryTree(6);
LinkedBinaryTree node3 = new LinkedBinaryTree('c');
LinkedBinaryTree node4 = new LinkedBinaryTree(4);
LinkedBinaryTree node5 = new LinkedBinaryTree(5);
LinkedBinaryTree node6 = new LinkedBinaryTree('-');
LinkedBinaryTree node7 = new LinkedBinaryTree(7);
//创建结点关系
node1.setRootleft(node2);
node1.setRootright(node3);
node2.setRootleft(node4);
node2.setRootright(node5);
node5.setRootleft(node6);
node5.setRootright(node7);
//调用方法完成相关操作
node1.order();
}
}
LinkedBinaryTree类
package 数据结构_二叉树;
public class LinkedBinaryTree<T> {
protected BinaryTreeNode<T> root;
protected int modCount;
public LinkedBinaryTree(T root) {
this.root = new BinaryTreeNode<T>(root);
}
public BinaryTreeNode<T> getRootNode() {
return root;
}
public void setRootleft(LinkedBinaryTree left){
root.setLeft(left.getRootNode());
}
public void setRootright(LinkedBinaryTree right){
root.setRight(right.getRootNode());
}
public void order(){
TraversalTree tra = new TraversalTree();
System.out.print("\n前序:");
tra.preorder(root);
System.out.print("\n中序:");
tra.infixorder(root);
System.out.print("\n后序:");
tra.postorder(root);
System.out.print("\n孩子:");
System.out.print(tra.children(root));
System.out.print("\n树的高度:"+tra.getTemp(root));
}
}
TraversalTree 类
package 数据结构_二叉树;
public class TraversalTree {
private int hi,temp;
public TraversalTree() {
hi = 0;
temp = 0;
}
/**
* 前序遍历
* @param root a root of preorder
*/
public void preorder(BinaryTreeNode root){
if(root.getLeft() == null && root.getRight() == null){
System.out.print(root.getElement()+" ");
return;
}
else{
System.out.print(root.getElement()+" ");
if(root.getLeft() != null)
preorder(root.getLeft());
if(root.getRight() != null)
preorder(root.getRight());
}
}
/**
* 中序遍历
* @param root root a root of infixorder
*/
public void infixorder(BinaryTreeNode root){
if(root.getLeft() == null && root.getRight() == null){
System.out.print(root.getElement()+" ");
return;
}
else{
if(root.getLeft() != null)
infixorder(root.getLeft());
System.out.print(root.getElement()+" ");
if(root.getRight() != null)
infixorder(root.getRight());
}
}
/**
* 后序遍历
* @param root root a root of postorder
*/
public void postorder(BinaryTreeNode root){
if(root.getLeft() == null && root.getRight() == null){
System.out.print(root.getElement()+" ");
return;
}
else{
if(root.getLeft() != null)
postorder(root.getLeft());
if(root.getRight() != null)
postorder(root.getRight());
System.out.print(root.getElement()+" ");
}
}
/**
* 统计该结点的子孙结点
* @param root the ancestor of tree
* @return
*/
public int children(BinaryTreeNode root){
int child = 0;
if(root.getLeft() != null)
child += 1 + children(root.getLeft());
if(root.getRight() != null)
child += 1 + children(root.getRight());
return child;
}
/**
* 统计该树的高度
* @param root the ancestor of tree
* @return
*/
public int getTemp(BinaryTreeNode root) {
high(root);
return temp;
}
private void high(BinaryTreeNode root){
if(root.getLeft() == null && root.getRight() == null){
if(hi > temp)
temp = hi;
return;
}
else{
hi++;
if(root.getLeft() != null)
high(root.getLeft());
if(root.getRight() != null)
high(root.getRight());
}
}
}
BinaryTreeNode类
package 数据结构_二叉树;
public class BinaryTreeNode<T> {
// 树结点,及左右结点定义
protected T element;
protected BinaryTreeNode<T> left,right;
// 结点构造方法
public BinaryTreeNode(T obj){
element = obj;
left = null;
right = null;
}
public BinaryTreeNode<T> getLeft() {
return left;
}
public void setLeft(BinaryTreeNode<T> left) {
this.left = left;
}
public BinaryTreeNode<T> getRight() {
return right;
}
public void setRight(BinaryTreeNode<T> right) {
this.right = right;
}
public T getElement() {
return element;
}
}