二叉树

  • 二叉树定义
  • 二叉树概念
  • 二叉树示意图
  • 二叉树的常用术语
  • 二叉树的思路分析
  • 二叉树的遍历
    • 前序遍历
    • 中序遍历
    • 后序遍历
  • 二叉树的节点查找
    • 节点查找思路
    • 前序遍历节点查找
    • 中序遍历节点查找
    • 后序遍历节点查找
  • 二叉树的节点删除
    • 节点删除思路
    • 节点删除代码实现

二叉树定义

1、一对一的线性结构:数组、链表、向量
2、一对多的数据结构:树

二叉树概念

树有很多种,我们把每个节点最多只能有两个子节点的树叫做二叉树

二叉树示意图

1、二叉树的子节点分为左节点和右节点

3、如果该二叉树的所有叶子节点都在最后一层,并且节点总数=2^n-1,n为层数,称为满二叉树

4、如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树

二叉树的常用术语


1、节点
2、根节点
3、父节点
4、子节点
5、叶子节点(没有子节点的节点)
6、节点的权(节点值)
7、路径(从root节点找到该节点的路径)
8、层
9、子树
10、树的高度(最大层数)
11、森林(多棵树组成森林)

二叉树的思路分析

前序遍历思路
1、先输出当前节点(root)
2、如果左子节点不为空,则递归前序遍历
3、如果右子节点不为空,则递归前序遍历

中序遍历思路
1、如果左子节点不为空,则递归中序遍历
2、输出当前节点
3、如果右子节点不为空,则递归中序遍历

后序遍历
1、如果左子节点不为空,则递归后序遍历
2、如果右子节点不为空,则递归后序遍历
3、输出当前节点

二叉树的遍历

前序遍历

