1、二叉链表节点类的定义;

2、二叉树类的定义;

3、建立下图所示的二叉树

以字符串的形式“根左右”定义一棵二叉树时,写出创建二叉树的操作:

4、编程实现以上二叉树的递归前序、非递归前序、中序和后序遍历操作,输出遍历序列;

5、完成统计以上二叉树中叶子结点的个数或计算以上二叉树的深度;

6、定义二叉查找树类;实现二叉查找树的查找、插入和删除操作;

7、从键盘上输入六个整数45、24、53、12、37、9构造二叉查找树,输出二叉查找树的中序遍历结果;

8、在二叉查找树上插入元素2和42。

9、在二叉查找树上查找37和50,并输出能否查找成功;

10、删除数据元素24和53,输出其中序遍历结果。

import java.util.Scanner;
import java.util.Stack;class BTNode<AnyType>{   //节点类AnyType data;BTNode leftChildNode,rightChildNode;public BTNode(){data=null;leftChildNode=rightChildNode=null;}public BTNode(AnyType d,BTNode<AnyType> lc,BTNode<AnyType> rc){data=d;leftChildNode=lc;rightChildNode=rc;}public BTNode(AnyType d){data=d;leftChildNode=null;rightChildNode=null;}public BTNode<AnyType>getleftChild(){return leftChildNode;}public BTNode<AnyType>getrightChild(){return rightChildNode;}public Object getData(){return data;}
}
public class BinaryTree<AnyType>{public BTNode<AnyType>root;int i=0;int count=0;public BinaryTree(){}public BinaryTree(AnyType rootNodeltem){root.data=rootNodeltem;root.leftChildNode=root.rightChildNode=null;}public BinaryTree(BTNode<AnyType>t){root=t;}public BTNode<AnyType>createBinaryTree(AnyType [] preOrder){//先序建树BTNode<AnyType>root=null;if(i<preOrder.length){AnyType data=preOrder[i];i++;if(!data.equals("null")){root=new BTNode<AnyType>(data);root.leftChildNode=createBinaryTree(preOrder);root.rightChildNode=createBinaryTree(preOrder);}}return root;}public void preOrder(BTNode<AnyType>root){                    //递归先序遍历if(root!=null){System.out.print(root.data+" ");preOrder(root.leftChildNode);preOrder(root.rightChildNode);}}public void inOrder(BTNode<AnyType>root){                     //中序遍历if(root!=null){inOrder(root.leftChildNode);System.out.print(root.data+" ");inOrder(root.rightChildNode);}}public void postOrder(BTNode<AnyType>root){                   //后序遍历if(root!=null){postOrder(root.leftChildNode);postOrder(root.rightChildNode);System.out.print(root.data+" ");}}public void preOrderTraverse(BTNode<AnyType> root){           //非递归先序遍历Stack<BTNode<AnyType>>S=new Stack<BTNode<AnyType>>();BTNode<AnyType>p=root;while(p!=null||!S.isEmpty()){if(p!=null){System.out.print(p.data+" ");S.push(p);p=p.leftChildNode;}else{p=S.pop();p=p.rightChildNode;}}}public BTNode<AnyType>search(BTNode<AnyType>root,AnyType x){    //查找某个元素if(root==null)return null;if(root.getData().equals(x))return root;BTNode<AnyType>p=search(root.leftChildNode,x);if(p==null)return p;elsereturn (search(root.rightChildNode,x));}public int countLeafNode(BTNode<AnyType>root){                  //计算叶子节点的个数if(root==null)return 0;if((root.leftChildNode==null&&root.rightChildNode==null ))return 1;else return countLeafNode(root.leftChildNode)+countLeafNode(root.rightChildNode);}public int depth(){                                              //求树的深度return depth(root); }public int depth(BTNode<AnyType> root){int leftdepth;int rightdepth;if(root==null)return 0;leftdepth=depth(root.leftChildNode);rightdepth=depth(root.rightChildNode);boolean la=leftdepth<rightdepth;if(la==true)return rightdepth+1;elsereturn leftdepth+1;}public static void main(String args[]){String tree[]=new String[13];Scanner cin=new Scanner(System.in);for(int i=0;i<13;i++){tree[i]=cin.next();}BinaryTree<String>bt=new BinaryTree<String>();BTNode<String>node=bt.createBinaryTree(tree);BinaryTree<String>text=new BinaryTree<String>(node);System.out.println("递归先序遍历的结果是:");bt.preOrder(text.root);System.out.println();System.out.println("非递归先序遍历的结果");bt.preOrderTraverse(text.root);System.out.println();System.out.println("中序遍历结果是:");bt.inOrder(text.root);System.out.println();System.out.println("后序遍历结果是:");bt.postOrder(text.root);System.out.println();System.out.println("请输入要查找的元素:");String exist=cin.next();System.out.println("如果存在则输出:");try{System.out.println(bt.search(text.root,exist).data);}catch(Exception e){System.out.println("您查找的元素不存在!");}System.out.println("叶子节点个数是"+bt.countLeafNode(text.root));System.out.println("树的深度是:"+bt.depth(text.root));BinarySearchTree<Integer>bst=new BinarySearchTree<Integer>();//二叉查找树bst.init();}
}
class BinarySearchTree<AnyType extends Comparable <?super AnyType>>{private BTNode root=null;public BinarySearchTree(){}public BinarySearchTree(BTNode<AnyType> node){root=node;}BTNode<AnyType>insert(AnyType x,BTNode<AnyType> t){              //二叉查找树插入节点if(t==null)return new BTNode(x,null,null);int compareResult=x.compareTo(t.data);if(compareResult<0)t.leftChildNode=insert(x,t.leftChildNode);else if(compareResult>0)t.rightChildNode=insert(x,t.rightChildNode);else ;return t;}BTNode<AnyType>remove(AnyType x,BTNode<AnyType> t){              //二叉查找树删除节点if(t==null)return t;int compareResult=x.compareTo(t.data);if(compareResult<0)t.leftChildNode=remove(x,t.leftChildNode);else if(compareResult>0)t.rightChildNode=remove(x,t.rightChildNode);else if(t.leftChildNode!=null&&t.rightChildNode!=null){t.data=findMin(t.rightChildNode).data;t.rightChildNode=remove(t.data,t.rightChildNode);}else t=(t.leftChildNode!=null)?t.leftChildNode:t.rightChildNode;return t;}public BTNode<AnyType>findMin(BTNode<AnyType> t){                 //二叉查找树找最小值if(t!=null){while(t.leftChildNode!=null)t=t.leftChildNode;}return t;}public BTNode createBSTree(int n,AnyType a[]){                    //创建二叉查找树for(int i=0;i<n;i++){root=insert(a[i],root);}return root;}public void inOrder(BTNode<AnyType> rootNode){                    //中序输出if(rootNode!=null){inOrder(rootNode.leftChildNode);System.out.print(rootNode.data+" ");inOrder(rootNode.rightChildNode);}}public boolean contains(AnyType x){return contains(x,root);}public boolean contains(AnyType x,BTNode<AnyType> t){             //二叉查找树查找某个元素if(t==null)return false;int compareResult=x.compareTo(t.data);if(compareResult<0)return contains(x,t.leftChildNode);else if(compareResult>0)return contains(x,t.rightChildNode);elsereturn true;}public void init(){Integer a[]=new Integer[100];Scanner cin=new Scanner(System.in);System.out.println("请输入要创建的二叉查找树树的元素个数:");int n=cin.nextInt();System.out.println("请输入二叉树的元素:");for(int i=0;i<n;i++)a[i]=cin.nextInt();BinarySearchTree<Integer>bst=new BinarySearchTree<Integer>();BTNode<Integer> node=bst.createBSTree(n, a);BinarySearchTree<Integer>test=new BinarySearchTree<Integer>(node);System.out.println("中序遍历的结果是:");bst.inOrder(test.root);System.out.println();System.out.println("请输入插入的元素,输入0结束");                while(true){int x=cin.nextInt();if(x==0)break;bst.insert(x, test.root);}System.out.println("插入元素后的中序遍历结果是:");System.out.println("请输入要查找的元素,输入0结束:");while(true){int y=cin.nextInt();if(y==0)break;if(bst.contains(y))System.out.println(y+"这个元素存在");elseSystem.out.println(y+"这个元素不存在");}bst.inOrder(test.root);System.out.println();System.out.println("请输入要删除的元素,输入0结束");while(true){int xx=cin.nextInt();if(xx==0)break;bst.remove(xx, test.root);System.out.println("删除元素后的中序遍历结果:");bst.inOrder(test.root);System.out.println();}}
}//a b d null null null c e null null f null null
//6 45 24 53 12 37 9

二叉树的遍历和二叉查找树相关推荐

