From 359d070f8f8bc4a14e27494fea251a1b05019034 Mon Sep 17 00:00:00 2001
From: wenjinyuan <1390436418@qq.com>
Date: Wed, 25 Nov 2020 20:40:10 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B5=B7=E7=BB=B5=E5=AE=9D=E5=AE=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
lec05-tree.iml | 11 ++
src/co/A.java | 7 ++
src/co/BinaryTree.java | 38 ++++++
src/co/BinaryTreeDemo.java | 42 +++++++
src/co/TreeNode.java | 248 +++++++++++++++++++++++++++++++++++++
5 files changed, 346 insertions(+)
create mode 100644 lec05-tree.iml
create mode 100644 src/co/A.java
create mode 100644 src/co/BinaryTree.java
create mode 100644 src/co/BinaryTreeDemo.java
create mode 100644 src/co/TreeNode.java
diff --git a/lec05-tree.iml b/lec05-tree.iml
new file mode 100644
index 0000000..b107a2d
--- /dev/null
+++ b/lec05-tree.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/co/A.java b/src/co/A.java
new file mode 100644
index 0000000..1998d45
--- /dev/null
+++ b/src/co/A.java
@@ -0,0 +1,7 @@
+package co;
+
+public class A {
+ public static void main(String[] args) {
+
+ }
+}
diff --git a/src/co/BinaryTree.java b/src/co/BinaryTree.java
new file mode 100644
index 0000000..bb8b0d1
--- /dev/null
+++ b/src/co/BinaryTree.java
@@ -0,0 +1,38 @@
+package src.co;
+
+public class BinaryTree {
+ public TreeNode root;
+
+
+ public void setRoot(TreeNode root ) {
+ this.root = root;
+ }
+
+ public void PreOrder() {
+ if (this.root != null) {
+ this.root.PreOrder();
+ }
+ }
+
+ public void MidOrder() {
+ if (this.root != null) {
+ this.root.MidOrder();
+ }
+ }
+
+ public void RearOrder() {
+ if (this.root != null) {
+ this.root.RearOrder();
+ }
+ }
+
+ public void insertTree(TreeNode node) {
+ if (this.root == null) {
+ this.root = node;
+ } else {
+ this.root.insertTree(node);
+ }
+ }
+
+
+}
\ No newline at end of file
diff --git a/src/co/BinaryTreeDemo.java b/src/co/BinaryTreeDemo.java
new file mode 100644
index 0000000..7b4128a
--- /dev/null
+++ b/src/co/BinaryTreeDemo.java
@@ -0,0 +1,42 @@
+package src.co;
+
+
+
+public class BinaryTreeDemo {
+ public static void main(String[] args) {
+ BinaryTree binaryTree = new BinaryTree();
+ TreeNode root = new TreeNode(7);
+
+ // 二叉树
+ // TreeNode node1 = new TreeNode(2);
+ // TreeNode node2 = new TreeNode(3);
+ // TreeNode node3 = new TreeNode(4);
+ // TreeNode node4 = new TreeNode(5);
+ // root.setLeft(node1);
+ // root.setRight(node2);
+ // node1.setLeft(node3);
+ // node1.setRight(node4);
+ // binaryTree.setRoot(root);
+ // root.PreOrder();
+
+ // 查找二叉树
+ root.insertTree(new TreeNode(3));
+ root.insertTree(new TreeNode(10));
+ root.insertTree(new TreeNode(12));
+ root.insertTree(new TreeNode(5));
+ root.insertTree(new TreeNode(1));
+ root.insertTree(new TreeNode(16));
+ root.insertTree(new TreeNode(11));
+ root.insertTree(new TreeNode(14));
+ root.insertTree(new TreeNode(18));
+
+ binaryTree.setRoot(root);
+ root.MidOrder();
+ root.selectTree(new TreeNode(12));
+ root.deleteTree(11);
+ root.PreOrder();
+
+ root.stepOrder(binaryTree.root);
+
+ }
+}
diff --git a/src/co/TreeNode.java b/src/co/TreeNode.java
new file mode 100644
index 0000000..7e8d2e2
--- /dev/null
+++ b/src/co/TreeNode.java
@@ -0,0 +1,248 @@
+package src.co;
+
+import java.util.Queue;
+
+import java.util.LinkedList;
+
+public class TreeNode {
+ private int data;
+
+ private TreeNode left;
+ private TreeNode right;
+
+ public TreeNode(int data) {
+ this.data = data;
+
+ }
+
+ public void setNode(int data) {
+ this.data = data;
+
+ }
+
+ public int getNode() {
+ return data;
+ }
+
+ public void setLeft(TreeNode left) {
+ this.left = left;
+
+ }
+
+ public TreeNode getLeft() {
+ return left;
+ }
+
+ public void setRight(TreeNode right) {
+ this.right = right;
+
+ }
+
+ public TreeNode getright() {
+ return right;
+ }
+
+ // 二叉树的前序遍历 中左右
+ public void PreOrder() {
+ System.out.print(" " + this.data);
+ 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.print(" " + this.data);
+
+ if (this.right != null) {
+
+ this.right.MidOrder();
+ }
+ }
+
+ // 二叉树的后序排序 左右中
+ public void RearOrder() {
+ if (this.left != null) {
+
+ this.left.RearOrder();
+ }
+
+ if (this.right != null) {
+
+ this.right.RearOrder();
+ }
+ System.out.print(" " + this);
+ }
+
+ // 层次遍历
+ public void stepOrder(TreeNode Node) {
+ System.out.println();
+ Queue order = new LinkedList<>();
+ order.add(Node);
+ while (!order.isEmpty()) {
+ TreeNode node = order.poll();
+ System.out.print(node.data + " ");
+ if (node.left != null) {
+ order.add(node.left);
+ }
+ if (null != node.right) {
+ order.add(node.right);
+ }
+ }
+
+ }
+
+ // 查找二叉树的插入
+ public void insertTree(TreeNode node) {
+ if (node == null) {
+ return;
+ } else {
+ if (this.data < node.data) {
+ if (this.right == null) {
+ this.right = node;
+ } else {
+ this.right.insertTree(node);
+ }
+ } else {
+ if (this.left == null) {
+ this.left = node;
+ } else {
+ this.left.insertTree(node);
+ }
+ }
+
+ }
+
+ }
+
+ public void selectTree(TreeNode node) {
+ if (node == null) {
+ return;
+ } else {
+ if (this.data == node.data) {
+ System.out.println(" 查找到" + this.data);
+ } else if (this.data > node.data) {
+ if (this.left == null) {
+ System.out.println("不存在");
+ } else {
+ this.left.selectTree(node);
+ }
+ } else if (this.data < node.data) {
+ if (this.right == null) {
+ System.out.println("不存在");
+ } else {
+ this.right.selectTree(node);
+ }
+ }
+
+ }
+
+ }
+
+ // 删除两个子节点的目标节点: 查找目标节点
+ public TreeNode searchChildNode(int data) {
+ if (data == this.data) {
+ return this;
+ } else if (data > this.data) {
+ if (this.right == null) {
+ return null;
+ } else {
+ return this.right.searchChildNode(data);
+ }
+
+ } else {
+ if (this.left == null) {
+ return null;
+ } else {
+ return this.left.searchChildNode(data);
+ }
+
+ }
+
+ }
+
+ // 删除有子节点的目标节点: 查找目标节点的父节点的方法
+
+ public TreeNode searchParentNode(int data) {
+ if (this.left != null && this.left.data == data || this.right != null && this.right.data == data) {
+ return this;
+ } else {
+ if (data > this.data && this.right != null) {
+ return this.right.searchParentNode(data);
+ } else if (data < this.data && this.left != null) {
+ return this.left.searchParentNode(data);
+ } else {
+ return null;
+ }
+ }
+ }
+
+ // 查找目标节点的右子节点中的最小节点
+ public int searchMinNde(TreeNode node) {
+ TreeNode target = node;
+ while (target.left != null) {
+ target = target.left;
+ }
+ deleteTree(target.data);
+
+ return target.data;
+
+ }
+
+ public void deleteTree(int data) {
+
+ TreeNode result = this.searchChildNode(data);
+ TreeNode parent = this.searchParentNode(data);
+
+ // 删除叶子节点
+ if (parent.left == result) {
+ if (result.left == null && result.right == null) {
+ parent.left = null;
+ }
+ }
+ if (parent.right == result) {
+ if (result.left == null && result.right == null) {
+ parent.right = null;
+ }
+ }
+
+ /*
+ * 删除只有一个子节点的目标节点 有两种情况: (1) 子节点是目标节点的左子节点 (2) 子节点是目标节点的右子节点
+ *
+ */
+ if (result.left != null && result.right == null) {
+ parent.left = result.left;
+ }
+ if (result.left == null && result.right != null) {
+ parent.left = result.right;
+
+ }
+
+ if (result.left != null && result.right == null) {
+ parent.right = result.left;
+ }
+ if (result.left == null && result.right != null) {
+ parent.right = result.right;
+
+ }
+
+ // 删除两个节点的目标节点
+ if (result.right != null && result.left != null) {
+ int nextMin = searchMinNde(result.right);
+ result.data = nextMin;
+ }
+ }
+
+}
--
Gitee