java实现二叉树及(层序,先序,中序,后序,节点查找)
来源:互联网 发布:专业淘宝食品图片拍摄 编辑:程序博客网 时间:2024/05/29 14:02
import java.util.Deque;import java.util.LinkedList;public class BiTree<T> { private T Data;//节点数据 private BiTree<T> leftChild;//左孩子 private BiTree<T> rightChild;//右孩子 public T getData() { return Data;}public void setData(T data) { Data = data;}public BiTree<T> getLeftChild() { return leftChild;}public void setLeftChild(BiTree<T> leftChild) { this.leftChild = leftChild;}public BiTree<T> getRightChild() { return rightChild;}public void setRightChild(BiTree<T> rightChild) { this.rightChild = rightChild;}public BiTree(){ Data=null; leftChild=null; rightChild=null; }public BiTree(T data){ Data=data; leftChild=null; rightChild=null;}//插入左孩子,返回新节点 public BiTree<T> insertLeftChild(T data){ BiTree<T> newNode=new BiTree<T>(); newNode.setData(data); //新节点的左孩子指向当前节点的左孩子 newNode.leftChild=this.leftChild; //当前节点的左孩子指向新节点 this.leftChild=newNode; return newNode; } //插入右孩子,返回新节点 public BiTree<T> insertRightChild(T data){ BiTree<T> newTree=new BiTree<T>(); newTree.setData(data); //新节点的右孩子指向当前节点的右孩子 newTree.rightChild=this.rightChild; //当前节点的右孩子指向新节点 this.rightChild=newTree; return newTree; } //删除当前节点的右孩子,并返回被删除的右孩子节点 public BiTree<T> deleteRightChild(){ BiTree<T> oldNode=this.rightChild; this.rightChild=null; return oldNode; } //删除当前节点的左孩子,并返回被删除的左孩子节点 public BiTree<T> deleteleftChild(){ BiTree<T> oldNode=this.leftChild; this.leftChild=null; return oldNode; } //前序遍历 public void preOrder(BiTree<T> current){ if(current==null){ return; } System.out.print(" "+current.getData()); preOrder(current.getLeftChild()); preOrder(current.getRightChild()); } //中序遍历 public void InOrder(BiTree<T> current){ if(current==null){ return; } InOrder(current.leftChild); System.out.print(" "+current.getData()); InOrder(current.rightChild); } //后序遍历 public void PostOrder(BiTree<T> current){ if(current==null){ return; } PostOrder(current.leftChild); PostOrder(current.rightChild); System.out.print(" "+current.getData()); } //层序遍历 /*1.将头结点存入队列中 *2.判断队列是否为空,不为空取出队首元素,并打印此元素 *3.判断取出的节点的左孩子是否为空,不为空放入队列中 *4.判断取出的节点的右孩子是否为空,不为空放入队列中 *重复2,3,4 步骤直到队列为空结束*/ public void CenOrder(BiTree<T> current){ Deque<BiTree<T>> queue=new LinkedList<>(); if(current!=null){ queue.add(current); } while(!queue.isEmpty()){ BiTree<T> node=queue.removeFirst(); System.out.print(" "+node.getData()); if(node.getLeftChild()!=null){ queue.add(node.getLeftChild()); } if(node.getRightChild()!=null){ queue.add(node.getRightChild()); } } } public BiTree<T> search(BiTree<T> current,T data){ if(current==null){ return null; }else{ if(current.getData().equals(data)) return current; BiTree<T> result=null; result=search(current.leftChild,data); if(result==null){ result=search(current.rightChild,data); } if(result==null){ System.out.println("找不到该节点"); } return result; } } public static void main(String[] args) { BiTree<String> head=new BiTree<String>("root"); BiTree<String> left=head.insertLeftChild("FG"); BiTree<String> right=head.insertRightChild("TY"); BiTree<String> left2=left.insertLeftChild("SS"); BiTree<String> right2=left.insertRightChild("ER"); BiTree<String> left3=right.insertLeftChild("Rign"); BiTree<String> right3=right.insertRightChild("RGB"); BiTree<String> left4=left2.insertLeftChild("f4"); BiTree<String> left5=right2.insertLeftChild("data"); BiTree<String> left7=right3.insertLeftChild("kll"); BiTree<String> right5=left3.insertRightChild("KLJ"); BiTree<String> right6=right2.insertRightChild("rj"); BiTree<String> right7=right3.insertRightChild("ghhj"); System.out.println("\n-------------------层序遍历--------------------->"); head.CenOrder(head); System.out.println("\n----------------先序遍历---------------------->"); head.preOrder(head); System.out.println("\n-----------------中序遍历--------------------->"); head.InOrder(head); System.out.println("\n------------------后序遍历----------------------->"); head.PostOrder(head); System.out.println("\n-----------------测试获取数据---------------------->"); System.out.println(head.search(head,"f4" ).getData()); }}
阅读全文
0 0
- java实现二叉树及(层序,先序,中序,后序,节点查找)
- java实现二叉树查找,先,中,后序编列
- 二叉树的中序、先序、后序、层序遍历 & 二叉树的深度 & 节点查找
- Java实现二叉树的递归构建及先序、中序、后序、层序遍历(递归和循环)
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- 二叉树的遍历(先、中、后、层序)C实现
- 二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法
- 二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法
- [C/C++] 先序建立二叉树| 先序、中序、后序遍历二叉树| 求二叉树深度、节点数、叶节点数 算法实现
- 二叉树的先序、中序、后序、层序递归及非递归遍历
- 二叉树---实现先序、中序、后序和层序遍历
- 二叉树非递归实现先序,中序,后序,按层遍历
- 我强大的二叉树(先序、层序创建,先序、中序、后序、层序遍历)
- 数据结构--java实现二叉树的先序、中序、后序、层次遍历及根据先序中序建立二叉树
- 动态二叉树的先序、中序、后序和层序遍历及转换为静态二叉树
- 二叉树建立以及先序、中序、后序、层次遍历(JAVA 实现)
- 非递归遍历二叉树(先序、中序、后序、层序)
- “筋斗云”小案例
- python--string模块
- 连续子数组的最大值C++
- 【拜小白opencv】18-使用VideoCapture类读取播放视频操作--多信息版
- 深入理解linux内核-ch02
- java实现二叉树及(层序,先序,中序,后序,节点查找)
- java Collection 集合
- iOS的SVN问题
- ES6中新增数组方法与ES5之间的区别
- uva 679 小球下落
- 递归实现二叉树
- python scrapy爬虫爬取图片简易方法,使用Images模块
- Oracle数据库之SQL子查询详解
- 设计模式(18) 迭代器模式(简单入门 行为模式)