1.为什么需要树这种数据结构
(1)数组储存方式分析:
优点:通过下标的方式访问元素,速度快,对于有序数组,还可以使用二分查找来提高检索速度
缺点:如果要检索某个具体值或者插入值,会整体移动,效率低
(2)链表存储方式分析
优点:在一定程度上对数组储存方式有优化,如插入一个数值节点,只需要将插入节点,链接到链表中即可,删除效率也很好
缺点:在进行检索时,效率任然较低.
(3)树储存方式分析
能提高数据储存,读取的效率,比如利用二叉排序树(Binary Sort Tree),即可以保证数据的检索速度,同时也可以保证数据的插入删除,修改的速度
树的术语
2.二叉树的概念
1)树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树
2)二叉树的子节点分为左节点和右节点
3)如果该二叉树的所有叶子节点都在最后一层,并且总节点数2^n-1,n是层数,我们称为满二叉树
4)如果还二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树
二叉树的遍历:
(1)前序遍历:先输出父节点,再遍历左子树和右子树
(2)中序遍历:先遍历左子树,在输出父节点,再遍历右子树
(3)后序遍历:先遍历左子树,在遍历右子树,最后输出父节点
总结:看输出父节点的顺序,就确定是前序,中序,后序
分析二叉树的遍历步骤
二叉树的查找
思考分析:前序,中序和后序遍历分别比较了多少次?
二叉树删除
暂时我们先定义一下删除的规则:如果我们删除的是叶子节点的话,删除该节点,如果我们删除的是非叶子节点,那么删除该子树
上面图片步骤与我接下来代码步骤有区别,代码先判断的是6,接下来按照步骤判断
package com.self.dataStructure.binaryTree;
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
HeroNode root = new HeroNode(1, "aa");
HeroNode node2 = new HeroNode(2, "bb");
HeroNode node3 = new HeroNode(3, "cc");
HeroNode node4 = new HeroNode(4, "dd");
HeroNode node5 = new HeroNode(5, "ee");
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
binaryTree.setRoot(root);
/*System.out.println("前序遍历");
binaryTree.preOrderTree();
System.out.println("中序遍历");
binaryTree.infixOrderTree();
System.out.println("后序遍历");
binaryTree.postOrderTree();
node3.setLeft(node5);
System.out.println("=========================");
System.out.println("前序遍历");
binaryTree.preOrderTree();
System.out.println("中序遍历"); //21534
binaryTree.infixOrderTree();
System.out.println("后序遍历"); //25431
binaryTree.postOrderTree();
*/
node3.setLeft(node5);
/*System.out.println("==============================");
HeroNode node = binaryTree.preOrderSearchTree(5);
System.out.println(node);
HeroNode heroNode = binaryTree.infixOrderSearchTree(5);
System.out.println(heroNode);
HeroNode heroNode1 = binaryTree.postOrderSearchTree(5);
System.out.println(heroNode1);*/
binaryTree.preOrderTree();
//binaryTree.deleteByNoTree(5);
binaryTree.deleteByNoTree(3);
System.out.println("============");
binaryTree.preOrderTree();
}
}
class BinaryTree{
private HeroNode root;
public void setRoot(HeroNode root) {
this.root = root;
}
//前序遍历
public void preOrderTree(){
if(this.root != null){
this.root.preOrder();
}else{
System.out.println("binaryTree is null");
}
}
//中序遍历
public void infixOrderTree(){
if(this.root != null){
this.root.infixOrder();
}else{
System.out.println("binaryTree is null");
}
}
//后序遍历
public void postOrderTree(){
if(this.root != null){
this.root.postOrder();
}else{
System.out.println("binaryTree is null");
}
}
//前序遍历查找
public HeroNode preOrderSearchTree(int no){
if(root != null){
return root.preOrderSearch(no);
}else{
return null;
}
}
//中序遍历查找
public HeroNode infixOrderSearchTree(int no){
if(root != null){
return root.infixOrderSearch(no);
}else{
return null;
}
}
//后序遍历查找
public HeroNode postOrderSearchTree(int no){
if(root != null){
return root.postOrderSearch(no);
}else{
return null;
}
}
//删除节点
public void deleteByNoTree(int no){
if(root != null){
if(root.getNo() == no){
root = null;
}else{
root.deleteByNo(no);
}
}else{
System.out.println("binaryTree is null");
}
}
}
class HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
public HeroNode(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 HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode{" +
"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);
}
//前序遍历查找
public HeroNode preOrderSearch(int no){
System.out.println("11111111");
if(this.no == no){
return this;
}
HeroNode resultHeroNode = null;
if(this.left != null){
resultHeroNode = this.left.preOrderSearch(no);
}
if(resultHeroNode != null){
return resultHeroNode;
}
if(this.right != null){
resultHeroNode = this.right.preOrderSearch(no);
}
return resultHeroNode;
}
//中序遍历查找
public HeroNode infixOrderSearch(int no){
HeroNode result = null;
if(this.left != null){
result = this.left.infixOrderSearch(no);
}
if(result != null){
return result;
}
System.out.println("22222222222222222");
if(this.no == no){
return this;
}
if(this.right != null){
result = this.right.infixOrderSearch(no);
}
return result;
}
//后序遍历查找
public HeroNode postOrderSearch(int no){
HeroNode result = null;
if(this.left != null){
result = this.left.postOrderSearch(no);
}
if(result != null){
return result;
}
if(this.right != null){
result = this.right.postOrderSearch(no);
}
if(result != null){
return result;
}
System.out.println("33333333333333333");
if(this.no == no){
return this;
}
return result;
}
//删除节点
public void deleteByNo(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.deleteByNo(no);
}
if(this.right != null){
this.right.deleteByNo(no);
}
}
}
这里有个思考:
大家可以一起思考一下,有时间博主会把这块代码实现一下.
来源:CSDN
作者:代码编制世界
链接:https://blog.csdn.net/qq_44962429/article/details/103942127