資料結構 avl樹 高度
阿新 • • 發佈:2021-08-16
package tree.bst.avl; public class AVLTreeDemo { public static void main(String[] args) { int[] arr = {4,3,6,5,7,8}; AvlTree avlTree = new AvlTree(); for (int i = 0; i < arr.length; i++) { avlTree.addNode(new Node(arr[i])); } // 中序遍歷顯示 avlTree.infixOrder(); // 求高度 System.out.println(avlTree.getRoot().height()); System.out.println(avlTree.getRoot().leftHeight()); System.out.println(avlTree.getRoot().rightHeight()); } } class AvlTree { public Node getRoot() { return root; } public void setRoot(Node root) { this.root = root; } private Node root; public int height() { if (root == null){ return 0; } else { return root.height(); } } public void addNode(Node node) { if (root == null) { root = node; } else { root.add(node); } } public void infixOrder() { if (root == null) { return; } root.infixOrder(); } public Node search(int value) { if (root == null) { return null; } else { return root.search(value); } } public Node searchParent(int value) { if (root == null) { return null; } else { return root.searchParent(value); } } /** * 刪除最小節點 * * @param node 傳入的節點 當做一顆二叉排序書的根節點 * @return 以node為根節點的的最小節點的值 */ public int delRightTreeMin(Node node) { Node temp = node; // 迴圈查詢左子節點找到最小值 while (temp.left != null) { temp = temp.left; } // delete the mini node deleteNode(temp.value); return temp.value; } public void deleteNode(int value) { if (root == null) { return; } // find thi node Node targetNode = this.search(value); // 如果沒有找到要刪除的節點 if (targetNode == null) { return; } // 沒有父節點 if (root.left == null && root.right == null) { root = null; return; } Node parent = searchParent(value); //葉子節點 if (targetNode.left == null && targetNode.right == null) { if (parent.left != null && parent.left.value == value) { parent.left = null; } else if (parent.right != null && parent.right.value == value) { parent.right = null; } } else if (targetNode.left != null && targetNode.right != null) { //兩顆子樹 int min = this.delRightTreeMin(targetNode.right); targetNode.value = min; } else { // 一顆子樹 // if has left node if (targetNode.left != null) { if (parent != null) { // 如果targetNode是parent的左右 if (parent.left.value == value) { parent.left = targetNode.left; } else { parent.right = targetNode.left; } } else { root = targetNode.left; } } else { if (parent != null) { if (parent.right.value == value) { parent.right = targetNode.right; } else { parent.left = targetNode.right; } } else { root = targetNode.right; } } } } } class Node { public int value; public Node left; public Node right; public Node(int value) { this.value = value; } public int leftHeight() { if (left == null) { return 0; } else { return left.height(); } } public int rightHeight() { if (right == null) { return 0; } else { return right.height(); } } public int height() { return Math.max(this.left == null ? 0 : this.left.height(), this.right == null ? 0 : this.right.height()) + 1; } // find node public Node search(int value) { if (value == this.value) { return this; } else if (value < this.value) { // left tree find if (this.left == null) { return null; } return this.left.search(value); } else { if (this.right == null) { return null; } return this.right.search(value); } } // 查詢要刪除的節點的父節點 public Node searchParent(int value) { if (this.left != null && this.left.value == value || (this.right != null && this.right.value == value)) { return this; } else { if (value < this.value && this.left != null) { return this.left.searchParent(value); } else if (value >= this.value && this.right != null) { return this.right.searchParent(value); } else { return null; } } } // 新增節點 public void add(Node node) { if (node == null) { return; } // 新增 left if (node.value < this.value) { if (this.left == null) { this.left = node; } else { this.left.add(node); } } // 新增到right if (node.value > this.value) { if (this.right == null) { this.right = node; } else { this.right.add(node); } } } // 中序便利 public void infixOrder() { if (this.left != null) { this.left.infixOrder(); } System.out.println(this); if (this.right != null) { this.right.infixOrder(); } } @Override public String toString() { return "Node{" + "value=" + value + '}'; } }