Java實現二叉樹的構建與遍歷
阿新 • • 發佈:2019-02-16
二叉搜尋樹
不僅可查詢資料;還可以高效的地插入,刪除資料,動態維護資料可以方便地回答很多資料之間的關係問題:
min,max,fool,ceil,rank,select
二分搜尋樹不一定是完全二叉樹,所以用陣列表示並不方便
二叉搜尋樹的構建
class test { public static void main(String[] args) { Node root = new Node(1,28) ; root.insert(2, 16); root.insert(3, 30); root.insert(4, 13); root.insert(5, 22); root.insert(6, 29); root.insert(7, 42); // 查詢 int a = root.search(4); System.out.println(a); root.removeMax(); System.out.println(1); // // 前序遍歷 // root.preOrder(); // // 中序遍歷 // root.inOrder(); // // 後序遍歷 // root.postOrder(); // // 層序遍歷 // root.levelOrder(); // // 最小值 // System.out.println(root.minNumber()); // // 最大值 // System.out.println(root.maxNumber()); } }
package Search; import java.util.*; public class Node { private int key; private int value; private Node left = null; private Node right = null; static int count = 1; public Node(){ } public Node(int key, int value) { this.key = key; this.value = value; } public int getCount() { return count; } public void setKey(int key) { this.key = key; } public void setValue(int value) { this.value = value; } public int getKey() { return key; } public int getValue() { return value; } public Node getLeft() { return left; } public Node getRight() { return right; } public void setLeft(Node left) { this.left = left; } public void setRight(Node right) { this.right = right; } public int size() { return count; } public boolean isEmpty() { return count == 0; } public Node insert(int key, int value) { return insert(this, key, value); } public boolean contain(int key) { return contain(this, key); } private boolean contain(Node node, int key) { if (node == null) { return false; } if (key == node.getKey()) { return true; } else if (key < node.getKey()) { return contain(node.getLeft(), key); } else { return contain(node.getRight().key); } } public int search(int key) { return search(this, key); } // 從二叉樹中刪除最小值所在的節點 public void removeMin(){ if(this != null){ removeMin(this); } } private Node removeMin(Node node) { if(node.getLeft() == null){ node = node.getRight(); count--; return node; } node = removeMin(node.getLeft()); return node; } // 從二叉樹中刪除最小值所在的節點 public void removeMax(){ if(this != null){ removeMax(this); } } // 刪除最大節點 private Node removeMax(Node node) { if(node.getRight() == null){ node = node.getLeft(); count--; return node; } node = removeMax(node.getRight()); return node; } private int search(Node node, int key) { if (null == node) { return -1; } if (key == node.getKey()) { return node.getValue(); } else if (key < node.getKey()) { return search(node.getLeft(), key); } else { return search(node.getRight(), key); } } private Node insert(Node node, int key, int value) { if (node == null) { count++; return new Node(key, value); } if (value == node.getValue()) { node.setValue(value); } else if (value < node.getValue()) { node.setLeft(insert(node.getLeft(), key, value)); } else { node.setRight(insert(node.getRight(), key, value)); } return node; } // 中序遍歷 public void inOrder() { inOrder(this); } private void inOrder(Node node) { if (node != null) { inOrder(node.getLeft()); System.out.println(node.getKey()); inOrder(node.getRight()); } } // 後序遍歷 public void postOrder() { postOrder(this); } private void postOrder(Node node) { if (node != null) { postOrder(node.getLeft()); postOrder(node.getRight()); System.out.println(node.getKey()); } } //前序遍歷 public void preOrder() { preOrder(this); } // 層序遍歷 void levelOrder() { List<Node> queue = new ArrayList<Node>(); queue.add(this); while (!queue.isEmpty()) { Node node = queue.get(0); System.out.println(node.getValue()); queue.remove(0); if (null != node.getLeft()) { queue.add(node.getLeft()); } if (null != node.getRight()) { queue.add(node.getRight()); } } } public void preOrder(Node node) { if (node != null) { System.out.println(node.getKey()); preOrder(node.getLeft()); preOrder(node.getRight()); } } // 尋找最小值 public int minNumber() { if(count != 0){ return minNumber(this); } return -1; } private int minNumber(Node node) { while(node.getLeft() != null){ node = node.getLeft(); } return node.getKey(); } // 尋找最大值 public int maxNumber() { if(count != 0){ return maxNumber(this); } return -1; } private int maxNumber(Node node) { while(node.getRight() != null){ node = node.getRight(); } return node.getKey(); } }