From a1d7bab6696139d4334caa9d9e59aa158bd4c018 Mon Sep 17 00:00:00 2001 From: chenhongchao Date: Tue, 24 Nov 2020 21:13:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .classpath | 6 + .gitignore | 3 + .project | 17 + .settings/org.eclipse.core.resources.prefs | 4 + .../.gitignore" | 3 + .../BinaryTree.java" | 132 ++++++ .../HeroNode.java" | 194 +++++++++ .../Text.java" | 79 ++++ .../.gitignore" | 3 + .../BinarayTreeDemo.java" | 395 ++++++++++++++++++ 10 files changed, 836 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 "\345\237\272\347\241\200\344\275\234\344\270\232/.gitignore" create mode 100644 "\345\237\272\347\241\200\344\275\234\344\270\232/BinaryTree.java" create mode 100644 "\345\237\272\347\241\200\344\275\234\344\270\232/HeroNode.java" create mode 100644 "\345\237\272\347\241\200\344\275\234\344\270\232/Text.java" create mode 100644 "\350\277\233\351\230\266\344\275\234\344\270\232/.gitignore" create mode 100644 "\350\277\233\351\230\266\344\275\234\344\270\232/BinarayTreeDemo.java" diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..3f3893a --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore index be4e216..f5a9c0c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ package-lock.json node_modules id.txt run.sh +/BinaryTree.class +/Main.class +/Node.class diff --git a/.project b/.project new file mode 100644 index 0000000..72865ed --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + lec05-tree + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..3217c92 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 +encoding/\u57FA\u7840\u4F5C\u4E1A=GBK +encoding/\u8FDB\u9636\u4F5C\u4E1A=GBK diff --git "a/\345\237\272\347\241\200\344\275\234\344\270\232/.gitignore" "b/\345\237\272\347\241\200\344\275\234\344\270\232/.gitignore" new file mode 100644 index 0000000..7c55441 --- /dev/null +++ "b/\345\237\272\347\241\200\344\275\234\344\270\232/.gitignore" @@ -0,0 +1,3 @@ +/BinaryTree.class +/HeroNode.class +/Text.class diff --git "a/\345\237\272\347\241\200\344\275\234\344\270\232/BinaryTree.java" "b/\345\237\272\347\241\200\344\275\234\344\270\232/BinaryTree.java" new file mode 100644 index 0000000..7f5e79c --- /dev/null +++ "b/\345\237\272\347\241\200\344\275\234\344\270\232/BinaryTree.java" @@ -0,0 +1,132 @@ +package 基础作业; + + + +//定义BinaryTree 二叉数 +class BinaryTree { + private HeroNode root; + + public void setRoot(HeroNode root) { + this.root = root; + } + + // 前序遍历 + public void preOrder() { + if (this.root != null) { + this.root.preOrde(); + } 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("二叉树为空,无法遍历!!!"); + } + + } + + // 层序 + public void LaywerTraversal() { + if (this.root != null) { + this.root.LaywerTraversal(); + } else { + System.out.println("二叉树为空,无法遍历!!!"); + } + + } + + // 前序查找 + public HeroNode preOrderSearch(int no) { + if (root != null) { + return root.preOrderSearch(no); + } else { + return null; + } + + } + + // 中序查找 + public HeroNode infixOrderSearch(int no) { + if (root != null) { + return root.infixOrderSearch(no); + } else { + return null; + } + + } + + // 后序查找 + public HeroNode postOrderSeach(int no) { + if (root != null) { + return root.infixOrderSearch(no); + } else { + return null; + } + + } + //删除节点 + public void delNode(int no) { + if (root != null) { + //如果只有一个root + if (root.getNo()== no) { + root = null; + }else { + //递归删除 + root.delNode(no); + } + }else { + System.out.println("空树,不可以删除!!"); + } + } + /** + *二叉树的插入,参数为(关键字) + * + **/ + public void insert(int key) { + if (this.root == null) { + HeroNode node = new HeroNode(key); + this.setRoot(node); + + + } else { + HeroNode node = new HeroNode(key); + HeroNode cur = new HeroNode(0); + cur=root; + while (true) { + if (node.getNo() < cur.getNo()) { + if (cur.getLeft() == null) { + cur.setLeft(node); + return; + } else { + cur=cur.getLeft(); + } + } else if (node.getNo() >= cur.getNo()) { + if (cur.getRight() == null) { + cur.setRight(node); + return; + } else { + cur=cur.getRight(); + + } + } + } + + } + } + +} diff --git "a/\345\237\272\347\241\200\344\275\234\344\270\232/HeroNode.java" "b/\345\237\272\347\241\200\344\275\234\344\270\232/HeroNode.java" new file mode 100644 index 0000000..e20a07a --- /dev/null +++ "b/\345\237\272\347\241\200\344\275\234\344\270\232/HeroNode.java" @@ -0,0 +1,194 @@ +package 基础作业; + +import java.util.LinkedList; + + + + + +//创建节点HeroNode 节点 +class HeroNode { + private int no; + private HeroNode left;// 默认null + private HeroNode right;// 默认null + + public HeroNode(int no) { + this.no = no; + } + + public int getNo() { + return no; + } + + public void setNo(int no) { + this.no = no; + } + + 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; + } + //递归删除节点 + //1如果删除的节点是叶子节点,则删除该节点 + //2如果删除的节点是非叶子节点,则删除该子树 + public void delNode(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.delNode(no); + } + if (this.right != null) { + this.right.delNode(no); + } + } + + // 编写前序遍历 + public void preOrde() { + System.out.print(" " + this.no);// 先输出父节点 + // 递归左子树前序遍历 + if (this.left != null) { + this.left.preOrde(); + + } + // 递归右子树 + if (this.right != null) { + this.right.preOrde(); + + } + } + + // 编写中序遍历 + public void infixOrder() { + // 递归左子树中序遍历 + if (this.left != null) { + this.left.infixOrder(); + } + // 输出父节点 + System.out.print(" " + this.no); + // 递归右子树 中序 + 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.print(" " + this.no); + } + + // 层序 + public void LaywerTraversal() { + LinkedList list = new LinkedList(); + list.add(this); + HeroNode cur; + while (!list.isEmpty()) { + cur = list.poll(); + System.out.print(" " + cur.no); + if (cur.left != null) { + list.add(cur.left); + } + if (cur.right != null) { + list.add(cur.right); + + } + } + + } + + // 前序遍历查找 + /** + * + * @param no 查找 no + * @return 如果找到就返回该Node,如果没有找到返回null + */ + + public HeroNode preOrderSearch(int no) { + // 比较当前节点是不是 + if (this.no == no) { + return this; + } + // 判断当前节点的左子节点是否为空,如果不为空,则递归前序查询 + HeroNode resNode = null; + if (this.left != null) { + resNode = this.left.preOrderSearch(no); + } + if (resNode != null) {// 说明我们左子树找到 + return resNode; + } + // 1.左递归前序查找,找到节点,则返回,否继续判断 + // 2.当前的节点的右子节点是否为空,如果不空,则继续向右递归前序查找 + if (this.right != null) { + resNode = this.right.preOrderSearch(no); + + } + return resNode; + } + + // 中序遍历 + public HeroNode infixOrderSearch(int no) { + // 判断当前结点的左子节点是否为空,如果不空,则递归中序查找 + HeroNode resNode = null; + if (this.left != null) { + resNode = this.left.infixOrderSearch(no); + } + if (resNode != null) { + return resNode; + } + if (this.no == no) { + return this; + } + + // 否则继续进行右递归的中序查找 + if (this.right != null) { + resNode = this.right.infixOrderSearch(no); + } + return resNode; + } + + // 后序遍历查找 + public HeroNode postOrderSeach(int no) { + // 判断当前节点的左子节点是否为空,如果不为空,则递归后序查找 + HeroNode resNode = null; + if (this.left != null) { + resNode = this.left.postOrderSeach(no); + } + if (resNode != null) { + return resNode; + } + if (this.right != null) { + resNode = this.right.infixOrderSearch(no); + } + if (resNode != null) { + return resNode; + } + // 左右子树都没找到 比较当前节点是不是 + if (this.no == no) { + return this; + } + return resNode; + } + +} diff --git "a/\345\237\272\347\241\200\344\275\234\344\270\232/Text.java" "b/\345\237\272\347\241\200\344\275\234\344\270\232/Text.java" new file mode 100644 index 0000000..3540c01 --- /dev/null +++ "b/\345\237\272\347\241\200\344\275\234\344\270\232/Text.java" @@ -0,0 +1,79 @@ +package 基础作业; + + + +public class Text { + + public static void main(String[] args) { + + // 构造了一个空树 + BinaryTree bt = new BinaryTree(); + /* + * 10 5 15 3 7 12 20 preOrder: 10 5 3 7 15 12 20 + */ + HeroNode heroNode1 = new HeroNode(10); + bt.setRoot(heroNode1); + HeroNode heroNode2 = new HeroNode(5); + heroNode1.setLeft(heroNode2); + HeroNode heroNode3 = new HeroNode(15); + heroNode1.setRight(heroNode3); + HeroNode heroNode4 = new HeroNode(3); + HeroNode heroNode5 = new HeroNode(7); + heroNode2.setLeft(heroNode4); + heroNode2.setRight(heroNode5); + HeroNode heroNode6 = new HeroNode(12); + HeroNode heroNode7 = new HeroNode(20); + heroNode3.setLeft(heroNode6); + heroNode3.setRight(heroNode7); + System.out.println("前序遍历:"); + bt.preOrder(); + System.out.printf("\n"); + System.out.println("中序遍历:"); + bt.infixOrder(); + System.out.printf("\n"); + System.out.println("后序遍历:"); + bt.postOrder(); + System.out.printf("\n"); + System.out.println("层序遍历:"); + bt.LaywerTraversal(); + System.out.printf("\n"); + // 前序查询 + System.out.println("前序查找测试"); + HeroNode preOrderSearch = bt.preOrderSearch(10); + if (preOrderSearch != null) { + System.out.printf("找到了,信息为 no=%d \n", preOrderSearch.getNo()); + } else { + System.out.println("没有找到了"); + } + // 中序查询 + System.out.println("中序查找测试"); + HeroNode infixOrderSearch = bt.infixOrderSearch(20); + if (infixOrderSearch != null) { + System.out.printf("找到了,信息为 no=%d \n ", infixOrderSearch.getNo()); + } else { + System.out.println("没有找到了"); + } + // 后序查询 + System.out.println("后序查找测试"); + HeroNode postOrderSeach = bt.postOrderSeach(15); + if (postOrderSeach != null) { + System.out.printf("找到了,信息为 no=%d \n", postOrderSeach.getNo()); + } else { + System.out.println("没有找到了"); + } + //删除节点 + bt.delNode(20); + System.out.println("删除20结果"); + bt.preOrder(); + bt.insert(40); + System.out.println("\n插入40"); + bt.preOrder(); + bt.insert(80); + System.out.println("\n插入80"); + bt.preOrder(); + bt.insert(1); + System.out.println("\n插入1"); + bt.preOrder(); + } + +} \ No newline at end of file diff --git "a/\350\277\233\351\230\266\344\275\234\344\270\232/.gitignore" "b/\350\277\233\351\230\266\344\275\234\344\270\232/.gitignore" new file mode 100644 index 0000000..567f791 --- /dev/null +++ "b/\350\277\233\351\230\266\344\275\234\344\270\232/.gitignore" @@ -0,0 +1,3 @@ +/BinarayTreeDemo.class +/BinaryTree.class +/HeroNode.class diff --git "a/\350\277\233\351\230\266\344\275\234\344\270\232/BinarayTreeDemo.java" "b/\350\277\233\351\230\266\344\275\234\344\270\232/BinarayTreeDemo.java" new file mode 100644 index 0000000..0e6f1ea --- /dev/null +++ "b/\350\277\233\351\230\266\344\275\234\344\270\232/BinarayTreeDemo.java" @@ -0,0 +1,395 @@ +package 进阶作业; + +import java.util.LinkedList; + +public class BinarayTreeDemo { + + public static void main(String[] args) { + + // 构造了一个空树 + BinaryTree bt = new BinaryTree(); + bt.insert(6); + bt.insert(7); + bt.insert(9); + bt.insert(8); + bt.insert(10); + bt.insert(3); + bt.insert(1); + bt.insert(2); + bt.insert(5); + bt.insert(4); + bt.preOrder(); + System.out.println(); + bt.look(9); + bt.look(5); + + + } + +} + +//定义BinaryTree 二叉数 +class BinaryTree { + private HeroNode root; + + public void setRoot(HeroNode root) { + this.root = root; + } + +//查找指定节点的前去和后记 + public void look(int key) { + if (this.root == null) { + System.out.println("空树 无法查找!!!!"); + } else { + HeroNode node = new HeroNode(key); + HeroNode cur = new HeroNode(0); + cur = root; + boolean f=true; + while (f) { + if (node.getNo() < cur.getNo()) { + if (cur.getLeft() == null) { + System.out.println("左子树没有找到!!"); + return; + } else { + cur = cur.getLeft(); + } + } else if (node.getNo() > cur.getNo()) { + if (cur.getRight() == null) { + System.out.println("右子树没有找到!!"); + return; + } else { + cur = cur.getRight(); + + } + } else if (node.getNo() == cur.getNo()) { + System.out.printf("找到了no=%d节点\n", node.getNo()); + if (cur.getLeft() != null) { + System.out.printf("no=%d节点的中序前驱是%d\n", node.getNo(), cur.getLeft().getNo()); + f=false; + + } else { + System.out.printf("no=%d节点的中序前驱是null\n", node.getNo()); + f=false; + } + if (cur.getRight() != null) { + System.out.printf("no=%d节点的中序前驱是%d\n", node.getNo(), cur.getRight().getNo()); + f=false; + + } else { + System.out.printf("no=%d节点的中序前驱null\n", node.getNo()); + f=false; + + } + } + + } + } + } + // 前序遍历 + public void preOrder() { + if (this.root != null) { + this.root.preOrde(); + } 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("二叉树为空,无法遍历!!!"); + } + + } + + // 层序 + public void LaywerTraversal() { + if (this.root != null) { + this.root.LaywerTraversal(); + } else { + System.out.println("二叉树为空,无法遍历!!!"); + } + + } + + // 前序查找 + public HeroNode preOrderSearch(int no) { + if (root != null) { + return root.preOrderSearch(no); + } else { + return null; + } + + } + + // 中序查找 + public HeroNode infixOrderSearch(int no) { + if (root != null) { + return root.infixOrderSearch(no); + } else { + return null; + } + + } + + // 后序查找 + public HeroNode postOrderSeach(int no) { + if (root != null) { + return root.infixOrderSearch(no); + } else { + return null; + } + + } + //删除节点 + public void delNode(int no) { + if (root != null) { + //如果只有一个root + if (root.getNo()== no) { + root = null; + }else { + //递归删除 + root.delNode(no); + } + }else { + System.out.println("空树,不可以删除!!"); + } + } + /** + *二叉树的插入,参数为(关键字) + * + **/ + public void insert(int key) { + if (this.root == null) { + HeroNode node = new HeroNode(key); + this.setRoot(node); + + + } else { + HeroNode node = new HeroNode(key); + HeroNode cur = new HeroNode(0); + cur=root; + while (true) { + if (node.getNo() < cur.getNo()) { + if (cur.getLeft() == null) { + cur.setLeft(node); + return; + } else { + cur=cur.getLeft(); + } + } else if (node.getNo() >= cur.getNo()) { + if (cur.getRight() == null) { + cur.setRight(node); + return; + } else { + cur=cur.getRight(); + + } + } + } + + } + } + +} + +//创建节点HeroNode 节点 +class HeroNode { + private int no; + private HeroNode left;// 默认null + private HeroNode right;// 默认null + + public HeroNode(int no) { + this.no = no; + } + + public int getNo() { + return no; + } + + public void setNo(int no) { + this.no = no; + } + + 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; + } + //递归删除节点 + //1如果删除的节点是叶子节点,则删除该节点 + //2如果删除的节点是非叶子节点,则删除该子树 + public void delNode(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.delNode(no); + } + if (this.right != null) { + this.right.delNode(no); + } + } + + + // 编写前序遍历 + public void preOrde() { + System.out.print(" " + this.no);// 先输出父节点 + // 递归左子树前序遍历 + if (this.left != null) { + this.left.preOrde(); + + } + // 递归右子树 + if (this.right != null) { + this.right.preOrde(); + + } + } + + // 编写中序遍历 + public void infixOrder() { + // 递归左子树中序遍历 + if (this.left != null) { + this.left.infixOrder(); + } + // 输出父节点 + System.out.print(" " + this.no); + // 递归右子树 中序 + 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.print(" " + this.no); + } + + // 层序 + public void LaywerTraversal() { + LinkedList list = new LinkedList(); + list.add(this); + HeroNode cur; + while (!list.isEmpty()) { + cur = list.poll(); + System.out.print(" " + cur.no); + if (cur.left != null) { + list.add(cur.left); + } + if (cur.right != null) { + list.add(cur.right); + + } + } + + } + + // 前序遍历查找 + /** + * + * @param no 查找 no + * @return 如果找到就返回该Node,如果没有找到返回null + */ + + public HeroNode preOrderSearch(int no) { + // 比较当前节点是不是 + if (this.no == no) { + return this; + } + // 判断当前节点的左子节点是否为空,如果不为空,则递归前序查询 + HeroNode resNode = null; + if (this.left != null) { + resNode = this.left.preOrderSearch(no); + } + if (resNode != null) {// 说明我们左子树找到 + return resNode; + } + // 1.左递归前序查找,找到节点,则返回,否继续判断 + // 2.当前的节点的右子节点是否为空,如果不空,则继续向右递归前序查找 + if (this.right != null) { + resNode = this.right.preOrderSearch(no); + + } + return resNode; + } + + // 中序遍历 + public HeroNode infixOrderSearch(int no) { + // 判断当前结点的左子节点是否为空,如果不空,则递归中序查找 + HeroNode resNode = null; + if (this.left != null) { + resNode = this.left.infixOrderSearch(no); + } + if (resNode != null) { + return resNode; + } + if (this.no == no) { + return this; + } + + // 否则继续进行右递归的中序查找 + if (this.right != null) { + resNode = this.right.infixOrderSearch(no); + } + return resNode; + } + + // 后序遍历查找 + public HeroNode postOrderSeach(int no) { + // 判断当前节点的左子节点是否为空,如果不为空,则递归后序查找 + HeroNode resNode = null; + if (this.left != null) { + resNode = this.left.postOrderSeach(no); + } + if (resNode != null) { + return resNode; + } + if (this.right != null) { + resNode = this.right.infixOrderSearch(no); + } + if (resNode != null) { + return resNode; + } + // 左右子树都没找到 比较当前节点是不是 + if (this.no == no) { + return this; + } + return resNode; + } + +} -- Gitee