您的位置:首页 > 其它

二叉树基本操作

2015-06-22 18:32 381 查看
package java_study.JianZhiOffer;

/**
* Created by ethan on 2015/6/21.
*/
public class TreeNode {

char value;
TreeNode lchild;
TreeNode rchild;

public TreeNode() {
this.value = 0;
this.lchild = null;
this.rchild = null;
}
public TreeNode(char value){
this.value = value;
this.lchild = null;
this.rchild = null;
}
public TreeNode(char value, TreeNode lchild, TreeNode rchild){
this.value = value;
this.lchild = lchild;
this.rchild = rchild;
}

public char getValue() {
return value;
}

public void setValue(char value) {
this.value = value;
}

public TreeNode getLchild() {
return lchild;
}

public void setLchild(TreeNode lchild) {
this.lchild = lchild;
}

public TreeNode getRchild() {
return rchild;
}

public void setRchild(TreeNode rchild) {
this.rchild = rchild;
}
}

package java_study.JianZhiOffer;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
* Created by ethan on 2015/6/21.
*/
public class TreeUtil {
// 接收前序和中序创建二叉树
public static TreeNode buildTree(String pre_str, int pre_start, int pre_end, String in_str, int in_start, int in_end){
if (pre_str==null || in_str==null) return null;
if (pre_start>pre_end || in_start>in_end) return null;
TreeNode root = new TreeNode(pre_str.charAt(pre_start));
int index_mid = in_start;
for (; index_mid<in_end; index_mid++)
if (in_str.charAt(index_mid) == pre_str.charAt(pre_start)) break;
if (index_mid > in_start)
root.setLchild(buildTree(pre_str, pre_start+1, pre_start+index_mid-in_start, in_str, in_start, index_mid-1));
if (index_mid < in_end)
root.setRchild(buildTree(pre_str, pre_start+index_mid-in_start+1, pre_end, in_str, index_mid+1, in_end));
return  root;
}

// 前序递归遍历
public static void preOrder(TreeNode root){
if (root != null){
System.out.print(root.getValue() + " ");
preOrder(root.getLchild());
preOrder(root.getRchild());
}
}
// 中序递归遍历
public static void inOrder(TreeNode root){
if (root != null ){
inOrder(root.getLchild());
System.out.print(root.getValue() + " ");
inOrder(root.getRchild());
}
}
// 后序递归遍历
public static void postOrder(TreeNode root){
if (root!=null){
postOrder(root.getLchild());
postOrder(root.getRchild());
System.out.print(root.getValue() + " ");
}
}
// 前序非递归遍历, 全压栈
public static void preOrderNoRecursive_1(TreeNode root){
if (root == null) return;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()){
TreeNode tmp = stack.pop();
System.out.print(tmp.getValue() + " ");
if (tmp.getRchild()!=null){
stack.push(tmp.getRchild());
}
if (tmp.getLchild()!=null){
stack.push(tmp.getLchild());
}
}
System.out.println();
}
// 前序非递归遍历, 全压栈
public static void preOrderNoRecursive_2(TreeNode root){
if (root==null) return;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode tmp = root;
while (tmp!=null || !stack.isEmpty()){
while (tmp!=null){
System.out.print(tmp.value+" ");
stack.push(tmp);
tmp=tmp.getLchild();
}
if (!stack.isEmpty()){
tmp = stack.pop().getRchild();
}
}
System.out.println();
}
// 前序非递归遍历
public static void preOrderNoRecursive(TreeNode root){
if (root==null) return;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode tmp = root;
while (tmp!=null){
System.out.print(tmp.value+" ");
stack.push(tmp);
tmp=tmp.getLchild();
}
while (!stack.isEmpty()){
TreeNode top = stack.pop();
TreeNode tmp_1 = top.getRchild();
while (tmp_1!=null){
System.out.print(tmp_1.value+" ");
stack.push(tmp_1);
tmp_1=tmp_1.getLchild();
}
}
System.out.println();
}
// 中序非递归遍历
public static void inOrderNoRecursive_1(TreeNode root){
if (root==null) return;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode tmp = root;
while (tmp!=null || !stack.isEmpty()){
while (tmp!=null){
stack.push(tmp);
tmp = tmp.getLchild();
}
if (!stack.isEmpty()){
TreeNode myNode = stack.pop();
System.out.print(myNode.getValue()+" ");
tmp = myNode.getRchild();
}
}
System.out.println();
}
// 中序非递归遍历
public static void inOrderNoRecursive(TreeNode root){
if (root==null) return;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode tmp = root;
while (tmp!=null){
stack.push(tmp);
tmp = tmp.getLchild();
}
while (!stack.isEmpty()){
TreeNode top = stack.pop();
System.out.print(top.value + " ");
TreeNode tmp_1 = top.getRchild();
while (tmp_1!=null){
stack.push(tmp_1);
tmp_1=tmp_1.getLchild();
}
}
System.out.println();
}
// 后序非递归遍历
public static void postOrderNoRecursive(TreeNode root){
if (root==null) return;
TreeNode tmp = root;
TreeNode preVisited = null;
Stack<TreeNode> stack = new Stack<TreeNode>();
while (tmp!=null || !stack.isEmpty()){
while (tmp!=null){
stack.push(tmp);
tmp = tmp.getLchild();
}
tmp = stack.peek();
if (tmp.getRchild()==null || tmp.getRchild()==preVisited){
System.out.print(tmp.getValue()+ " ");
preVisited = tmp;
stack.pop();
tmp=null;
}else{
tmp = tmp.getRchild();
}
}
System.out.println();
}

