码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构学习笔记(五) 树的创建和遍历

时间:2017-06-03 13:56:43      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:string   二叉树   插入   pack   first   先序   index   get   nbsp   

创建(先序创建和根据先序和中序进行创建)和遍历(先序遍历、中序遍历、后序遍历、非递归堆栈遍历、层次遍历):

  

package tree;

public class XianCreateTree {
    private static Node header;
    //非递归遍历的堆栈头指针
    private static Node first;
    //层次遍历的尾和头指针(队尾插入,对头出去)
    private static Node rear;
    private static Node front;
    private static int i;
    public static void main(String[] args) {
   //这是先序创建
// char[] tree = new char[]{‘A‘,‘B‘,‘C‘,‘*‘,‘*‘,‘D‘,‘*‘,‘*‘,‘E‘,‘*‘,‘F‘,‘G‘,‘*‘,‘*‘,‘*‘}; // createTree(tree); char[] pre = new char[]{‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘}; char[] mid = new char[]{‘C‘,‘B‘,‘D‘,‘A‘,‘E‘,‘G‘,‘F‘}; header = new Node(pre[0]); preMid(pre,mid,header); System.out.print("先序遍历:"); preTraverse(header); System.out.println(); System.out.print("中序遍历:"); midTraverse(header); System.out.println(); System.out.print("后序遍历:"); afterTraverse(header); System.out.println();
//非递归遍历和层次遍历不能一起执行,因为都有next
// System.out.print("非递归历:"); // noRecursion(header); // System.out.println(); System.out.print("层次遍历:"); queue(header); System.out.println(); } /* * 先序创建一棵二叉树 */ public static Node createTree(char[] tree){ if(tree[i] == ‘*‘){ return null; } Node node = new Node(tree[i]); if(header == null){ header = node; } i++; node.left=createTree(tree); i++; node.right=createTree(tree); return node; } /** * 先 和 中创建一棵二叉树 */ public static Node preMid(char[] pre,char[] mid,Node node){ int index = getIndex(mid,node.data); //说明还有右子树 int len = pre.length-1-index; char[] pre_left; char[] pre_right; char[] mid_left; char[] mid_right; if(index > 0){ if(index == 1){ node.left = new Node(mid[0]); }else{ pre_left = new char[index]; mid_left = new char[index]; //arg0:源数组;arg1:源数组要复制的起始位置;arg2:目的数组;arg3:目的数组放置的起始位置;rg4:复制的长度。 System.arraycopy(pre, 1, pre_left, 0, index); System.arraycopy(mid, 0, mid_left, 0, index); //找到根节点后的第一个节点 //找到根节点后的第一个节点在中序的位置 node.left = preMid(pre_left,mid_left,new Node(pre_left[0])); } } if(len > 0){ if(len == 1){ node.right = new Node(mid[index+1]); }else{ pre_right = new char[len]; mid_right = new char[len]; System.arraycopy(pre, index+1, pre_right, 0, mid.length-index-1); System.arraycopy(mid, index+1, mid_right, 0, mid.length-index-1); node.right = preMid(pre_right,mid_right,new Node(pre_right[0])); } } return node; } public static int getIndex(char[] mid,char c){ for (int i = 0; i < mid.length; i++) { if(mid[i] == c){ return i; } } return -1; } /** * 先序遍历 */ public static Node preTraverse(Node node){ if(node != null){ System.out.print(node.data+" "); node.left = preTraverse(node.left); node.right = preTraverse(node.right); } return node; } /** * 中序遍历 */ public static Node midTraverse(Node node){ if(node != null){ node.left = midTraverse(node.left); System.out.print(node.data+" "); node.right = midTraverse(node.right); } return node; } /** * 后序遍历 */ public static Node afterTraverse(Node node){ if(node != null){ node.left = afterTraverse(node.left); node.right = afterTraverse(node.right); System.out.print(node.data+" "); } return node; } /* * 使用堆栈(堆栈里存放的是节点)非递归遍历(中序) */ public static void noRecursion(Node node){ first = new Node(‘o‘); //右子树 while(node != null || first.next != null){ //左子树 while(node != null){ //System.out.print(first.next.data +" ");先序 node.next = first.next; first.next = node; node = node.left; } if(first.next != null){ System.out.print(first.next.data +" "); node = first.next; first.next = first.next.next; node = node.right; } } } /** * 使用队列层次遍历 */ public static void queue(Node node){ rear = front = node; while(front != null){ if(front.left != null){ rear.next = front.left; rear = rear.next; } if(front.right != null){ rear.next = front.right; rear = rear.next; } System.out.print(front.data+" "); front = front.next; } } }

 

  

树的结点类:

  

package tree;

public class Node {
    public char data;
    public Node left;
    public Node right;
    public Node next;
    public Node(char data) {
        super();
        this.data = data;
    }
    
}

 

数据结构学习笔记(五) 树的创建和遍历

标签:string   二叉树   插入   pack   first   先序   index   get   nbsp   

原文地址:http://www.cnblogs.com/trashman/p/6936941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!