【编程】二叉树问题
来源:互联网 发布:vscode颜色主题 编辑:程序博客网 时间:2024/06/05 20:55
1。由先序和中序建立二叉树。
2。由中序和后序建立二叉树。
3。二叉树的非递归先序,中序和后序遍历
import java.util.Scanner;import java.util.Stack;class Node { Node left; Node right; char value; Node() {} Node(char value) { left = null; right = null; this.value = value; }}public class BuidTree { /** * 由先序和中序建立二叉树 * @param A 先序遍历 * @param B 中序遍历 * @param L1 先序遍历下标起点 * @param H1 先序遍历下标终点 * @param L2 中序遍历下标起点 * @param R2 中序遍历下标终点 */ public static Node PreInCreate(char A[], char B[], int L1, int H1, int L2, int H2) { Node root = new Node(); root.value = A[L1]; int i; for(i = L2; B[i] != root.value; i++); int llen = i - L2; int rlen = H2 - i; if(llen > 0) { root.left = PreInCreate(A, B, L1+1, L1+llen, L2, L2+llen-1); } else { root.left = null; } if(rlen > 0) { root.right = PreInCreate(A, B, H1-rlen+1, H1, H2-rlen+1, H2); } else { root.right = null; } return root; } /** * 由中序和后序建立二叉树 * @param A 中序遍历 * @param B 后序遍历 * @param L1 中序遍历起点下标 * @param H1 中序遍历终点下标 * @param L2 后序遍历起点下标 * @param H2 后序遍历终点下标 * @return */ public static Node InPostCreate(char A[], char B[], int L1, int H1, int L2, int H2) { Node root = new Node(B[H2]); int i; for(i = L1; A[i] != root.value && i <= H1; i++); int llen = i - L1; int rlen = H1 - i; if(llen > 0) { root.left = InPostCreate(A, B, L1, L1+llen-1, L2, L2+llen-1); } else { root.left = null; } if(rlen > 0) { root.right = InPostCreate(A, B, i+1, H1, H2-rlen, H2-1); } else { root.right = null; } return root; } //先序遍历 public static void preOrder(Node head) { if(head == null) return; System.out.print(head.value + " "); preOrder(head.left); preOrder(head.right); } //非递归先序遍历 public static void preOrderUnRecurion(Node head) { System.out.println("非递归先序遍历:"); if(head != null) { Stack<Node> stack = new Stack<>(); stack.add(head); while(!stack.isEmpty()) { head = stack.pop(); System.out.print(head.value + " "); if(head.right != null) { stack.add(head.right); } if(head.left != null) { stack.add(head.left); } } } } //中序遍历 public static void inOrder(Node head) { if(head == null) return; inOrder(head.left); System.out.print(head.value + " "); inOrder(head.right); } public static void inOrderUnRecurion(Node head) { System.out.println("非递归中序遍历:"); if(head != null) { Stack<Node> stack = new Stack<>(); while(!stack.isEmpty() || head != null) { if(head != null) { stack.add(head); head = head.left; } else { head = stack.pop(); System.out.print(head.value + " "); head = head.right; } } } System.out.println(); } //后序遍历 public static void posOrder(Node head) { if(head==null) { return; } posOrder(head.left); posOrder(head.right); System.out.print(head.value + " "); } public static void posOrderUnRecurion(Node head) { System.out.println("非递归后序遍历: "); if(head != null) { Stack<Node> s1 = new Stack<>(); Stack<Node> s2 = new Stack<>(); s1.push(head); while(!s1.isEmpty()) { head = s1.pop(); s2.push(head); if(head.left != null) { s1.push(head.left); } if(head.right != null) { s1.push(head.right); } } while(!s2.isEmpty()) { System.out.print(s2.pop().value + " "); } } System.out.println(); } public static void testPreInCreate() { String A, B; Scanner scanner = new Scanner(System.in); A = scanner.nextLine(); B = scanner.nextLine(); Node root = PreInCreate(A.toCharArray(), B.toCharArray(), 0, A.length()-1, 0, B.length()-1); System.out.println("先序遍历:"); preOrder(root); System.out.println(); System.out.println("中序遍历:"); inOrder(root); System.out.println(); System.out.println("后序遍历:"); posOrder(root); } public static void testInPostCreate() { String A, B; Scanner scanner = new Scanner(System.in); A = scanner.nextLine(); B = scanner.nextLine(); Node root = InPostCreate(A.toCharArray(), B.toCharArray(), 0, A.length()-1, 0, B.length()-1); System.out.println(A + " " + B); System.out.println("先序遍历:"); //preOrder(root); preOrderUnRecurion(root); System.out.println(); System.out.println("中序遍历:"); //inOrder(root); inOrderUnRecurion(root); System.out.println(); System.out.println("后序遍历:"); //posOrder(root); posOrderUnRecurion(root); } public static void main(String[] args) { //由先序和中序建立二叉树 //testPreInCreate(); System.out.println(); //由中序和后序建立二叉树 testInPostCreate(); }}
阅读全文
0 0
- 【编程】二叉树问题
- 数据结构二叉树的编程基本问题
- 编程之美-重建二叉树扩展问题1 2
- 编程之美3.8 3.9 3.10二叉树问题实现
- 编程之美 3.9 重建二叉树 扩展问题
- 二叉树相关问题编程实现(1)
- 编程之美系列之二叉树1—二叉树中的距离问题
- 编程之美系列之二叉树2—二叉树的距离问题(续)
- 二叉树编程模型
- 二叉树有关编程
- 二叉树编程实践
- 二叉树遍历问题
- 数据结构-二叉树 问题
- 二叉树遍历问题
- 二叉树问题
- 二叉树的问题
- 二叉树问题
- 二叉树相关问题
- 指针例证
- 代码 -- 堆排序 解决大数组找前K大。
- 不念过去不畏将来
- 基于腾讯云主机CentOS搭建邮件收发服务器
- crypt.3. 计算素数表: 埃拉托色尼筛法
- 【编程】二叉树问题
- Docs » Learning the ArduPilot Codebase » Threading
- 获取android应用的包名和主类目
- canvas fabric.js api梳理
- redis集群部署rvm安装问题
- The Google File System : part4 MASTER OPERATION
- 安卓ble蓝牙快速开发
- returned a response status of 403 OR 409
- W5500上传温湿度到oneNET平台