这篇文章主要知识点是关于Java、二叉树、前序建树、前中后递归、非递归遍历、层序遍历、的内容,如果大家想对相关知识点有系统深入的学习,可以参阅以下电子书
本文实例讲述了Java实现的二叉树常用操作。分享给大家供大家参考,具体如下:
import java.util.ArrayDeque; import java.util.Queue; import java.util.Stack; //二叉树的建树,前中后 递归非递归遍历 层序遍历 //Node节点 class Node { int element; Node left; Node right; public Node() { } public Node(int element) { this.element = element; } } // BinaryTree public class Tree { // creat tree from array public static Node creatTree(int[] data, int i) { if (i >= data.length || data[i] == -1) return null; Node temp = new Node(data[i]); temp.left = creatTree(data, i * 2 + 1); temp.right = creatTree(data, i * 2 + 2); return temp; } // pre前序遍历递归 public static void pre(Node temp) { if (temp == null) return; System.out.print(temp.element + " "); pre(temp.left); pre(temp.right); } // mid中序遍历递归 public static void mid(Node temp) { if (temp == null) return; mid(temp.left); System.out.print(temp.element + " "); mid(temp.right); } // last后序遍历递归 public static void last(Node temp) { if (temp == null) return; last(temp.left); last(temp.right); System.out.print(temp.element + " "); } // pre1前序遍历非递归 public static void pre1(Node temp) { Stack<Node> stack = new Stack<>(); while (temp != null || !stack.isEmpty()) { while (temp != null) { stack.push(temp); System.out.print(temp.element + " "); temp = temp.left; } if (!stack.isEmpty()) { temp = stack.pop().right; } } } // mid1中序遍历非递归 public static void mid1(Node temp) { Stack<Node> stack = new Stack<>(); while (temp != null || !stack.isEmpty()) { while (temp != null) { stack.push(temp); temp = temp.left; } if (!stack.isEmpty()) { temp = stack.pop(); System.out.print(temp.element + " "); temp = temp.right; } } } // last1后序遍历非递归 public static void last1(Node temp) { Stack<Node> stack = new Stack<>(); Stack<Node> stack2 = new Stack<>(); while (temp != null || !stack.isEmpty()) { while (temp != null) { stack.push(temp); stack2.push(temp); temp = temp.right; } if (!stack.isEmpty()) { temp = stack.pop().left; } } while (!stack2.isEmpty()) System.out.print(stack2.pop().element + " "); } // ceng层序遍历 public static void ceng(Node temp) { if (temp == null) return; Queue<Node> queue = new ArrayDeque<>(); queue.offer(temp); while (!queue.isEmpty()) { temp = queue.poll(); System.out.print(temp.element + " "); if (temp.left != null) queue.offer(temp.left); if (temp.right != null) queue.offer(temp.right); } } // Demo public static void main(String[] args) { int[] array = { 1, 2, 3, 4, 5, 6, 7, -1, -1, 10, -1, -1, 13 }; Node tree = creatTree(array, 0); System.out.println("码农之家测试结果:"); pre(tree); System.out.println(); pre1(tree); System.out.println(); mid(tree); System.out.println(); mid1(tree); System.out.println(); last(tree); System.out.println(); last1(tree); System.out.println(); ceng(tree); } }
运行结果:
更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
以上就是本次给大家分享的关于java的全部知识点内容总结,大家还可以在下方相关文章里找到相关文章进一步学习,感谢大家的阅读和支持。
Copyright 2018-2020 www.xz577.com 码农之家
版权投诉 / 书籍推广 / 赞助:520161757@qq.com
java实现按层遍历二叉树
本文实例为大家分享了java实现按层遍历二叉树,按层遍历二叉树可以通过队列来实现。其主要思路如下: 1、先将根节点放入队列中 2、每次都从队列中取出一个结点打印该结点的值 3、若这个结点有子结点,则将它的子结点放入队列尾,知道队列为空。 实现代码如下: import java.util.LinkedList;import java.util.Queue; public class LayerTranverse { //按层遍历二叉树 public static void main(String[] args) { BinaryTree1 biTree1=new BinaryTree1(); int[] data={2,8,7,4,9,3,1,6,5}; biTree1.buildTree1(data); biTree1.layerTranverse(); } }class Node1{ public int data; public Node1 left; public Node1 right; public Node1(int data){ this.data=data; this.left=null; this.right=null; } }class BinaryTree1{ private Node1 root; public BinaryTree1(){ root=null; } //将data数据插入到排序的二叉树中 public void insert1(int data){ Node1 newNode1=new Node1(data); if(root==null){ root=newNode1; }else{ Node……
JavaScript实现二叉树的先序、中序及后序遍历方法详解
本文实例讲述了JavaScript实现二叉树的先序、中序及后序遍历方法。分享给大家供大家参考,具体如下: 之前学数据结构的时候,学了二叉树的先序、中序、后序遍历的方法,并用C语言实现了,下文是用js实现二叉树的3种遍历,并以动画的形式展现出遍历的过程。 整个遍历过程还是采用递归的思想,原理很粗暴也很简单 先序遍历的函数: function preOrder(node){ if(!(node==null)){ divList.push(node); preOrder(node.firstElementChild); preOrder(node.lastElementChild); }} 中序遍历的函数: function inOrder(node) { if (!(node == null)) { inOrder(node.firstElementChild); divList.push(node); inOrder(node.lastElementChild); }} 后序遍历的函数: function postOrder(node) { if (!(node == null)) { postOrder(node.firstElementChild); postOrder(node.lastElementChild); divList.push(node); }} 颜色变化函数: function changeColor(){ var i=0; divList[i].style.backgroundColor ……
JavaScript数据结构与算法之二叉树遍历算法详解【先序、中序、后序】
本文实例讲述了JavaScript数据结构与算法之二叉树遍历算法。分享给大家供大家参考,具体如下: javascript数据结构与算法--二叉树遍历(先序) 先序遍历先访问根节点, 然后以同样方式访问左子树和右子树 代码如下: /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * * *//*用来生成一个节点*/function Node(data, left, right) { this.data = data;//节点存储的数据 this.left = left; this.right = right; this.show = show;}function show() { return this.data;}/*用来生成一个二叉树*/function BST() { this.root = null; this.insert = insert;}/*将数据插入二叉树 (1)设根节点为当前节点。 (2)如果待插入节点保存的数据小于当前节点,则设新的当前节点为原节点的左节点;反 之,执行第4步。 (3)如果当前节点的左节点为null,就将新的节点插入这个位置,退出循环;反之,继续 执……