本文仅作为二叉树的基础入门
一、什么是二叉树
简介
- 每个节点最多只能有两个子节点的一种形式称为二叉树。
- 二叉树的子节点分为左节点和右节点。
- 如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2^n -1 , n 为层数,则我们称为满二叉树。
- 如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树。
类型
- 完全二叉树
- 满二叉树
- 平衡二叉树(AVL树)
相关术语
树的结点(node):包含一个数据元素及若干指向子树的分支;
孩子结点(child node):结点的子树的根称为该结点的孩子;
双亲结点:B 结点是A 结点的孩子,则A结点是B 结点的双亲;
兄弟结点:同一双亲的孩子结点; 堂兄结点:同一层上结点;
祖先结点: 从根到该结点的所经分支上的所有结点
子孙结点:以某结点为根的子树中任一结点都称为该结点的子孙
结点层:根结点的层定义为1;根的孩子为第二层结点,依此类推;
树的深度:树中最大的结点层
结点的度:结点子树的个数
树的度: 树中最大的结点度。
叶子结点:也叫终端结点,是度为 0 的结点;
分枝结点:度不为0的结点;
有序树:子树有序的树,如:家族树;
无序树:不考虑子树的顺序;
二、实现一个二叉树
搞清楚了什么是二叉树,先来创建一个二叉树
1、创建二叉树
- 创建一个节点,节点中需要包含两个指针指向左子结点和右子结点
class StudentNode {
private int id;
private String name;
private StudentNode left;
private StudentNode right;
public StudentNode(int id, String name) {
this.id = id;
this.name = name;
}
// Get And Set
@Override
public String toString() {
return "StudentNode{id=" + id +", name='" + name+"'}";
}
}
- 创建二叉树,其中有一个根节点(root)
class BinaryTree {
/**
* 根节点
*/
private StudentNode root;
public void setRoot(StudentNode root) {
this.root = root;
}
}
- main方法,向二叉树中添加数据
public static void main(String[] args) {
//创建二叉树
BinaryTree binaryTree = new BinaryTree();
//创建向二叉树中添加的节点
StudentNode root = new StudentNode(1,"aaa");
StudentNode node2 = new StudentNode(2,"bbb");
StudentNode node3 = new StudentNode(3,"ccc");
StudentNode node4 = new StudentNode(4,"ddd");
StudentNode node5 = new StudentNode(5,"eee");
//手动创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);
}
2、遍历二叉树
思路分析
前序遍历: 先输出父节点,再遍历左子树和右子树
中序遍历: 先遍历左子树,再输出父节点,再遍历右子树
后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点
小结: 看输出父节点的顺序,就确定是前序,中序还是后序
代码实现
//节点
class StudentNode {
/**
* 前序遍历
*/
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);
}
}
//二叉树
class BinaryTree {
/**
* 前序遍历
*/
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("二叉树为空,不可遍历");
}
}
2、查找二叉树中的某个节点
//学生节点
class StudentNode {
/**
* 前序遍历查找
* @param id 学生id
* @return
*/
public StudentNode preOrderSearch(int id){
System.out.println("进入前序");
//1.比较当前节点
if(this.id == id){
return this;
}
StudentNode resultNode = null;
//2.判断左子节点是否为空,不为空则继续向左子节点查找
if(this.left != null){
resultNode = this.left.preOrderSearch(id);
}
//说明在左子树找到
if(resultNode != null){
return resultNode;
}
//3.判断右子节点
if(this.right != null){
resultNode = this.right.preOrderSearch(id);
}
return resultNode;
}
/**
* 中序遍历查找
* @param id 学生id
* @return
*/
public StudentNode infixOrderSearch(int id){
StudentNode resultNode = null;
//1.判断左子节点
if(this.left != null){
resultNode = this.left.infixOrderSearch(id);
}
//说明在左子树找到
if(resultNode != null){
return resultNode;
}
System.out.println("进入中序");
//2.比较当前节点
if(this.id == id){
return this;
}
//3.判断右子节点
if(this.right != null){
resultNode = this.right.infixOrderSearch(id);
}
return resultNode;
}
/**
* 后序遍历查找
* @param id 学生id
* @return
*/
public StudentNode postOrderSearch(int id){
StudentNode resultNode = null;
//1.判断左子节点
if(this.left != null){
resultNode = this.left.postOrderSearch(id);
}
//说明在左子树找到
if(resultNode != null){
return resultNode;
}
//2.判断右子节点
if(this.right != null){
resultNode = this.right.postOrderSearch(id);
}
//说明在右子树找到
if(resultNode != null){
return resultNode;
}
System.out.println("进入后序");
//3.比较当前节点
if(this.id == id){
return this;
}
return resultNode;
}
}
//二叉树
class BinaryTree {
/**
* 前序遍历查找
* @param id
* @return
*/
public StudentNode preOrderSearch(int id){
if(root != null){
return root.preOrderSearch(id);
}else {
return null;
}
}
/**
* 中序遍历查找
* @param id
* @return
*/
public StudentNode infixOrderSearch(int id){
if(root != null){
return root.infixOrderSearch(id);
}else {
return null;
}
}
/**
* 后序遍历查找
* @param id
* @return
*/
public StudentNode postOrderSearch(int id){
if(root != null){
return root.postOrderSearch(id);
}else {
return null;
}
}
}
3、删除二叉树中的某个节点
要求
如果删除的节点是叶子节点,则删除该节点
如果删除的节点是非叶子节点,则删除该子树.
代码实现
/**
* 学生节点
*/
class StudentNode {
/**
* 节点删除
* @param id
*/
public void deleteNode(int id){
//1.如果当前节点的左子节点不为空,就将左子节点删除
if(this.left != null && this.left.id == id){
this.left = null;
return;
}
//2.如果当前节点的右子节点不为空,就将右子节点删除
if(this.right != null && this.right.id == id){
this.right = null;
return;
}
//3.将左子树递归删除
if(this.left!= null){
this.left.deleteNode(id);
}
//4.将右子树递归删除
if(this.right != null){
this.right.deleteNode(id);
}
}
}
/**
* 二叉树
*/
class BinaryTree {
/**
* 删除节点
* @param id
*/
public void deleteNode(int id){
if(root != null){
//需要先判断root是不是你要删除的节点
if(root.getId() == id){
root = null;
}
else{
root.deleteNode(id);
}
}else{
System.out.println("root is empty");
}
}
}
三、附完整代码
/**
* 二叉树
*/
public class BinaryTreeDemo {
public static void main(String[] args) {
//创建二叉树
BinaryTree binaryTree = new BinaryTree();
//创建向二叉树中添加的节点
StudentNode root = new StudentNode(1,"aaa");
StudentNode node2 = new StudentNode(2,"bbb");
StudentNode node3 = new StudentNode(3,"ccc");
StudentNode node4 = new StudentNode(4,"ddd");
StudentNode node5 = new StudentNode(5,"eee");
//手动创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);
// // 测试遍历
// System.out.println("前序遍历");
// binaryTree.preOrder();
//
// System.out.println("中序遍历");
// binaryTree.infixOrder();
//
// System.out.println("后序遍历");
// binaryTree.postOrder();
//测试遍历查找
// System.out.println("前序遍历查找");
// StudentNode resNode = binaryTree.preOrderSearch(5);
// if (resNode!=null){
// System.out.println(resNode);
// }
// else{
// System.out.println("查找失败");
// }
//
// System.out.println("中序遍历查找");
// resNode = binaryTree.infixOrderSearch(5);
// if (resNode!=null){
// System.out.println(resNode);
// }
// else{
// System.out.println("查找失败");
// }
//
// System.out.println("后序遍历查找");
// resNode = binaryTree.postOrderSearch(5);
// if (resNode!=null){
// System.out.println(resNode);
// }
// else{
// System.out.println("查找失败");
// }
//测试删除节点
System.out.println("删除前二叉树");
binaryTree.preOrder();
binaryTree.deleteNode(3);
System.out.println("删除后二叉树");
binaryTree.preOrder();
}
}
/**
* 二叉树
*/
class BinaryTree {
/**
* 根节点
*/
private StudentNode root;
public void setRoot(StudentNode 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("二叉树为空,不可遍历");
}
}
/**
* 前序遍历查找
* @param id
* @return
*/
public StudentNode preOrderSearch(int id){
if(root != null){
return root.preOrderSearch(id);
}else {
return null;
}
}
/**
* 中序遍历查找
* @param id
* @return
*/
public StudentNode infixOrderSearch(int id){
if(root != null){
return root.infixOrderSearch(id);
}else {
return null;
}
}
/**
* 后序遍历查找
* @param id
* @return
*/
public StudentNode postOrderSearch(int id){
if(root != null){
return root.postOrderSearch(id);
}else {
return null;
}
}
/**
* 删除节点
* @param id
*/
public void deleteNode(int id){
if(root != null){
//需要先判断root是不是你要删除的节点
if(root.getId() == id){
root = null;
}
else{
root.deleteNode(id);
}
}else{
System.out.println("root is empty");
}
}
}
/**
* 学生节点
*/
class StudentNode {
private int id;
private String name;
private StudentNode left;
private StudentNode right;
public StudentNode(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public StudentNode getLeft() {
return left;
}
public void setLeft(StudentNode left) {
this.left = left;
}
public StudentNode getRight() {
return right;
}
public void setRight(StudentNode right) {
this.right = right;
}
@Override
public String toString() {
return "StudentNode{id=" + id +", 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);
}
/**
* 前序遍历查找
* @param id 学生id
* @return
*/
public StudentNode preOrderSearch(int id){
System.out.println("进入前序");
//1.比较当前节点
if(this.id == id){
return this;
}
StudentNode resultNode = null;
//2.判断左子节点是否为空,不为空则继续向左子节点查找
if(this.left != null){
resultNode = this.left.preOrderSearch(id);
}
//说明在左子树找到
if(resultNode != null){
return resultNode;
}
//3.判断右子节点
if(this.right != null){
resultNode = this.right.preOrderSearch(id);
}
return resultNode;
}
/**
* 中序遍历查找
* @param id 学生id
* @return
*/
public StudentNode infixOrderSearch(int id){
StudentNode resultNode = null;
//1.判断左子节点
if(this.left != null){
resultNode = this.left.infixOrderSearch(id);
}
//说明在左子树找到
if(resultNode != null){
return resultNode;
}
System.out.println("进入中序");
//2.比较当前节点
if(this.id == id){
return this;
}
//3.判断右子节点
if(this.right != null){
resultNode = this.right.infixOrderSearch(id);
}
return resultNode;
}
/**
* 后序遍历查找
* @param id 学生id
* @return
*/
public StudentNode postOrderSearch(int id){
StudentNode resultNode = null;
//1.判断左子节点
if(this.left != null){
resultNode = this.left.postOrderSearch(id);
}
//说明在左子树找到
if(resultNode != null){
return resultNode;
}
//2.判断右子节点
if(this.right != null){
resultNode = this.right.postOrderSearch(id);
}
//说明在右子树找到
if(resultNode != null){
return resultNode;
}
System.out.println("进入后序");
//3.比较当前节点
if(this.id == id){
return this;
}
return resultNode;
}
/**
* 节点删除
* @param id
*/
public void deleteNode(int id){
//1.如果当前节点的左子节点不为空,就将左子节点删除
if(this.left != null && this.left.id == id){
this.left = null;
return;
}
//2.如果当前节点的右子节点不为空,就将右子节点删除
if(this.right != null && this.right.id == id){
this.right = null;
return;
}
//3.将左子树递归删除
if(this.left!= null){
this.left.deleteNode(id);
}
//4.将右子树递归删除
if(this.right != null){
this.right.deleteNode(id);
}
}
}