java数据结构编写二叉树_java 数据结构与算法 BinaryTree二叉树编写
import java.util.Stack;
public class BinaryTree {
TreeNode root = null;
public BinaryTree() {
this.root = new TreeNode("A");
}
/*
* 构建二叉树
* A
* B C
* D E F
*/
public void createTree() {
TreeNode nodeb = new TreeNode("B");
TreeNode nodec = new TreeNode("C");
TreeNode noded = new TreeNode("D");
TreeNode nodee = new TreeNode("E");
TreeNode nodef = new TreeNode("F");
root.leftchild = nodeb;
root.rightchild = nodec;
nodeb.leftchild = noded;
nodeb.rightchild = nodee;
nodec.rightchild = nodef;
}
/*
* 构建另外一个二叉树来测试代码是否正确
* A
* B C
* D E F G
* H I J K
*/
public void createTestTree() {
TreeNode nodeb = new TreeNode("B");
TreeNode nodec = new TreeNode("C");
TreeNode noded = new TreeNode("D");
TreeNode nodee = new TreeNode("E");
TreeNode nodef = new TreeNode("F");
TreeNode nodeg = new TreeNode("G");
TreeNode nodeh = new TreeNode("H");
TreeNode nodei = new TreeNode("I");
TreeNode nodej = new TreeNode("J");
TreeNode nodek = new TreeNode("K");
root.leftchild = nodeb;
root.rightchild = nodec;
nodeb.leftchild = noded;
nodeb.rightchild = nodee;
noded.leftchild = nodeh;
nodee.leftchild = nodei;
nodec.leftchild = nodef;
nodec.rightchild = nodeg;
nodef.rightchild = nodej;
nodeg.leftchild = nodek;
}
/*
* 求二叉树的高度
*/
public int getHeight() {
return getHeight(this.root);
}
private int getHeight(TreeNode r) {
if(r == null) {
return 0;
}
else {
int heightL = getHeight(r.leftchild);
int heightR = getHeight(r.rightchild);
return (heightL > heightR) ? (heightL+1): (heightR+1);
}
}
/*
* 求二叉树节点个数
*/
public int getSize() {
return getSize(this.root);
}
private int getSize(TreeNode r) {
if(r == null) {
return 0;
}
else {
return 1 + getSize(r.leftchild) + getSize(r.rightchild);
}
}
/*
* 前序遍历
*/
public void preOrder(TreeNode node) {
if(node == null) {
return;
}
else {
System.out.print(node.value + " ");
preOrder(node.leftchild);
preOrder(node.rightchild);
}
}
/*
* 中序遍历
*/
public void inOrder(TreeNode node) {
if(node == null) {
return;
}else {
inOrder(node.leftchild);
System.out.print(node.value + " ");
inOrder(node.rightchild);
}
}
/*
* 后序遍历
*/
public void postOrder(TreeNode node) {
if(node == null) {
return;
}else {
postOrder(node.leftchild);
postOrder(node.rightchild);
System.out.print(node.value + " ");
}
}
/*
* 用非迭代方法实现前序遍历,用栈
* tip 判断栈是否为空 不能用 a != null, 应该用 !a.isEmpty()
*/
public void preOrderInStack(TreeNode node) {
Stack> a = new Stack<>();
if(node == null) {
System.out.println("空树");
}else {
a.push(node);
while(!a.isEmpty()) {
TreeNode b = a.pop();
System.out.print(b.value + " ");
if(b.rightchild != null) {
a.push(b.rightchild);
}
if(b.leftchild != null) {
a.push(b.leftchild);
}
}
}
}
/*
* 用非迭代方法实现中序遍历
*/
public void inOrderInStack(TreeNode node) {
Stack> a = new Stack<>();
if(node == null) {
System.out.println("空树");
}else {
a.push(node);
TreeNode b = a.peek();
while(b.leftchild != null) {
a.push(b.leftchild);
b = a.peek();
}
while(!a.isEmpty()) {
TreeNode c = a.peek();
if(c.leftchild == null || c.leftchild.isChecked == true) {
a.pop();
c.isChecked = true;
System.out.print(c.value + " ");
}else {
a.push(c.leftchild);
continue;
}
if(c.rightchild != null) {
a.push(c.rightchild);
}
}
}
}
/*
* 用非迭代方法实现后序遍历
*/
public void postOrderInStack(TreeNode node) {
Stack> a = new Stack<>();
if(node == null) {
System.out.println("空树");
}else {
a.push(node);
TreeNode b = a.peek();
while(b.leftchild != null) {
a.push(b.leftchild);
b = a.peek();
}
while(!a.isEmpty()) {
TreeNode c = a.peek();
if(c.rightchild != null && c.rightchild.isChecked == false) {
a.push(c.rightchild);
}
if(c.leftchild != null && c.leftchild.isChecked == false) {
a.push(c.leftchild);
}
if(c != a.peek()) {
continue;
}else {
a.pop();
c.isChecked = true;
System.out.print(c.value + " ");
}
}
}
}
/*
* 进行非迭代的中序或者后序遍历以后,要从新把元素的isChecked属性设置为false,以避免影响另外一个遍历
*/
public void unchecked(TreeNode node) {
if(node == null) {
return;
}else {
node.isChecked = false;
unchecked(node.leftchild);
unchecked(node.rightchild);
}
}
public class TreeNode{
private E value;
private TreeNode leftchild;
private TreeNode rightchild;
private boolean isChecked;
public TreeNode(E val) {
this.value = val;
this.leftchild = null;
this.rightchild = null;
this.isChecked = false;
}
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryTree tree = new BinaryTree();
tree.createTestTree();
TreeNode r = tree.root;
System.out.println(tree.getHeight());
System.out.println(tree.getSize());
tree.preOrder(r);
System.out.print("\n");
tree.inOrder(r);
System.out.print("\n");
tree.postOrder(r);
System.out.print("\n");
tree.preOrderInStack(r);
System.out.print("\n");
tree.inOrderInStack(r);
System.out.print("\n");
tree.unchecked(r);
tree.postOrderInStack(r);
}
}
java数据结构编写二叉树_java 数据结构与算法 BinaryTree二叉树编写相关推荐
- 二叉树的层序遍历算法 + 打印二叉树所有最左边的元素(算法)
二叉树的层序遍历算法 + 打印二叉树所有最左边的元素(算法) 层序遍历 /** * 树结构定义 */ private static class BinaryNode<T> {BinaryN ...
- java有内置二叉树_java数据结构——二叉树
一.树概述 树是一种特殊的数据结构,他可以用来描述有分支的结构是由一个或者一个以上的有限集合组成,具有两个属性:一是存在一个特殊的节点,成为树根:二是其余节点分为n>=0个互斥集合,T1,T2, ...
- java 二维链表_Java数据结构与算法----数组与链表
数据类型 1 数据类型介绍 数据类型的分类(按照结构划分):线性结构和非线性结构 线性结构:线性结构作为最常用的数据结构,其特点是数据元素之间存在一对一的线性关系 线性结构有两种不同的存储结构,即顺序 ...
- java快排原理_Java数据结构与算法——快速排序
声明:码字不易,转载请注明出处,欢迎文章下方讨论交流. 前言:Java数据结构与算法专题会不定时更新,欢迎各位读者监督.本篇文章介绍排序算法中最常用也是面试中最容易考到的排序算法--快排,包括快排的思 ...
- java遍历树结构数据_Java数据结构——二叉树的遍历(汇总)
二叉树的遍历分为深度优先遍历(DFS)和广度优先遍历(BFS) DFS遍历主要有: 前序遍历 中序遍历 后序遍历 一.递归实现DFS Node.java: public class Node { pr ...
- java数据结构是什么_java数据结构
数据结构(Data Structure):是相互之间存在一种或多种特定关系的数据元素的集合. 4.逻辑结构: 集合结构中的数据元素除了同属于一种类型外,别无其它关系. 线性结构结构中的数据元素之间存在 ...
- java数据结构课程设计_java数据结构课程设计
华东交大理工学院 课程设计(论文)任务书 电信分院2009 电子商务专业一班 一.课程设计(论文)题目走迷宫 二.课程设计(论文)工作自 2011 年6月20 日起至 2011 年6月30 日止. 三 ...
- java vector内存结构_Java 数据结构
Java工具包提供了强大的数据结构.在Java中的数据结构主要包括以下几种接口和类: 枚举(Enumeration) 位集合(BitSet) 向量(Vector) 栈(Stack) 字典(Dictio ...
- java 数据结构 快速入门_Java 数据结构快速入门
数据结构:栈 简介 栈(stack),又称堆栈,它是运算受限的线性表. 限制 栈(stack)的限制是仅允许在标的一端进行插入和删除操作,不允许在其他任何位置进行添加.查找.删除等操作. 采用该结构的 ...
最新文章
- 现代软件工程 第十二章 【用户体验】练习与讨论
- linux搭建--ubuntu使用qemu-kvm,libvirt搭建虚拟机,并搭建libvmi来虚拟机自省(三)
- IPv6相比IPv4在那些方面做出了改进?IPv4 to IPv6过渡的三种tunnel技术各自的特性...
- java编程点滴(3)--ubuntu下jdk的配置
- JUKI贴片机RX-7R_JUKI贴片机_贴片机
- 【问链财经-区块链基础知识系列】 第三十课 区块链金融面临的四大挑战
- 技术干货 | JavaScript 之事件循环(Event Loop)
- Zephyr应用笔记:mcuboot引导程序简单介绍
- JS遍历数组的12种方法
- VS2010开发应用程序读写注册表
- [JSOI2016]病毒感染[dp]
- MR Mapper类
- 学习c3p0连接池的原理与使用总结
- 嵌入式C语言(指针)
- Mean Shift和CamShift算法
- scrapy实战之与豆瓣反爬抗争
- C#实现触摸屏自定义键盘
- matlab海龟交易策略,【策略篇】海龟交易系统使用方法和源码
- 京东CPS商品推广接入流程
- Apache Hadoop YARN:另一个资源协调者
热门文章
- 织梦手机版list.php,织梦一级目录作域名list.php无法跳转到手机站解决方法
- c语言16位字节只能用到255,《C语言程序设计》月考试题(第三章).doc
- css 倒三角_改善CSS的10种最佳做法,帮助你从样式中获得最大的收益。
- 编写程序,生成包含1000个0到100之间的随机整数,并统计每个元素的出现次数
- Python数据结构实战—栈(Stack)
- TensorFlow模型保存和提取方法(含滑动平均模型)
- Shell.FlyoutHeader
- 《BUG创造队》作业9:【Beta】冲刺 Scrum meeting 1
- 【算法】Quick Select
- 将jar文件加到Maven的local repository中