@TOC
分析树
前戏:
{7,3,10,1,5,9,12}
数组:
增加慢:index本来有值,要指定插入时,后面的值都要后移
查询快:直接根据index找
链表
增加快:每插入一个数只要把节点的next指给要插入值的引用,把插入值的next指向原来的值引用
查询慢:但是要查询时,比如要查询12,就要查7次
二叉树:
分析如果以二叉排序树来存储数据,那么对数据的增删改查的效率都可以提高
树的常用术语
节点
根节点
父节点
子节点
叶子节点(没有子节点的节点)
节点的权(节点值)
路径(从root节点找到该节点的路线)
层
子树
树的高度(最大层数)
森林:多颗子树构成森林
n为层数:
2
n
−
1
2^n-1
2n−1
遍历二叉树
package com.fly.tree;
/**
* @Title: 二叉树
* @author: Fly
* @date: 2020/4/6 - 15:49
*/
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
HeroNode heroNode1 = new HeroNode(1, "宋江");
HeroNode heroNode2 = new HeroNode(2, "吴用");
HeroNode heroNode3 = new HeroNode(3, "卢俊义");
HeroNode heroNode4 = new HeroNode(4, "林冲");
binaryTree.setRoot(heroNode1);
heroNode1.setLeft(heroNode2);
heroNode1.setRight(heroNode3);
heroNode3.setRight(heroNode4);
System.out.println("前序:");
binaryTree.preOrder();
System.out.println("中序:");
binaryTree.infixOrder();
System.out.println("后序:");
binaryTree.postOrder();
}
static class BinaryTree{
private HeroNode root;
public void setRoot(HeroNode root){
this.root=root;
}
public void preOrder(){
if (this.root==null){
System.out.println("当前二叉树为空");
}else {
this.root.preOrder();
}
}
public void infixOrder(){
if (this.root==null){
System.out.println("当前二叉树为空");
}else {
this.root.infixOrder();
}
}
public void postOrder(){
if (this.root==null){
System.out.println("当前二叉树为空");
}else {
this.root.postOrder();
}
}
}
static class HeroNode {
private int no;
private String name;
private HeroNode left;
private HeroNode right;
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
public HeroNode(int no, String name) {
this.no = no;
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;
}
//编写前序遍历的方法
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 com.fly.tree;
/**
* @Title: 二叉树查找
* @author: Fly
* @date: 2020/4/6 - 17:00
*/
public class BinaryTreeSearchDemo {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
HeroNode heroNode1 = new HeroNode(1, "宋江s");
HeroNode heroNode2 = new HeroNode(2, "吴用s");
HeroNode heroNode3 = new HeroNode(3, "卢俊义s");
HeroNode heroNode4 = new HeroNode(4, "林冲s");
binaryTree.setRoot(heroNode1);
heroNode1.setLeft(heroNode2);
heroNode1.setRight(heroNode3);
heroNode3.setRight(heroNode4);
System.out.println("你要找的4");
binaryTree.preOrderSearch(4);
}
static class BinaryTree{
private HeroNode root;
public void setRoot(HeroNode root){
this.root=root;
}
public void preOrderSearch(int no){
HeroNode heroNode = this.root.preOrderSearch(no);
System.out.println(heroNode);
}
}
static class HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
public HeroNode(int no, String name) {
this.no = no;
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;
}
//编写前序遍历查找的方法
public HeroNode preOrderSearch(int no){
System.out.println("前序遍历查了次");
if (this.no==no){
return this;
}
HeroNode resNode=null;
if (this.left!=null){
resNode = this.left.preOrderSearch(no);
}
//说明左子树找到了
if (resNode!=null){
return resNode;
}
if (this.right!=null){
resNode=this.right.preOrderSearch(no);
}
//说明右子树找到了
if (resNode!=null){
return resNode;
}
return null;
}
//中序遍历查找
public HeroNode infixOrderSearch(int no){
HeroNode resNode=null;
if (this.left!=null){
resNode=this.left.infixOrderSearch(no);
}
if (resNode!=null){
return resNode;
}
System.out.println("中序遍历查了 次");
if (this.no==no){
return this;
}
if (this.right!=null){
resNode=this.right.infixOrderSearch(no);
}
if (resNode!=null){
return resNode;
}
return null;
}
}
}
删除节点
这里不考虑子节点的安排,如果有子节点也一起删除()
package com.fly.tree;
/**
* @Title: 删除二叉树结点
* @author: Fly
* @date: 2020/4/6 - 17:07
*/
public class BinaryTreeDeletedDemo {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
HeroNode heroNode1 = new HeroNode(1, "宋江d");
HeroNode heroNode2 = new HeroNode(2, "吴用d");
HeroNode heroNode3 = new HeroNode(3, "卢俊义d");
HeroNode heroNode4 = new HeroNode(4, "林冲d");
binaryTree.setRoot(heroNode1);
heroNode1.setLeft(heroNode2);
heroNode1.setRight(heroNode3);
heroNode3.setRight(heroNode4);
System.out.println("前序(删除前):");
binaryTree.preOrder();
binaryTree.delNode(3);
System.out.println("前序(删除后):");
binaryTree.preOrder();
}
static class BinaryTree{
private HeroNode root;
public void setRoot(HeroNode root){
this.root=root;
}
public void delNode(int no){
if (root!=null){
if (root.getNo()==no){
root=null;
}else {
root.delNode(no);
}
}else {
System.out.println("空树,不能删");
}
}
public void preOrder(){
this.root.preOrder();
}
}
static class HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public void setRight(HeroNode right) {
this.right = right;
}
public void delNode(int no){
//万一左子节点还有左子节点 没关系 只要将左子节点置空,就是把左子树断掉
if (this.left.no==no && this.left!=null){
this.left=null;
return;
}
if (this.right.no==no && this.right!=null){
this.right=null;
return;
}
if (this.left!=null){
this.left.delNode(no);
}
if (this.right!=null){
this.right.delNode(no);
}
}
public void preOrder() {
System.out.println(this);
if (this.left != null) {
this.left.preOrder();
}
if (this.right != null) {
this.right.preOrder();
}
}
}
}