import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.Stack;
import java.util.Vector;
import java.util.concurrent.LinkedBlockingQueue;//2叉树常用操作练习
public class BinaryTreeTest {//TESTpublic static void main(String[] args) {BinaryTree<Integer> tree = null;int[] temp = {5,8,4,7,3,1,6,9,0,2};for(int i = 0 ; i < temp.length ; i++){tree = buildTree(temp[i],tree);}preIteratorTree(tree);midIteratorTree(tree);postIteratorTreeOne(tree);layoutIteratorTree(tree);preIteratorTreeRecursion(tree);System.out.println();midIteratorTreeRecursion(tree);System.out.println();postIteratorTreeRecursion(tree);System.out.println();Integer count = treeLeafCount(tree,0);System.out.println();System.out.println(count);System.out.println(treeHeight(tree));exchangeTreeNode(tree);preIteratorTree(tree);System.out.println("isExist = " + nodeIsExist(tree,3));System.out.println("isExist = " + nodeIsExist(tree,-1));BinaryTree<Integer> node = nearestCommonParent(tree,6,9);if(node == null) System.out.println("null");else System.out.println(node.data);node = nearestCommonParent(tree,2,-1);if(node == null) System.out.println("null");else System.out.println(node.data);int[] at = {1,2,3,4,5,6,7,8,9,10};BinaryTree<Integer> tree_ = buildBalanceTree(at,0,at.length-1,null);midIteratorTreeRecursion(tree_);System.out.println();Vector<BinaryTree<Integer>> vector = new Vector<BinaryTree<Integer>>();printPath(tree,22,vector);for(int i = 0 ; i < vector.size() ; i++){BinaryTree<Integer> temp_ = vector.get(i);Vector<BinaryTree<Integer>> vectori = new Vector<BinaryTree<Integer>>();nodePath(tree,temp_.data,vectori);Collections.reverse(vectori);for(int j = 0 ; j < vectori.size() ; j++){System.out.print(vectori.get(j).data +  " ");}System.out.println();}}//先序遍历递归private static void preIteratorTreeRecursion(BinaryTree<Integer> node){if(node != null){System.out.print(node.data + "  ");preIteratorTreeRecursion(node.lchild);preIteratorTreeRecursion(node.rchild);}}//中序遍历递归private static void midIteratorTreeRecursion(BinaryTree<Integer> node){if(node != null){midIteratorTreeRecursion(node.lchild);System.out.print(node.data + "  ");midIteratorTreeRecursion(node.rchild);}}//后续遍历递归private static void postIteratorTreeRecursion(BinaryTree<Integer> node){if(node != null){postIteratorTreeRecursion(node.lchild);postIteratorTreeRecursion(node.rchild);System.out.print(node.data + "  ");}}//交换二叉树的左右儿子private static void exchangeTreeNode(BinaryTree<Integer> node){BinaryTree<Integer> temp = null;if(node != null){temp = node.lchild;node.lchild = node.rchild;node.rchild = temp;exchangeTreeNode(node.lchild);exchangeTreeNode(node.rchild);}}//判断一个节点是否在一颗子树中private static boolean nodeIsExist(BinaryTree<Integer> node,Integer data){if(node != null){if(data == node.data){return true;}else{boolean isExist1 = nodeIsExist(node.lchild,data);boolean isExist2 = nodeIsExist(node.rchild,data);return isExist1 || isExist2 ;}}return false;}//寻找行走路径private static boolean nodePath(BinaryTree<Integer> node,Integer query,Vector<BinaryTree<Integer>> v){if(node != null){if(query == node.data){v.add(node);return true;}else{boolean has = false;if(nodePath(node.lchild,query,v)){v.add(node);has = true;}if(!has && nodePath(node.rchild,query,v)){v.add(node);has = true;}return has;}}return false;}//二叉树叶子节点个数递归先序遍历private static Integer treeLeafCount(BinaryTree<Integer> node,Integer count){if(node.lchild == null && node.rchild == null){System.out.print(node.data + "  ");return ++count;}if(node.lchild != null)count = treeLeafCount(node.lchild , count);if(node.rchild != null)count = treeLeafCount(node.rchild , count);return count;}//求两个节点的最近公共祖先private static BinaryTree<Integer> nearestCommonParent(BinaryTree<Integer> tree,Integer node1,Integer node2){Vector<BinaryTree<Integer>> v1 = new Vector<BinaryTree<Integer>>();Vector<BinaryTree<Integer>> v2 = new Vector<BinaryTree<Integer>>();if(nodePath(tree,node1,v1) && nodePath(tree,node2,v2)){Collections.reverse(v1);Collections.reverse(v2);for(int i = 0 ; i < (v1.size() > v2.size() ? v1.size() : v2.size()) ; i++){if(v1.size() > i && v2.size() > i){if(v1.get(i) != v2.get(i)){return v1.get(i-1);}}else{i = v1.size() > v2.size() ? v2.size() : v1.size();return v1.get(i-1);}}}return null;}//从根节点开始找到所有路径,使得路径上的节点值和为某一数值(路径不一定以叶子节点结束)private static void printPath(BinaryTree<Integer> node,int count,Vector<BinaryTree<Integer>> v){if(node == null || node.data > count || count < 0){return ;}else{count -= node.data;if(count == 0){v.add(node);}else{printPath(node.lchild,count,v);printPath(node.rchild,count,v);}}}//求二叉树的高度private static Integer treeHeight(BinaryTree<Integer> node){if(node == null){return 0;}if(node.lchild == null && node.rchild == null){return 1;}int height1 = treeHeight(node.lchild);int height2 = treeHeight(node.rchild);return  height1 - height2 > 0 ? ++height1 : ++height2;}//建立2叉排序树private static BinaryTree<Integer> buildTree(Integer data,BinaryTree<Integer> node){if(node == null){node = new BinaryTree<Integer>(); node.data = data;}else{if(data < node.data){node.setLchild(buildTree(data,node.lchild));}else{node.setRchild(buildTree(data,node.rchild));}}return node;}//中序遍历非递归private static void midIteratorTree(BinaryTree<Integer> node){Stack<BinaryTree<Integer>> stack = new Stack<BinaryTree<Integer>>();while(node != null || !stack.isEmpty()){while(node != null){stack.push(node);node = node.lchild;}if(!stack.isEmpty()){BinaryTree<Integer> temp = stack.pop();System.out.print(temp.data + "  ");node = temp.rchild;}}System.out.println();}//将一个排好序的数组,转变成一颗平衡二叉树(尽量平衡) private static BinaryTree<Integer> buildBalanceTree(int[] array,int start,int end,BinaryTree<Integer> root){int sign = 0;if(end >= start){sign = end > start ? (end + start ) / 2 : end;if(root == null){root = new BinaryTree<Integer>();root.setData(array[sign]);}if(root != null){root.setLchild(buildBalanceTree(array,start,sign-1,root.lchild));root.setRchild(buildBalanceTree(array,sign+1,end,root.rchild));}}return root;}//后序遍历非递归private static void postIteratorTreeOne(BinaryTree<Integer> curr){Stack<BinaryTree<Integer>> stack = new Stack<BinaryTree<Integer>>();Map<BinaryTree<Integer>,Boolean> stackHeadLocal = new HashMap<BinaryTree<Integer>,Boolean>();while(curr != null || !stack.isEmpty()){while(curr != null){stackHeadLocal.put(curr, true);stack.push(curr);curr = curr.lchild;}if(!stack.isEmpty()){BinaryTree<Integer> temp = stack.lastElement();if(temp.rchild != null && stackHeadLocal.get(temp)){curr = temp.rchild;stackHeadLocal.put(temp, false);}else{temp = stack.pop();System.out.print(temp.data + "  ");}}}System.out.println();}//先序遍历非递归private static void preIteratorTree(BinaryTree<Integer> node){Stack<BinaryTree<Integer>> stack = new Stack<BinaryTree<Integer>>();stack.push(node);while(node != null || !stack.isEmpty()){if(!stack.isEmpty()){node = stack.pop();System.out.print(node.data + "  ");}if(node.rchild != null){stack.push(node.rchild);}if(node.lchild != null){stack.push(node.lchild);}node = node.lchild;}System.out.println();}//层次遍历2叉树private static void layoutIteratorTree(BinaryTree<Integer> node){Queue<BinaryTree<Integer>> queue = new LinkedBlockingQueue<BinaryTree<Integer>>();queue.offer(node);while(!queue.isEmpty()){if(!queue.isEmpty()){node = queue.poll();System.out.print(node.data + "  ");}if(node.lchild != null){queue.offer(node.lchild);}if(node.rchild != null){queue.offer(node.rchild);}}System.out.println();}static class BinaryTree<T>{private T data;private BinaryTree<T> lchild;private BinaryTree<T> rchild;public T getData() {return data;}public void setData(T data) {this.data = data;}public BinaryTree<T> getLchild() {return lchild;}public void setLchild(BinaryTree<T> lchild) {this.lchild = lchild;}public BinaryTree<T> getRchild() {return rchild;}public void setRchild(BinaryTree<T> rchild) {this.rchild = rchild;}}
}

