二叉排序樹java實現
二叉樹排序樹是什么?
二叉排序樹(Binary Sort Tree)又稱二叉查找樹、二叉搜索樹。 它或者是一棵空樹;或者是具有下列性質的二叉樹:
-
若左子樹不空,則左子樹上所有結點的值均小於它的根結點的值;
-
若右子樹不空,則右子樹上所有結點的值均大於它的根結點的值;
-
左、右子樹也分別為二叉排序樹;
二叉排序樹節點的定義
package tree;
public class TreeNode {
private int data;//數據
private TreeNode left;//左子樹節點
private TreeNode right;//右子樹節點
public TreeNode(int data, TreeNode left, TreeNode right) {
super();
this.data = data;
this.left = left;
this.right = right;
}
public TreeNode(int data) {
super();
this.data = data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
@Override
public String toString() {
return "TreeNode [data=" + data+"]";
}
}
二叉排序樹類實現
package tree;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
public class BinaryTree {
private TreeNode root;
public BinaryTree() {
super();
// TODO Auto-generated constructor stub
}
public BinaryTree(TreeNode root) {
super();
this.root = root;
}
public TreeNode getRoot() {
return root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
// 前序遍歷
public static void pre_visit(TreeNode root) {
System.out.println(root);
if (root.getLeft() != null)
{
pre_visit(root.getLeft());
}
if (root.getRight() != null)
{
pre_visit(root.getRight());
}
}
// 中序遍歷
public static void infix_visit(TreeNode root) {
if (root != null)
{
if (root.getLeft() != null)
{
infix_visit(root.getLeft());
}
System.out.println(root);
if (root.getRight() != null)
{
infix_visit(root.getRight());
}
}
}
// 后續遍歷
public static void post_visit(TreeNode root) {
if (root != null)
{
if (root.getLeft() != null)
{
post_visit(root.getLeft());
}
if (root.getRight() != null)
{
post_visit(root.getRight());
}
System.out.println(root);
}
}
// 前序查找
public static TreeNode preFind(TreeNode root, int data) {
if (root != null)
{
if (root.getData() == data)
{
return root;
}
TreeNode temp = null;
if (root.getLeft() != null)
{
temp = preFind(root.getLeft(), data);
}
if (temp != null)
{
return temp;
}
if (root.getRight() != null)
{
temp = preFind(root.getRight(), data);
}
return temp;
} else
{
return null;
}
}
// 中序查找
public static TreeNode infixFind(TreeNode root, int data) {
if (root != null)
{
TreeNode temp = null;
if (root.getLeft() != null)
{
temp = infixFind(root.getLeft(), data);
}
if (temp != null)
{
return temp;
}
if (root.getData() == data)
{
return root;
}
if (root.getRight() != null)
{
temp = infixFind(root.getRight(), data);
}
return temp;
} else
{
return null;
}
}
// 后序查找
public static TreeNode postFind(TreeNode root, int data) {
if (root != null)
{
TreeNode temp = null;
if (root.getLeft() != null)
{
temp = postFind(root.getLeft(), data);
}
if (temp != null)
{
return temp;
}
if (root.getRight() != null)
{
temp = postFind(root.getRight(), data);
}
if (temp != null)
{
return temp;
}
if (root.getData() == data)
{
return root;
}
return temp;
} else
{
return null;
}
}
// 查找數據,按照二叉排序樹的情況
public TreeNode findByOrder(int key) {
TreeNode temp = this.root;
while (temp != null)
{
if (temp.getData() > key)
{
temp = temp.getLeft();
} else if (temp.getData() < key)
{
temp = temp.getRight();
} else
{
return temp;
}
}
return null;
}
// 按照二叉排序樹進行插入
public boolean insertByOrder(int data) {
TreeNode newNode = new TreeNode(data);
if (root == null)
{// 當前樹為空樹,沒有任何節點
root = newNode;
return true;
} else
{
TreeNode current = root;
TreeNode parentNode = null;
while (current != null)
{
parentNode = current;
if (current.getData() > data)
{// 當前值比插入值大,搜索左子節點
current = current.getLeft();
if (current == null)
{// 左子節點為空,直接將新值插入到該節點
parentNode.setLeft(newNode);
;
return true;
}
} else
{
current = current.getRight();
if (current == null)
{// 右子節點為空,直接將新值插入到該節點
parentNode.setRight(newNode);
return true;
}
}
}
}
return false;
}
// 查找二叉樹的最大值
public TreeNode findMax() {
TreeNode cur = this.root;
TreeNode max = this.root;
while (cur != null)
{
max = cur;
cur = cur.getRight();
}
return max;
}
// 查找二叉樹的最小值
public TreeNode findMin() {
TreeNode cur = this.root;
TreeNode min = this.root;
while (cur != null)
{
min = cur;
cur = cur.getLeft();
}
return min;
}
// 求二叉樹的高度
public static int getTreeHigh(TreeNode root) {
if (root == null)
{
return 0;
} else
{
return Math.max(getTreeHigh(root.getLeft()), getTreeHigh(root.getRight())) + 1;
}
}
// 返回二叉樹節點的數量
public static int getSize(TreeNode root) {
if (root == null)
{
return 0;
} else
{
return getSize(root.getLeft()) + getSize(root.getRight()) + 1;
}
}
// 返回葉子節點的數量
public static int getLeafSize(TreeNode root) {
if (root == null)//如果根節點為空0
{
return 0;
}
if (root.getLeft() == null && root.getRight() == null)//如果是葉子節點不存在左右子樹
{
return 1;
}
return getLeafSize(root.getLeft()) + getLeafSize(root.getRight());//左子樹的葉子+右子樹葉子的數量
}
// 銷毀二叉樹
public static void destoryTree(TreeNode root) {
while (root != null) {//當根節點不空
TreeNode left = root.getLeft();
if (left == null) {//如果左子樹為空
TreeNode right = root.getRight();//獲取右子樹
root.setRight(null);//刪除根節點
root = right;//根節點指向右子樹
} else {
root.setRight(left.getLeft());//根節點的右兒子指向左子樹
left.setRight(root);//左子樹的右子樹指向根節點
root = left;//根節點指向左子樹
}
}
}
// 刪除二叉樹節點
// root是根節點
// key是待刪除的節點的值
// 返回根節點
public static TreeNode deleteTreeNode(TreeNode root, int key) {
if (root == null)
{
return null;
}
if (key < root.getData())
{// 向左子樹進行刪除
root.setLeft(deleteTreeNode(root.getLeft(), key));
return root;
}
if (key > root.getData())
{
root.setRight(deleteTreeNode(root.getRight(), key));
return root;
}
// 開始刪除,如果是葉子節點
if (root.getLeft() == null && root.getRight() == null)
{
root = null;
return root;
}
// 待刪除節點只有右子樹
if (root.getLeft() == null && root.getRight() != null)
{
root = root.getRight();
return root;
}
// 只有左子樹
if (root.getRight() == null && root.getLeft() != null)
{
root = root.getLeft();
return root;
}
// 有兩個孩子
if (root.getLeft() != null && root.getRight() != null)
{
// 挑選左子樹中最大或者右子樹中最小的,替換當前節點,再將替換節點置空
int val = findMaxInLeftTree(root.getLeft());
root.setData(val);
root.setLeft(deleteTreeNode(root.getLeft(), key));
return root;
}
return root;
}
// 找到左子樹中最大的節點的值
private static int findMaxInLeftTree(TreeNode left) {
if (left == null)
{
return 0;
}
if (left.getRight() == null)
{
return left.getData();
}
if (left.getRight() == null && left.getLeft() == null)
{
return left.getData();
}
return findMaxInLeftTree(left.getRight());
}
// 層序打印二叉樹
public static void printTree(TreeNode root) {
if (root == null)
{
return;
}
Deque<TreeNode> prioriDeque = new LinkedList<TreeNode>();
prioriDeque.offerLast(root);
while (prioriDeque.size() != 0)
{
TreeNode pNode = prioriDeque.getFirst();
prioriDeque.pollFirst();
System.out.printf(pNode.getData() + "" + " ");
if (pNode.getLeft() != null)
{
prioriDeque.push(pNode.getLeft());
}
if (pNode.getRight() != null)
{
prioriDeque.push(pNode.getRight());
}
}
}
// 按層打印二叉樹
public static void printTreeBylayer(TreeNode root) {
if (root == null)
{
return;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
int current;// 當前層
int next;// 下一層的節點個數
queue.offer(root);
current = 1;
next = 0;
while (!queue.isEmpty())
{
TreeNode currentNode = queue.poll();
System.out.printf("%-4d", currentNode.getData());
current--;
if (currentNode.getLeft() != null)
{
queue.offer(currentNode.getLeft());
next++;
}
if (currentNode.getRight() != null)
{
queue.offer(currentNode.getRight());
next++;
}
if (current == 0)
{
System.out.println();
current = next;
next = 0;
}
}
}
// 可視化打印
public static void printBinaryTree(TreeNode root, int level) {
if (root == null)
return;
printBinaryTree(root.getRight(), level + 1);
if (level != 0)
{
for (int i = 0; i < level - 1; i++)
System.out.print("|\t");
System.out.println("|-------" + root.getData());
} else
System.out.println(root.getData());
printBinaryTree(root.getLeft(), level + 1);
}
// 二叉樹的二分查找
public static TreeNode binarySearch(TreeNode root, int key) {
if (root == null)
{
return null;
}
if (root != null)
{
if (root.getData() == key)
{
return root;
} else if (root.getData() > key)
{
return binarySearch(root.getLeft(), key);
} else
{
return binarySearch(root.getRight(), key);
}
}
return null;
}
// 二叉樹的反轉
public static void mirror(TreeNode root) {
if (root == null)
{
return;
}
if (root.getLeft() == null && root.getRight() == null)
{
return;
}
TreeNode temp = root.getLeft();
root.setLeft(root.getRight());
root.setRight(temp);
mirror(root.getLeft());
mirror(root.getRight());
}
}