-
java实现二叉查找树
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class BinarySearchTree {
-
-
- private TreeNode root = null;
-
-
- private List<TreeNode> nodelist = new ArrayList<TreeNode>();
-
- private class TreeNode {
-
- private int key;
- private TreeNode leftChild;
- private TreeNode rightChild;
- private TreeNode parent;
-
- public TreeNode(int key, TreeNode leftChild, TreeNode rightChild,
- TreeNode parent) {
- this.key = key;
- this.leftChild = leftChild;
- this.rightChild = rightChild;
- this.parent = parent;
- }
-
- public int getKey() {
- return key;
- }
-
- public String toString() {
- String leftkey = (leftChild == null ? "" : String
- .valueOf(leftChild.key));
- String rightkey = (rightChild == null ? "" : String
- .valueOf(rightChild.key));
- return "(" + leftkey + " , " + key + " , " + rightkey + ")";
- }
-
- }
-
-
- public boolean isEmpty() {
- if (root == null) {
- return true;
- } else {
- return false;
- }
- }
-
-
- public void TreeEmpty() throws Exception {
- if (isEmpty()) {
- throw new Exception("树为空!");
- }
- }
-
-
- public TreeNode search(int key) {
- TreeNode pNode = root;
- while (pNode != null && pNode.key != key) {
- if (key < pNode.key) {
- pNode = pNode.leftChild;
- } else {
- pNode = pNode.rightChild;
- }
- }
- return pNode;
- }
-
-
- public TreeNode minElemNode(TreeNode node) throws Exception {
- if (node == null) {
- throw new Exception("树为空!");
- }
- TreeNode pNode = node;
- while (pNode.leftChild != null) {
- pNode = pNode.leftChild;
- }
- return pNode;
- }
-
-
- public TreeNode maxElemNode(TreeNode node) throws Exception {
- if (node == null) {
- throw new Exception("树为空!");
- }
- TreeNode pNode = node;
- while (pNode.rightChild != null) {
- pNode = pNode.rightChild;
- }
- return pNode;
- }
-
-
- public TreeNode successor(TreeNode node) throws Exception {
- if (node == null) {
- return null;
- }
-
-
- if (node.rightChild != null) {
- return minElemNode(node.rightChild);
- }
-
- TreeNode parentNode = node.parent;
- while (parentNode != null && node == parentNode.rightChild) {
- node = parentNode;
- parentNode = parentNode.parent;
- }
- return parentNode;
- }
-
-
- public TreeNode precessor(TreeNode node) throws Exception {
- if (node == null) {
- return null;
- }
-
-
- if (node.leftChild != null) {
- return maxElemNode(node.leftChild);
- }
-
- TreeNode parentNode = node.parent;
- while (parentNode != null && node == parentNode.leftChild) {
- node = parentNode;
- parentNode = parentNode.parent;
- }
- return parentNode;
- }
-
-
- public void insert(int key) {
- TreeNode parentNode = null;
- TreeNode newNode = new TreeNode(key, null, null, null);
- TreeNode pNode = root;
- if (root == null) {
- root = newNode;
- return;
- }
- while (pNode != null) {
- parentNode = pNode;
- if (key < pNode.key) {
- pNode = pNode.leftChild;
- } else if (key > pNode.key) {
- pNode = pNode.rightChild;
- } else {
-
- return;
- }
- }
- if (key < parentNode.key) {
- parentNode.leftChild = newNode;
- newNode.parent = parentNode;
- } else {
- parentNode.rightChild = newNode;
- newNode.parent = parentNode;
- }
-
- }
-
-
- public void delete(int key) throws Exception {
- TreeNode pNode = search(key);
- if (pNode == null) {
- throw new Exception("树中不存在要删除的关键字!");
- }
- delete(pNode);
- }
-
-
- private void delete(TreeNode pNode) throws Exception {
- if (pNode == null) {
- return;
- }
- if (pNode.leftChild == null && pNode.rightChild == null) {
- TreeNode parentNode = pNode.parent;
- if (pNode == parentNode.leftChild) {
- parentNode.leftChild = null;
- } else {
- parentNode.rightChild = null;
- }
- return;
- }
- if (pNode.leftChild == null && pNode.rightChild != null) {
- TreeNode parentNode = pNode.parent;
- if (pNode == parentNode.leftChild) {
- parentNode.leftChild = pNode.rightChild;
- pNode.rightChild.parent = parentNode;
- } else {
- parentNode.rightChild = pNode.rightChild;
- pNode.rightChild.parent = parentNode;
- }
- return;
- }
- if (pNode.leftChild != null && pNode.rightChild == null) {
- TreeNode parentNode = pNode.parent;
- if (pNode == parentNode.leftChild) {
- parentNode.leftChild = pNode.leftChild;
- pNode.rightChild.parent = parentNode;
- } else {
- parentNode.rightChild = pNode.leftChild;
- pNode.rightChild.parent = parentNode;
- }
- return;
- }
-
- TreeNode successorNode = successor(pNode);
- delete(successorNode);
- pNode.key = successorNode.key;
- }
-
-
- public List<TreeNode> inOrderTraverseList() {
- if (nodelist != null) {
- nodelist.clear();
- }
- inOrderTraverse(root);
- return nodelist;
- }
-
-
- private void inOrderTraverse(TreeNode root) {
- if (root != null) {
- inOrderTraverse(root.leftChild);
- nodelist.add(root);
- inOrderTraverse(root.rightChild);
- }
- }
-
-
- public String toStringOfOrderList() {
- StringBuilder sbBuilder = new StringBuilder(" [ ");
- for (TreeNode p : inOrderTraverseList()) {
- sbBuilder.append(p.key);
- sbBuilder.append(" ");
- }
- sbBuilder.append("]");
- return sbBuilder.toString();
- }
-
-
- public String toString() {
- StringBuilder sbBuilder = new StringBuilder(" [ ");
- for (TreeNode p : inOrderTraverseList()) {
- sbBuilder.append(p);
- sbBuilder.append(" ");
- }
- sbBuilder.append("]");
- return sbBuilder.toString();
- }
-
- public TreeNode getRoot() {
- return root;
- }
-
- public static void testNode(BinarySearchTree bst, TreeNode pNode)
- throws Exception {
- System.out.println("本结点: " + pNode);
- System.out.println("前趋结点: " + bst.precessor(pNode));
- System.out.println("后继结点: " + bst.successor(pNode));
- }
-
- public static void testTraverse(BinarySearchTree bst) {
- System.out.println("二叉树遍历:" + bst);
- System.out.println("二叉查找树转换为有序列表: " + bst.toStringOfOrderList());
- }
-
- public static void main(String[] args) {
- try {
- BinarySearchTree bst = new BinarySearchTree();
- System.out.println("查找树是否为空? " + (bst.isEmpty() ? "是" : "否"));
- int[] keys = new int[] { 15, 6, 18, 3, 7, 13, 20, 2, 9, 4 };
- for (int key : keys) {
- bst.insert(key);
- }
- System.out.println("查找树是否为空? " + (bst.isEmpty() ? "是" : "否"));
- TreeNode minkeyNode = bst.minElemNode(bst.getRoot());
- System.out.println("最小关键字: " + minkeyNode.getKey());
- testNode(bst, minkeyNode);
- TreeNode maxKeyNode = bst.maxElemNode(bst.getRoot());
- System.out.println("最大关键字: " + maxKeyNode.getKey());
- testNode(bst, maxKeyNode);
- System.out.println("根结点关键字: " + bst.getRoot().getKey());
- testNode(bst, bst.getRoot());
- testTraverse(bst);
- System.out.println("****************************** ");
- testTraverse(bst);
- } catch (Exception e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
- }
-
- }
-
相关阅读:
[Leetcode] Copy List with Random Pointer
[Java] StringBuffer类
[Leetcode] Wildcard Matching
[Leetcode] Substring with Concatenation of All Words
[Leetcode] Longest Palindromic Substring
[Leetcode] Maximum Product Subarray
[leetcode] Permutation Sequence
[Leetcode] Simplify Path
[Leetcode] Minimum Window Substring
Owin asp.net 脱离 IIS
-
原文地址:https://www.cnblogs.com/votg/p/5035537.html
Copyright © 2020-2023
润新知