package tree;/*** 二叉树的前序遍历* @author shkstart* @create 2021-07-29 15:38*/
public class BinaryTreeDemo {public static void main(String[] args) {//创建二叉树BinaryTree binaryTree = new BinaryTree();//创建节点girlfriends root = new girlfriends(1, "前女朋友1号");girlfriends gf2 = new girlfriends(2, "前女朋友2号");girlfriends gf3 = new girlfriends(3, "前女朋友3号");girlfriends gf4 = new girlfriends(4, "前女朋友4号");//手动创建二叉树root.setLeft(gf2);root.setRight(gf3);gf3.setRight(gf4);binaryTree.setRoot(root);System.out.println("前序遍历");binaryTree.preorder();
//        System.out.println("中序遍历");
//        binaryTree.infixorder();
//        System.out.println("后序遍历");
//        binaryTree.postorder();}
}//创建二叉树(BinaryTree 二叉树)
class BinaryTree{private girlfriends root;public void setRoot(girlfriends root) {this.root = root;}//前序遍历public void preorder(){if(this.root!=null){this.root.preorder();}else{System.out.println("二叉树为空");}}
//
//    //中序遍历
//    public void infixorder(){//        if(this.root!=null){//            this.root.infixorder();
//        }else{//            System.out.println("二叉树为空");
//        }
//    }
//
//    //后序遍历
//    public void postorder(){//        if(this.root!=null){//            this.root.postorder();
//        }else{//            System.out.println("二叉树为空");
//        }
//    }
}//先创建girlfriends节点
class girlfriends{private int no;//编号private String name;//名字private  girlfriends left;//左节点private  girlfriends right;//右节点public girlfriends(int no, String name) {this.no = no;this.name = name;}public int getNo() {return no;}public void setNo(int no) {this.no = no;}public String getName() {return name;}public void setName(String name) {this.name = name;}public girlfriends getLeft() {return left;}public void setLeft(girlfriends left) {this.left = left;}public girlfriends getRight() {return right;}public void setRight(girlfriends right) {this.right = right;}@Overridepublic String toString() {return "girlfriends{" +"no=" + no +", name='" + name + '\'' +'}';}//前序遍历public  void preorder(){System.out.println(this);//先输出父节点//递归左子树前序遍历if(this.left!=null){this.left.preorder();}//递归向右子树前序遍历if(this.right!=null){this.right.preorder();}}
//
//    //中序遍历
//    public void infixorder(){//        //递归向左子树中序遍历
//        if(this.left!=null){//            this.left.infixorder();
//        }
//        //输出父节点
//        System.out.println(this);
//        //递归向右子树中序遍历
//        if(this.right!=null){//            this.right.infixorder();
//        }
//    }
//    //后序遍历
//    public void postorder(){//        //递归左子树后序遍历
//        if(this.left!=null){//            this.left.postorder();
//        }
//        //递归右子树后序遍历
//        if(this.right!=null){//            this.right.postorder();
//        }
//        //输出父节点
//        System.out.println(this);
//    }
}

中序遍历

package tree;/*** 二叉树的前序遍历* @author shkstart* @create 2021-07-29 15:38*/
public class BinaryTreeDemo {public static void main(String[] args) {//创建二叉树BinaryTree binaryTree = new BinaryTree();//创建节点girlfriends root = new girlfriends(1, "前女朋友1号");girlfriends gf2 = new girlfriends(2, "前女朋友2号");girlfriends gf3 = new girlfriends(3, "前女朋友3号");girlfriends gf4 = new girlfriends(4, "前女朋友4号");//手动创建二叉树root.setLeft(gf2);root.setRight(gf3);gf3.setRight(gf4);binaryTree.setRoot(root);//        System.out.println("前序遍历");
//        binaryTree.preorder();System.out.println("中序遍历");binaryTree.infixorder();
//        System.out.println("后序遍历");
//        binaryTree.postorder();}
}//创建二叉树(BinaryTree 二叉树)
class BinaryTree{private girlfriends root;public void setRoot(girlfriends root) {this.root = root;}//    //前序遍历
//    public void preorder(){//        if(this.root!=null){//            this.root.preorder();
//        }else{//            System.out.println("二叉树为空");
//        }
//    }
////中序遍历public void infixorder(){if(this.root!=null){this.root.infixorder();}else{System.out.println("二叉树为空");}}
//
//    //后序遍历
//    public void postorder(){//        if(this.root!=null){//            this.root.postorder();
//        }else{//            System.out.println("二叉树为空");
//        }
//    }
}//先创建girlfriends节点
class girlfriends{private int no;//编号private String name;//名字private  girlfriends left;//左节点private  girlfriends right;//右节点public girlfriends(int no, String name) {this.no = no;this.name = name;}public int getNo() {return no;}public void setNo(int no) {this.no = no;}public String getName() {return name;}public void setName(String name) {this.name = name;}public girlfriends getLeft() {return left;}public void setLeft(girlfriends left) {this.left = left;}public girlfriends getRight() {return right;}public void setRight(girlfriends right) {this.right = right;}@Overridepublic String toString() {return "girlfriends{" +"no=" + no +", name='" + name + '\'' +'}';}
//    //前序遍历
//    public  void preorder(){//        System.out.println(this);//先输出父节点
//        //递归左子树前序遍历
//        if(this.left!=null){//            this.left.preorder();
//        }
//        //递归向右子树前序遍历
//        if(this.right!=null){//            this.right.preorder();
//        }
//    }
////中序遍历public void infixorder(){//递归向左子树中序遍历if(this.left!=null){this.left.infixorder();}//输出父节点System.out.println(this);//递归向右子树中序遍历if(this.right!=null){this.right.infixorder();}}
//    //后序遍历
//    public void postorder(){//        //递归左子树后序遍历
//        if(this.left!=null){//            this.left.postorder();
//        }
//        //递归右子树后序遍历
//        if(this.right!=null){//            this.right.postorder();
//        }
//        //输出父节点
//        System.out.println(this);
//    }
}

后序遍历

package tree;/*** 二叉树的前序遍历* @author shkstart* @create 2021-07-29 15:38*/
public class BinaryTreeDemo {public static void main(String[] args) {//创建二叉树BinaryTree binaryTree = new BinaryTree();//创建节点girlfriends root = new girlfriends(1, "前女朋友1号");girlfriends gf2 = new girlfriends(2, "前女朋友2号");girlfriends gf3 = new girlfriends(3, "前女朋友3号");girlfriends gf4 = new girlfriends(4, "前女朋友4号");//手动创建二叉树root.setLeft(gf2);root.setRight(gf3);gf3.setRight(gf4);binaryTree.setRoot(root);//        System.out.println("前序遍历");
//        binaryTree.preorder();
//        System.out.println("中序遍历");
//        binaryTree.infixorder();System.out.println("后序遍历");binaryTree.postorder();}
}//创建二叉树(BinaryTree 二叉树)
class BinaryTree{private girlfriends root;public void setRoot(girlfriends root) {this.root = root;}//    //前序遍历
//    public void preorder(){//        if(this.root!=null){//            this.root.preorder();
//        }else{//            System.out.println("二叉树为空");
//        }
//    }
//
//    //中序遍历
//    public void infixorder(){//        if(this.root!=null){//            this.root.infixorder();
//        }else{//            System.out.println("二叉树为空");
//        }
//    }
////后序遍历public void postorder(){if(this.root!=null){this.root.postorder();}else{System.out.println("二叉树为空");}}
}//先创建girlfriends节点
class girlfriends{private int no;//编号private String name;//名字private  girlfriends left;//左节点private  girlfriends right;//右节点public girlfriends(int no, String name) {this.no = no;this.name = name;}public int getNo() {return no;}public void setNo(int no) {this.no = no;}public String getName() {return name;}public void setName(String name) {this.name = name;}public girlfriends getLeft() {return left;}public void setLeft(girlfriends left) {this.left = left;}public girlfriends getRight() {return right;}public void setRight(girlfriends right) {this.right = right;}@Overridepublic String toString() {return "girlfriends{" +"no=" + no +", name='" + name + '\'' +'}';}
//    //前序遍历
//    public  void preorder(){//        System.out.println(this);//先输出父节点
//        //递归左子树前序遍历
//        if(this.left!=null){//            this.left.preorder();
//        }
//        //递归向右子树前序遍历
//        if(this.right!=null){//            this.right.preorder();
//        }
//    }
//
//    //中序遍历
//    public void infixorder(){//        //递归向左子树中序遍历
//        if(this.left!=null){//            this.left.infixorder();
//        }
//        //输出父节点
//        System.out.println(this);
//        //递归向右子树中序遍历
//        if(this.right!=null){//            this.right.infixorder();
//        }
//    }//后序遍历public void postorder(){//递归左子树后序遍历if(this.left!=null){this.left.postorder();}//递归右子树后序遍历if(this.right!=null){this.right.postorder();}//输出父节点System.out.println(this);}
}

二叉树的节点查找

节点查找思路

前序遍历节点查找
1、先判断当前节点的no是否等于要查找的。
2、如果当前节点等于要查找的节点,则返回当前节点。
3、如果不相等,则判断当前节点的左子节点是否为空,如果不为空,则递归前序查找。
4、如果左递归前序查找,找到节点则返回,否则继续判断,当前节点的右子节点是否为 空,如果不为空,则继续向右递归前序查找。

中序遍历节点查找
1、判断当前节点的左子节点是否为空,如果不为空,则递归中序查找
2、如果找到,则返回,如果没有找到,就和当前节点比较,如果是,则返回当前节点,否则继续进行右递归的中序查找
3、如果右递归中序查找,找到就返回,否则返回null

后序遍历节点查找
1、判断当前节点的左子节点是否为空,如果不为空,则递归后序查找
2、如果找到,就返回,如果没有找到,就判断当前节点的右子节点是否为空,如果不为空,则右递归进行后序查找,如果找到,就返回
3、和当前节点进行比较,如果是则返回,否则返回null

前序遍历节点查找

//前序遍历查找public girlfriends preordersearch(int no){if(root!=null){return root.preordersearch(no);}else{return null;}}
public girlfriends preordersearch(int no){//比较当前节点if(this.no==no){return this;}//不是返回null//1、判断当前节点的左子节点是否为空,如果不为空,则递归前序查找//2、如果左递归前序查找,找到节点,则返回girlfriends resNode=null;if(this.left!=null){resNode=this.left.preordersearch(no);}if(resNode!=null){//说明左子节点找到return resNode;}//1、左递归前序遍历查找,找到节点,则返回,否则继续判断//2、当前的节点的右子节点是否为空,如果不为空,则继续向右递归前序遍历查找if(this.right!=null){resNode=this.right.preordersearch(no);}return resNode;}

中序遍历节点查找

//中序遍历public void infixorder(){//递归向左子树中序遍历if(this.left!=null){this.left.infixorder();}//输出父节点System.out.println(this);//递归向右子树中序遍历if(this.right!=null){this.right.infixorder();}}
//中序遍历查找public girlfriends infixordersearch(int no){//判断当前节点的左子节点是否为空,如果不为空,则递归中序查找girlfriends resNode=null;if(this.left!=null){resNode=left.left.infixordersearch(no);}if(resNode!=null){return resNode;}//如果找到,则返回,如果没有找到,就与当前节点比较,如果是则返回当前节点if(this.no==no){return this;}//否则继续进行右递归中序遍历if(this.right!=null){resNode=this.right.infixordersearch(no);}return resNode;}

后序遍历节点查找

//后序遍历public void postorder(){//递归左子树后序遍历if(this.left!=null){this.left.postorder();}//递归右子树后序遍历if(this.right!=null){this.right.postorder();}//输出父节点System.out.println(this);}
//后序遍历查找public girlfriends postordersearch(int no){//先判断当前节点是否为空,如果不为空,则递归后序查找girlfriends resNode=null;if(this.left!=null){resNode=this.left.postordersearch(no);}if(resNode!=null){//说明在左子树找到return resNode;}//如果左子树没有找到,则向右子树递归进行后序遍历查找if(this.right!=null){resNode=this.right.postordersearch(no);}if(resNode!=null){return resNode;}//如果左右子树都没有找到,就比较当前节点是不是要找的节点if(this.no==no){return this;}return resNode;}

二叉树的节点删除

节点删除思路

1、因为二叉树是单向的,所以我们要判断当前节点的子节点是否需要删除,而不是判断当前节点是否需要删除
2、如果当前节点的左子节点不为空,并且左子节点就是要删除的节点,就将this.left=null;并且返回(结束递归删除)
3、如果当前节点的右子节点不为空,并且右子节点就是要删除的节点,就将this.right=null;并且返回(结束递归删除)
4、如果第二和第三步没有需要删除的节点,那么我们就需要向左子树进行递归删除
5、如果第四不也没有要删除的节点,则应当向右子节点递归删除

节点删除代码实现

public void delNode(int no){if(root !=null){//如果只有一个root节点if(root.getNo()==no){root=null;//说明整颗树是空树}else{//递归删除root.delNode(no);}}else{System.out.println("空树");}}
public void delNode(int no){if(this.left!=null && this.left.no==no){this.left=null;//置空return;}if(this.right!=null && this.right.no==no){this.right=null;//置空return;}//向左子树进行递归删除if(this.left != null){this.left.delNode(no);}//向右子树进行递归删除if(this.right !=null){this.right.delNode(no);}}

数据结构——>二叉树相关推荐

  1. 数据结构 -- 二叉树

          这篇文章介绍的是经典的数据结构--二叉树,在这篇文章里介绍了几乎二叉树的所有操作.       二叉树给我们最重要的印象莫过于递归,因为这棵树就是递归的,所以,我在解决各个问题时大部分都用 ...

  2. 数据结构 - 二叉树 - 面试中常见的二叉树算法题

    数据结构 - 二叉树 - 面试中常见的二叉树算法题 数据结构是面试中必定考查的知识点,面试者需要掌握几种经典的数据结构:线性表(数组.链表).栈与队列.树(二叉树.二叉查找树.平衡二叉树.红黑树).图 ...

  3. 数据结构——二叉树的递归算法

    二叉树的结构定义: typedef struct BiNode {TElemType data;struct BiNode *lchild;struct BiNode *rchild; }BiNode ...

  4. 数据结构——二叉树的层次遍历进阶

    之前的一个博客 数据结构--二叉树的层次遍历看完这个,可以简单实现下面的问题 问题: 1.计算二叉树的最大宽度(二叉树的最大宽度是指二叉树所有层中结点个数的最大值. 2.用按层次顺序遍历二叉树的方法, ...

  5. 数据结构----二叉树叶子结点到根节点的高度计算

    数据结构----二叉树叶子结点到根节点的高度计算 代码: #include<stdio.h> #include<stdlib.h> typedef struct bstTree ...

  6. 数据结构 二叉树的存储结构_线程二叉树| 数据结构

    数据结构 二叉树的存储结构 线程二叉树 (Threaded Binary Tree ) A binary tree can be represented by using array represen ...

  7. 二叉树----数据结构:二叉树的三种遍历及习题

    二叉树----数据结构:二叉树的三种遍历,利用递归算法. 关于二叉树的遍历,应用非常广泛,不单单是访问打印结点,还可以进行一系列的操作,如赋值.删除.查找.求二叉树的深度等等. 有递归和非递归两种算法 ...

  8. 数据结构-二叉树入门Go语言实现

    数据结构-二叉树入门Go语言实现 之前我们一直在谈的是一对一的线性结构,可现实中,还有很多一对多的情况需要处理,所以我们需要研究这种一对多的数据结构--"树",考虑它的各种特性,来 ...

  9. 数据结构——二叉树——特点及性质

    数据结构--二叉树--特点及性质 二叉树(Binary Tree)是n(n=0)个结点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根结点和两棵互不相交的.分别称为根结点的左子树和右子树的二 ...

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

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

最新文章

  1. 右值引用 移动构造函数 移动语义
  2. c++的引用是什么意思?怎么回事?
  3. java多线程下载_Java实现多线程下载,支持断点续传
  4. java web 导出word_JavaWeb Project使用FreeMaker导出Word文件
  5. HDFS客户端的权限错误:Permission denied
  6. Maven +Tomcat+m2eclipse的热部署(hot deploy)
  7. x265将yuv转h265(七)
  8. reflexil教程_【转载】教你使用 Reflexil 反编译.NET
  9. paip.c3p0 数据库连接池 NullPointerException 的解决...
  10. psd缩略图上传组件
  11. 全能视频播放器:OmniPlayer for Mac(1.4.6)
  12. (完美解决)Word标题的编号出现一条黑色竖线
  13. 安全狗“老用户推荐新用户”有奖活动进行中 最高IPhone 4S手机
  14. 自动组策略(GPO)备份工具
  15. 旷视研究院张祥雨:3年看1800篇论文,28岁掌舵旷视基础模型研究
  16. Windows操作系统的日志分析
  17. 原味的SM3密码杂凑算法
  18. Python基于逻辑回归的糖尿病视网膜病变检测(数据集messidor_features.arff)
  19. [Pytorch框架] 5.1 kaggle介绍
  20. linux内核配置nfs,【参赛手记】开启Digilent提供的Linux内核的NFS支持

热门文章

  1. iOS开发中的错误整理,再一次整理通过通知中心来处理键盘,一定记得最后关闭通知中心...
  2. sqlite or svn 错误 The database disk image is malformed 可解决
  3. 使用Ajax时常用的转码方法encodeURI,escape,encodeURI
  4. HTML hidden 属性
  5. 7.携程架构实践 --- IaaS & PaaS
  6. 2.这就是搜索引擎:核心技术详解 --- 网络爬虫
  7. 8.卷1(套接字联网API)---基本UDP套接字编程
  8. 61. 创建快速响应的Web应用
  9. 第015讲 仿sohu首页面布局
  10. PIP(Python包管理工具)-Mac环境下安装