转载于:https://www.cnblogs.com/lixusign/p/3352557.html

【数据结构】- 二叉树基础操作相关推荐

  1. [数据结构] 二叉树基础

    Python实现 """Pre-order, in-order and post-order traversal of binary trees.Author: Wenr ...

  2. 43. 盘点那些必问的数据结构算法题之二叉树基础

    盘点那些必问的数据结构算法题之二叉树基础 0 概述 1 定义 2 基本操作 1) 创建结点 2) BST 插入结点 3) BST 删除结点 4) BST 查找结点 5)BST 最小值结点和最大值结点 ...

  3. 【数据结构】基础:二叉树

    [数据结构]基础:二叉树基础 摘要:本文将会介绍二叉树的基础内容,首先引入树的概念,了解树的基本概念与性质,再对二叉树的概念和性质进行分析,最后对其方法进行实现,最重要的是理解对于二叉树方法实现的分治 ...

  4. 【Python数据结构系列】☀️《树与二叉树-基础知识》——知识点讲解+代码实现☀️

    文章目录 数据结构之树和二叉树 第一部分 树和二叉树的基础知识 1.树和二叉树的定义 1.1 树的定义 1.2 树的基本术语 1.3 二叉树的定义 2.二叉树的性质和存储结构 2.1 二叉树的性质 2 ...

  5. 数据结构实验二 :二叉树的操作与实现

    数据结构实验一:线性表,堆栈和队列实现 数据结构实验二 :二叉树的操作与实现 数据结构实验三: 图的操作与实现 数据结构实验四 : 查找和排序算法实现 文章目录 一.实验目的: 二.使用仪器.器材 三 ...

  6. 广州大学学生实验报告,数据结构实验,二叉树的操作与实现

    广州大学学生实验报告 开课学院及实验室: 计算机科学与工程实验室 418              2022年10月3日 学院 计算机科学与网络工程 年级.专业.班 计科 姓名 Great Macro ...

  7. 数据结构——二叉树的遍历

    "树"是一种重要的数据结构,本文浅谈二叉树的遍历问题,採用C语言描写叙述. 一.二叉树基础 1)定义:有且仅有一个根结点,除根节点外,每一个结点仅仅有一个父结点,最多含有两个子节点 ...

  8. 数据结构:二叉树及堆排序

    文章目录 1.树概念及结构 1.1 树的概念 1.2树的表示 1.3树在史记中的运用 2. 二叉树概念及结构 2.1 概念 2.2 特殊的二叉树 2.3二叉树的存储结构 2.3.1 顺序存储 2.5. ...

  9. 数据结构——二叉树总结

    数据结构-二叉树总结 写在前面 二叉树遍历 递归实现先.中.后序遍历 非递归遍历 先序非递归 中序非递归 后序非递归 层次遍历 二叉树还原 先序中序建树 后序中序建树 层次中序建树 二叉树应用 二叉查 ...

最新文章

  1. linux ll一页一页,Linux使用技巧33则
  2. jackson 反序列化string_Java 中使用Jackson反序列化
  3. 包银消费CTO汤向军:消费金融大数据风控架构与实践
  4. hdu5343 后缀自动机+dp
  5. plsql developer连接oracle--本地不安装oracle
  6. TomCat JDK环境变量
  7. 在java中8421_JAVA常量介绍
  8. 喜欢网络文学的人有多少?当代年轻人是这样阅读的
  9. 【C++】随机函数的使用
  10. Android 仿美团网,大众点评购买框悬浮效果之修改版
  11. idea开发vue项目时,使用@引入组件警告:Module is not installed
  12. android 获取用户名和密码,如何通过Android中的电子邮件地址获取用户名和密码
  13. TransE 论文笔记
  14. 平面设计banner排版技巧哪些比较实用
  15. 通过Ubuntu16.04编译Android下的osip动态库
  16. 破案了!不会讲笑话不会作诗的chatGPT!
  17. deepin C++ 编译错误 file not found 其实是原文件后缀的问题
  18. 【卷积神经网络】CNN详解以及猫狗识别实例
  19. linux中apache与tomcat如何使用
  20. 弹窗广告关不掉?工信部:用这个方法举报!

热门文章

  1. 1088 三人行 (20分)
  2. 奇偶个数 中国大学生mooc 翁恺 C语言
  3. oracle数据库同步异步优劣点,ORACLE数据库异步IO介绍
  4. jsonobject json对象里面_「jsonobject」用JSONObject解析和处理json数据 - seo实验室
  5. Selenium系列文章汇总
  6. 嘘,我已经瞒着开发解锁APP日志文件抓取及分析啦!
  7. 课节6: 图神经网络进阶模型之 ERNIESage下
  8. TokenInsight:BTC新增流量小幅下降,链上活跃度平稳
  9. SAP License:SAP的采购组
  10. 欺诈与反欺诈的旷世攻防之战