1、二叉树简介
二叉树是n个有限元素的集合,该集合或者为空、或者由一个称为根(root)的元素及两个不相交的、被分别称为左子树和右子树的二叉树组成,是有序树。当集合为空时,称该二叉树为空二叉树。在二叉树中,一个元素也称作一个结点 。
2、二叉树图解
1)普通二叉树结构
2)满二叉树(只有最后一层为叶子节点)
3)完全二叉树
3、二叉树的生成代码
1)节点
// 创建节点
class HeroNode {
int id;
String name;
HeroNode left; // 左子节点
HeroNode right; // 右子节点
public HeroNode() {
}
public HeroNode(int id, String name) {
super();
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 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 [id=" + id + ", name=" + name + "]";
}
}
2)二叉树
// 创建二叉树
class BinaryTree {
// 根节点
private HeroNode root;
public HeroNode getRoot() {
return root;
}
public void setRoot(HeroNode root) {
this.root = root;
}
}
4、二叉树的遍历
1)前序遍历
前序遍历遵循:根节点==》左子树节点==》右子树节点
简称:根==》左==》右
a.节点中的前序遍历代码
// 前序遍历(根=>左=>右)
public void preOrder() {
// 先输出父节点
System.out.println(this);
// 递归向左子树
if (this.left != null) {
this.left.preOrder();
}
// 递归向右子树
if (this.right != null) {
this.right.preOrder();
}
}
b.二叉树中前序遍历代码(封装节点中的代码)
// 前序遍历
public void preOrder() {
if (this.root != null) {
this.root.preOrder();
} else {
System.out.println("此二叉树为空,无法前序遍历!!!");
}
}
2)中序遍历
中序遍历遵循:左子树节点==》根节点==》右子树节点
简称:左==》根==》右
a.节点中 中序遍历的代码
// 中序遍历(左=>根=>右)
public void midOrder() {
// 先递归左子树
if (this.left != null) {
this.left.midOrder();
}
// 输出当前节点
System.out.println(this);
// 递归右子树
if (this.right != null) {
this.right.midOrder();
}
}
b.二叉树中中序遍历的代码
// 中序遍历
public void midOrder() {
if (this.root != null) {
this.root.midOrder();
} else {
System.out.println("此二叉树为空,无法前序遍历!!!");
}
}
3)后序遍历
后序遍历遵循:左子树节点==》右子树节点==》根节点
简称:左==》右==》根
a.节点中后序遍历的代码
// 后续遍历(左=>右=>根)
public void subOrder() {
// 先递归左子树
if (this.left != null) {
this.left.subOrder();
}
// 递归右子树
if (this.right != null) {
this.right.subOrder();
}
// 输出当前节点
System.out.println(this);
}
b.二叉树中后序遍历的代码
// 后序遍历
public void subOrder() {
if (this.root != null) {
this.root.subOrder();
} else {
System.out.println("此二叉树为空,无法前序遍历!!!");
}
}
5、测试
1)创建main方法
public static void main(String[] args) {
HeroNode root = new HeroNode(1, "伊泽瑞尔");
HeroNode heroNode2 = new HeroNode(2, "瑞文");
HeroNode heroNode3 = new HeroNode(3, "奥丽安娜");
HeroNode heroNode4 = new HeroNode(4, "菲茨");
HeroNode heroNode5 = new HeroNode(5, "莫菲特");
// 手动创建二叉树
root.setLeft(heroNode2);
root.setRight(heroNode3);
heroNode3.setRight(heroNode4);
heroNode3.setLeft(heroNode5);
BinaryTree binaryTree = new BinaryTree();
binaryTree.setRoot(root);
// 测试遍历
System.out.println("***********前序遍历***********");
binaryTree.preOrder();
System.out.println("***********中序遍历***********");
binaryTree.midOrder();
System.out.println("***********后序遍历***********");
binaryTree.subOrder();
}
2)运行
6、完整代码
package tree;
public class BinaryTreeDemo {
public static void main(String[] args) {
HeroNode root = new HeroNode(1, "伊泽瑞尔");
HeroNode heroNode2 = new HeroNode(2, "瑞文");
HeroNode heroNode3 = new HeroNode(3, "奥丽安娜");
HeroNode heroNode4 = new HeroNode(4, "菲茨");
HeroNode heroNode5 = new HeroNode(5, "莫菲特");
// 手动创建二叉树
root.setLeft(heroNode2);
root.setRight(heroNode3);
heroNode3.setRight(heroNode4);
heroNode3.setLeft(heroNode5);
BinaryTree binaryTree = new BinaryTree();
binaryTree.setRoot(root);
// 测试遍历
System.out.println("***********前序遍历***********");
binaryTree.preOrder();
System.out.println("***********中序遍历***********");
binaryTree.midOrder();
System.out.println("***********后序遍历***********");
binaryTree.subOrder();
}
}
// 创建节点
class HeroNode {
int id;
String name;
HeroNode left; // 左子节点
HeroNode right; // 右子节点
public HeroNode() {
}
public HeroNode(int id, String name) {
super();
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 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 [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 midOrder() {
// 先递归左子树
if (this.left != null) {
this.left.midOrder();
}
// 输出当前节点
System.out.println(this);
// 递归右子树
if (this.right != null) {
this.right.midOrder();
}
}
// 后续遍历(左=>右=>根)
public void subOrder() {
// 先递归左子树
if (this.left != null) {
this.left.subOrder();
}
// 递归右子树
if (this.right != null) {
this.right.subOrder();
}
// 输出当前节点
System.out.println(this);
}
}
// 创建二叉树
class BinaryTree {
// 根节点
private HeroNode root;
public HeroNode getRoot() {
return root;
}
public void setRoot(HeroNode root) {
this.root = root;
}
// 前序遍历
public void preOrder() {
if (this.root != null) {
this.root.preOrder();
} else {
System.out.println("此二叉树为空,无法前序遍历!!!");
}
}
// 中序遍历
public void midOrder() {
if (this.root != null) {
this.root.midOrder();
} else {
System.out.println("此二叉树为空,无法前序遍历!!!");
}
}
// 后序遍历
public void subOrder() {
if (this.root != null) {
this.root.subOrder();
} else {
System.out.println("此二叉树为空,无法前序遍历!!!");
}
}
}