Java数据结构--- 二叉树(二叉排序树,层次遍历二叉树节点)
来源:互联网 发布:mac c4d r17安装教程 编辑:程序博客网 时间:2024/06/05 14:31
Java 二叉树数据结构
今天就写一下二叉树的常用操作,明天准备写一些图中的常用函数!以后每天都要更新博客,从此要养成,积累的好习惯。俗话说,滴水穿石,对于以前自己没有写博客的习惯,感觉很可惜,做过很多工作,但就是因为没有及时记下来,所以干什么忘什么,基本很多工作干完了一段时间也就忘了,感觉很可惜。既然决定了写博客,那以后每天多少都要写点,也作为自己一天工作的总结。
本文包括二叉树的常用函数,包括二叉排序树的建立,层次遍历二叉树节点,找最大,最小节点,先序,中序,后序遍历等常用操作!
<pre name="code" class="java">package com.robin.binaryTree;public class Node {public int data;public Node left;public Node right;public Node(int data){this.data = data;this.left = null;this.right = null;}}
</pre><pre code_snippet_id="627062" snippet_file_name="blog_20150324_3_1929932" name="code" class="java" style="text-align: left;"><pre name="code" class="java">package com.robin.binaryTree;import java.util.LinkedList;import java.util.Queue;public class BinaryTree {public Node root;//构造函数public BinaryTree(){this.root = null;}public void buildTree(int[] data){for(int i = 0; i < data.length; i++){insertNode(data[i]);}}public void insertNode(int data){Node newNode = new Node(data);if(this.root == null){root = new Node(data);}else{Node current = root;Node parent;while(true){parent = current;if(data < current.data){current = current.left;if(current == null){parent.left = newNode;return;}}else{current = current.right;if(current == null){parent.right = newNode;return;}}}}}public void preOrder(){this.preOrder(this.root);}public void preOrder(Node localRoot){if(localRoot != null){System.out.print(localRoot.data + " ");this.preOrder(localRoot.left);this.preOrder(localRoot.right);}}public void inOrder(){this.inOrder(this.root);}public void inOrder(Node localRoot){if(localRoot != null){this.inOrder(localRoot.left);System.out.print(localRoot.data + " ");this.inOrder(localRoot.right);}}public void postOrder(){this.postOrder(this.root);}public void postOrder(Node localRoot){if(localRoot != null){this.postOrder(localRoot.left);this.postOrder(localRoot.right);System.out.print(localRoot.data + " ");}}public void layoutOrder(){this.layoutOrder(this.root);}public void layoutOrder(Node localRoot){if(localRoot == null){return;}Queue<Node> queue = new LinkedList<Node>();queue.add(localRoot);while(!queue.isEmpty()){Node node = queue.poll();System.out.print(node.data +" ");if(node.left != null)queue.add(node.left);if(node.right != null)queue.add(node.right);}System.out.println();}public void findMin(){this.findMin(this.root);}public void findMin(Node node){Node current = node;if(node == null){return;}while(current.left != null){current = current.left;}System.out.println("the min data is:"+current.data);}public void findMax(){this.findMax(this.root);}public void findMax(Node node){Node current = node;if(node == null){return;}while(current.right != null){current = current.right;}System.out.println("the max data is:"+current.data);}public static void main(String[] args) {BinaryTree bt = new BinaryTree();int[] data = {2,8,7,4,9,3,1,6,7,5};bt.buildTree(data);bt.preOrder();System.out.println();bt.inOrder();System.out.println();bt.postOrder();System.out.println();bt.layoutOrder();bt.findMin();bt.findMax();}}
0 0
- Java数据结构--- 二叉树(二叉排序树,层次遍历二叉树节点)
- 数据结构--二叉树--层次遍历二叉树(顺序遍历)
- 数据结构-层次遍历二叉树
- 二叉树层次遍历java
- 二叉排序树,遍历二叉树
- C++数据结构--按层次遍历二叉树
- 【数据结构】二叉树的层次遍历
- 【数据结构】二叉树的层次遍历2
- 数据结构 — 5.二叉树层次遍历
- 数据结构—二叉树层次遍历
- 初学数据结构---二叉树层次遍历
- 层次遍历二叉树
- 层次遍历二叉树
- 二叉树层次遍历
- 二叉树 层次遍历
- 二叉树层次遍历
- 层次遍历二叉树
- 二叉树层次遍历
- ios判断蓝牙是否开启
- 恶意卡TLE && 测评时判断OJ是否将TLE判成RE或WA
- c和c++的一些训练题(12)(继承问题)(子随父姓)
- C库函数_malloc calloc & realloc
- 分布式缓存--Memcached
- Java数据结构--- 二叉树(二叉排序树,层次遍历二叉树节点)
- volatile的用法
- 关于敏捷开发
- iOS XMPP研究探索:登录与注册
- VC2010工程基础设置——个人习惯
- MongoDB的增删改查
- (3.1.1)HTTP常见错误返回代码
- C#图片与string相互转换
- RFID第一次作业第一题