// 层次遍历 利用队列
public static void levelTraverse(TreeNode root){
if (root == null) return;
Queue<TreeNode> stack = new LinkedList<TreeNode>();
stack.add(root);
while (!stack.isEmpty()){
TreeNode cur = stack.remove();
System.out.print(cur.getValue() + " ");
if (cur.getLchild()!=null)
stack.add(cur.getLchild());
if (cur.getRchild()!=null){
stack.add(cur.getRchild());
}
}
System.out.println();
}

// 二叉树的高度
public static int getDepth(TreeNode root){
if (root==null) return 0;
int depth_l = getDepth(root.getLchild());
int depth_r = getDepth(root.getRchild());
return (depth_l > depth_r ? depth_l : depth_r)+1;
}

// 二叉树的结点的个数
public static int countNodes(TreeNode root){
if (root==null) return 0;
return 1+countNodes(root.getLchild()) + countNodes(root.getRchild());
}
}

package java_study.JianZhiOffer;

import org.junit.Test;

/**
* Created by ethan on 2015/6/21.
*/
public class TestTreeUtil {
String pre_str = "abdhecfg";
String in_str = "hdbeafcg";
public TreeNode root = TreeUtil.buildTree(pre_str, 0, pre_str.length()-1, in_str, 0, in_str.length()-1);
// 手动初始化
//    public TreeNode init(){
//        TreeNode node0 = new TreeNode('a');
//        TreeNode node1 = new TreeNode('b');
//        TreeNode node2 = new TreeNode('c');
//        TreeNode node3 = new TreeNode('d');
//        TreeNode node4 = new TreeNode('e');
//        TreeNode node5 = new TreeNode('f');
//        TreeNode node6 = new TreeNode('g');
//        TreeNode node7 = new TreeNode('h');
//        node0.setLchild(node1);
//        node0.setRchild(node2);
//        node1.setLchild(node3);
//        node1.setRchild(node4);
//        node2.setLchild(node5);
//        node2.setRchild(node6);
//        node3.setLchild(node7);
//        return node0;
//    }

@Test
public void test3(){
System.out.println(TreeUtil.getDepth(root));
System.out.println(TreeUtil.countNodes(root));
}

@Test
public void test2(){
TreeUtil.preOrderNoRecursive(root);
TreeUtil.preOrderNoRecursive_1(root);
TreeUtil.preOrderNoRecursive_2(root);
TreeUtil.inOrderNoRecursive(root);
TreeUtil.inOrderNoRecursive_1(root);
TreeUtil.postOrderNoRecursive(root);
TreeUtil.levelTraverse(root);
}

@Test
public void test1(){
String pre_str = "abdhecfg";
String in_str = "hdbeafcg";
TreeNode root = TreeUtil.buildTree(pre_str, 0, pre_str.length()-1, in_str, 0, in_str.length()-1);
TreeUtil.postOrder(root);
}

@Test
public void test(){
TreeUtil.preOrder(root);
System.out.println();
TreeUtil.inOrder(root);
System.out.println();
TreeUtil.postOrder(root);
System.out.println();
}
}
a1b4
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息