  1. Algorithm:树相关算法(BBT/BST/B树/R树)简介(二叉查找树、二叉查找树的插入节点、二叉查找树的删除、二叉树的遍历、平衡二叉树)C 语言实现

    Algorithm:树相关算法(BBT/BST/B树/R树)简介(二叉查找树.二叉查找树的插入节点.二叉查找树的删除.二叉树的遍历.平衡二叉树)C++语言实现 目录 树的基础知识 1.二叉树的遍-前序 ...

  2. 数据结构-二叉树的遍历

    二叉树是每个结点最多有两个子树的树结构.通常子树被称作"左子树"(left subtree)和"右子树"(right subtree).二叉树常被用于实现二叉查 ...

  3. 数据结构与算法-- 二叉树后续遍历序列校验

    二叉树后续遍历序列校验 题目:输入一个整数数组,判断改数组是否是某个二叉搜索树的后续遍历结果,如果是返回true否则false,假设输入数组的任意两个数字不相同. 例如输入{5,7,6,9,11,10 ...

  4. tinyxml2遍历所有节点_Python实现二叉树的遍历

    Outline: 二叉树概念 二叉树遍历(前序.中序.后序.宽度优先遍历)的迭代实现和递归实现: 二叉树的深度,二叉树到leaf的所有路径. 树(Tree) 是一种抽象数据类型(ADT),是由n(n& ...

  5. java 二叉树的高度_最全二叉树:完整详解二叉树的遍历以及完全二叉树等6种二叉树...

    树在数据结构中占据了非常重要的位置,尤其是二叉树.经常是在java面试中必问的一个环节,而且二叉树的应用场景真的非常普遍,需要重点掌握好. 但是一直以来,很多同学对于二叉树的掌握都是不太全面.今天我就 ...

  6. 【数据结构复习】二叉树的遍历——从微软2014校园招聘说起

    [数据结构复习]是学习.复习常用数据结构系列文章.数据结构与算法密不可分,是程序实现功能的重要组成部分.优秀的数据结构可以提高算法的时间及空间效率.反之,将增加算法复杂度,甚至妨碍程序的正确执行. 一 ...

  7. 算法:图解二叉树的遍历

    遍历 先序遍历 从树根开始绕着整棵树的外围转一圈,经过结点的顺序就是先序遍历的顺序 先序遍历结果:ABDHIEJCFKG 来源 来源 中序遍历 中序遍历可以想象成,按树画好的左右位置投影下来就可以了 ...

  8. 数据结构树、二叉树、完全二叉树、二叉查找树、平衡二叉树、红黑树、B+树

    树.二叉树.平衡二叉树.二叉搜索树 树的前序遍历.中序遍历和后序遍历 树的前序遍历.中序遍历和后续遍历是以遍历时根所在的位置顺序命名的.层次遍历即按层从上至下,从左至右遍历即可. 前序遍历:根-> ...

  9. 二叉树的遍历(递归与非递归)

    class Node: # 定义树节点def __init__(self, value, left=None, right=None): # 节点的值及左子树.右子树self.value = valu ...

最新文章

  1. java sort reverse_Java ArrayList sort() 方法
  2. Windows下用PIP安装scipy出现 no lapack/blas resources found
  3. thinkphp5.0.9预处理导致的sql注入复现与详细分析
  4. win8.1安装veket的方法
  5. JSP——JavaBean应用
  6. boost.asio系列——io_service
  7. ASP.NET MVC5+EF6+EasyUI 后台管理系统(52)-美化EasyUI皮肤和图标
  8. jquery.js和jquery-1.4.2.min.js的区别
  9. httpd svn 编译安装_如何安装CA证书?
  10. HTML之组件margin、padding
  11. 爬虫实例4 爬取网络小说
  12. linux使用cpu缓存,解决方案:如何在Linux中获取CPU缓存的大小
  13. Android技术分享| 自定义LayoutManager
  14. 技术能力和工作能力的联系和区别
  15. 树莓派3b+串口配置
  16. 自动化测试效率提升方案
  17. 一道有趣的最长子序列问题
  18. linux 格式工厂,在Deepin 20系统下安装格式工厂deb包后字体很小的解决方案
  19. mysql的Innodb的系统表空间和独立表空间
  20. 最短移臂调度算法_MATLAB优化算法实例——蚁群算法

热门文章

  1. 精品:企业级智能 PDF 和文档处理 SDK:GdPicture.NET
  2. 单片机c语言快速除法运算,单片机开发厂家之单片机C语言快速精度除法方案
  3. MATLAB读取显示DICOM图像
  4. 有关计算机基础的论文,有关计算机基础论文范文
  5. 【夏虫语冰】Visual Studio工程编译问题汇总
  6. 解决anaconda下载pytorch慢问题(清华镜像源)
  7. 十大项目管理-人力资源管理
  8. seo里的长尾关键词是什么
  9. 记录两种打包后在手机上看日志的方法(vconsole和eruda)
  10. MySQL5.7.28_03_一张图片带你进阶MySQL