数据结构平衡二叉树
Posted 写Bug的渣渣高
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构平衡二叉树相关的知识,希望对你有一定的参考价值。
/**
* 平衡二叉树
* 对于 BST 节点元素,一定要实现 Comparable 接口
* @param <E>
*/
public class BST<E extends Comparable<E>>
/**
* 二叉树的节点
*/
private class Node
public E e;
public Node left ,right;
public Node(E e)
this.e = e;
left = null;
right = null;
private Node root;
private int size;
public BST()
root = null;
size =0;
public void add(E e)
// if (root == null)
// root = new Node(e);
// size++;
// else
root = add(root,e);
//
/**
* 需要以node为跟然后使用递归插入元素
* @param node
* @param e
*/
private Node add(Node node ,E e )
// 不包括重复情况
// if (e.equals(node.e))
// return;
// else if (e.compareTo(node.e) < 0 && node.left == null)
// node.left = new Node(e);
// size++;
// return;
// else if (e.compareTo(node.e) > 0 && node.right ==null)
// node.right = new Node(e);
// size++;
// return;
//
// if (e.compareTo(node.e) < 0)
// add(node.left,e);
// else
// add(node.right,e);
//
/**
* 使用的时递归的方法来添加如果,如果这个节点为空,那么根据值创建一个node并返回
*/
if (node == null)
size++;
return new Node(e);
if (e.compareTo(node.e) < 0)
node.left = add(node.left,e);
else if (e.compareTo(node.e) > 0)
node.right = add(node.right,e);
return node;
public boolean contains(E e)
return contains(root,e);
// 看以node为跟的二分搜搜树是否包含元素e
private boolean contains(Node node ,E e)
if (node == null)
return false;
if (e.compareTo(node.e) == 0)
return true;
else if (e.compareTo(node.e) < 0)
contains(node.left,e);
else
contains(node.right,e);
return false;
public void preOrder()
preOrder(root);
private void preOrder(Node node)
if (node == null)
return;
System.out.println(node.e);
preOrder(node.left);
preOrder(node.right);
/**
* 非递归的前序遍历
*/
public void preOrderNR()
Stack<Node> stack = new Stack<Node>();
stack.push(root);
while (!stack.isEmpty())
Node cur = stack.pop();
System.out.println(cur.e);
if (cur.left != null)
stack.push(cur.left);
if (cur.right != null)
stack.push(cur.right);
/**
* 先释放所有孩子节点,再释放节点本身
*/
public void postOrder()
postOrder(root);
private void postOrder(Node node)
if (node == null)
return;
postOrder(node.left);
postOrder(node.right);
System.out.println(node.e);
/**
* 中序遍历就是顺序遍历
*/
public void inOrder()
inOrder(root);
private void inOrder(Node node)
if (node == null)
return;
inOrder(node.left);
System.out.println(node.e);
inOrder(node.right);
public E minMum()
if (size == 0)
throw new IllegalArgumentException("二分搜索树为空");
return minMum(root).e;
private Node minMum(Node node)
if (node.left == null)
return node;
return minMum(node.left);
/**
* 如何删除??
* 如果最小值是叶子,直接删
* 否则就删了该节点,然后把该节点右子树接到该节点双亲节点
* @return
*/
public E removeMin()
E ret = minMum();
root = removeMin(root);
return ret;
private Node removeMin(Node node)
if (node.left == null)
// 这里右子树可能为空
Node rightNode = node.right;
node.right = null;
size --;
return rightNode;
node.left = removeMin(node.left);
return node;
public E removeMax()
E ret = maxMum();
root = removeMin(root);
return ret;
private Node removeMax(Node node)
if (node.right == null)
Node rightNode = node.left;
node.right = null;
size --;
return rightNode;
node.right = removeMin(node.right);
return node;
public E maxMum()
if (size == 0)
throw new IllegalArgumentException("二分搜索树为空");
return maxMum(root).e;
private Node maxMum(Node node)
if (node.right == null)
return node;
return maxMum(node.right);
public void remove(E e)
root = remove(root,e);
private Node remove(Node node,E e)
if (node == null)
return null;
if (e.compareTo(node.e) < 0)
node.left = remove(node.left,e);
else if (e.compareTo(node.e) > 0)
node.right = remove(node.right,e);
else //e == node.e
if (node.left == null)
Node rightNode = node.right;
node.right = null;
size--;
return rightNode;
if (node.right == null)
Node leftNode = node.left;
node.left = null;
size --;
return leftNode;
// 待删除节点均不为空,找到比待删除节点大的最小节点,即待删除节点右子树的最小节点
Node successor = minMum(node.right);
successor.right = removeMin(node.right);
successor.left = node.left;
node.left = node.right = null;
return successor;
/**
* 广度优先遍历.层序遍历,需要使用到队列
*/
public void levelOrder()
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty())
Node cur = q.remove();
System.out.println(cur.e);
if (cur.left != null)
q.add(cur.left);
if (cur.right != null)
q.add(cur.right);
public int size()
return size;
public boolean isEmpty()
return size==0;
@Override
public String toString()
StringBuilder res = new StringBuilder();
generateBSTString(root,0,res);
return res.toString();
private void generateBSTString(Node root, int i, StringBuilder res)
if (root == null)
res.append(generateDepthString(i)+"null\\n");
return;
res.append(generateDepthString(i)+root.e+"\\n");
generateBSTString(root.left,i+1,res);
generateBSTString(root.right,i+1,res);
private String generateDepthString(int i)
StringBuilder res = new StringBuilder();
for (int j = 0; j < i; j++)
res.append("--");
return res.toString();
以上是关于数据结构平衡二叉树的主要内容,如果未能解决你的问题,请参考以下文章