zoukankan      html  css  js  c++  java
  • java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历

    import java.util.LinkedList;
    import java.util.Scanner;
    import java.util.Stack;
    
    //structure of binary tree
    class BiTree {
    	BiTree lchild;
    	BiTree rchild;
    	String data;
    }
    
    public class BiTreeTest {
    	static Scanner scanner = new Scanner(System.in);
    
    	// test case: a b c # # d e # g # # f # # #
    	static BiTree createBiTree(BiTree root) {
    		String data = scanner.next();
    		if (data.equals("#")) {
    			return null;
    		} else {
    			root = new BiTree();
    			root.data = data;
    			root.lchild = createBiTree(root.lchild);
    			root.rchild = createBiTree(root.rchild);
    			return root;
    		}
    	}
    
    	// preOrder recursive traverse
    	static void preOrderRecur(BiTree root) {
    		if (root != null) {
    			System.out.print(root.data + " ");
    			preOrderRecur(root.lchild);
    			preOrderRecur(root.rchild);
    		}
    	}
    
    	// inOrder recursive traverse
    	static void inOrderRecur(BiTree root) {
    		if (root != null) {
    			inOrderRecur(root.lchild);
    			System.out.print(root.data + " ");
    			inOrderRecur(root.rchild);
    		}
    	}
    
    	// preOrder in non-recursive
    	static void preOrder(BiTree root) {
    		Stack<BiTree> stack = new Stack<BiTree>();
    		BiTree cur;
    		stack.push(root);
    		while (!stack.empty()) {
    			while ((cur = stack.peek()) != null) {
    				System.out.print(cur.data + " ");
    				stack.push(cur.lchild);
    			}
    			cur = stack.pop();
    			if (!stack.empty() && (cur = stack.pop()) != null) {
    				stack.push(cur.rchild);
    			}
    		}
    	}
    
    	// inOrder in non-recursive
    	static void inOrder(BiTree root) {
    		Stack<BiTree> stack = new Stack<BiTree>();
    		BiTree cur;
    		stack.push(root);
    		while (!stack.empty()) {
    			while ((cur = stack.peek()) != null) {
    				stack.push(cur.lchild);
    			}
    			stack.pop();
    			if (!stack.empty() && (cur = stack.pop()) != null) {
    				System.out.print(cur.data + " ");
    				stack.push(cur.rchild);
    			}
    		}
    	}
    
    	// level traverse,use LinkedList instead of queue data structure
    	static void levelTraverse(BiTree root) {
    		LinkedList<BiTree> list = new LinkedList<BiTree>();
    		BiTree cur;
    		list.add(root);
    		while (list.size() != 0) {
    			cur = list.removeFirst();
    			if (cur != null) {
    				System.out.print(cur.data + " ");
    			}
    			if (cur.lchild != null) {
    				list.add(cur.lchild);
    			}
    			if (cur.rchild != null) {
    				list.add(cur.rchild);
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		BiTree root = null;
    		root = createBiTree(root);
    		// preOrderRecur(root);
    		// inOrderRecur(root);
    		// inOrder(root);
    		levelTraverse(root);
    	}
    }


  • 相关阅读:
    Ubuntu18.04安装RTX2080Ti+NVIDIA驱动+CUDA
    G++ 编译多个源文件
    线段树【递归版本】
    Linux 安装 python 指定版本--编译源码方式
    正则表达式高级替换
    【转载】Git忽略规则和.gitignore规则不生效的解决办法
    一次“惊险”的系统修复过程
    YOLO模型对图片中车辆的识别比对
    YOLOv3模型识别车位图片的测试报告(节选)
    在windows下用python调用darknet的yolo接口
  • 原文地址:https://www.cnblogs.com/pangblog/p/3397948.html
Copyright © 2011-2022 走看看