一.文件树
每个结点的子结点不一样,使用链表存储节约空间。
package my;
import java.awt.print.Printable;
public class Test
{
public static void main(String[] args){
TreeNode root = new TreeNode("usr");
construct(root);
print(root);
}
static class TreeNode{
public Object element;
public TreeNode firstChild;
public TreeNode nextSibling;
public int flag = 0;
public TreeNode(Object e){
this.element = e;
}
}
public static void construct(TreeNode root){
TreeNode r_1_1 = new TreeNode("mark");
TreeNode r_1_2 = new TreeNode("alex");
TreeNode r_1_3 = new TreeNode("bill");
root.firstChild = r_1_1;
r_1_1.nextSibling = r_1_2;
r_1_2.nextSibling = r_1_3;
TreeNode r_2_1 = new TreeNode("book");
TreeNode r_2_2 = new TreeNode("course");
TreeNode r_2_3 = new TreeNode("work");
TreeNode r_2_4 = new TreeNode("course");
r_1_1.firstChild = r_2_1;
r_2_1.nextSibling = r_2_2;
r_2_2.nextSibling = r_2_3;
r_1_3.firstChild = r_2_4;
TreeNode r_3_1 = new TreeNode("ch1.r");
TreeNode r_3_2 = new TreeNode("ch2.r");
TreeNode r_3_3 = new TreeNode("ch3.r");
TreeNode r_3_4 = new TreeNode("cop3212");
r_2_1.firstChild = r_3_1;
r_3_1.nextSibling = r_3_2;
r_3_2.nextSibling = r_3_3;
r_2_4.firstChild = r_3_4;
}
//每个结点有两个链表,先遍历儿子,再遍历兄弟
public static void print(TreeNode root){
if(root.flag == 0){
System.out.println(root.element);
root.flag = 1;
}
TreeNode firstChild = root.firstChild;
TreeNode nextSibling = root.nextSibling;
while(firstChild != null){
print(firstChild);
firstChild = firstChild.firstChild;
}
while(nextSibling != null){
print(nextSibling);
nextSibling = nextSibling.nextSibling;
}
}
}
二.二叉查找树
对于每个节点X,左子树的值都小于X的值,右子树的值都大于X的值。
package my;
import java.util.PrimitiveIterator.OfDouble;
public class BinarySearchTest
{
public static void main(String[] args)
{
BinarySearchTree tree = new BinarySearchTree();
tree.insert(6);
tree.insert(2);
tree.insert(1);
tree.insert(5);
tree.insert(3);
tree.insert(4);
tree.insert(8);
tree.printTree();
tree.remove(2);
tree.printTree();
}
/* 节点 */
static class BinaryNode{
Comparable element;
BinaryNode left;
BinaryNode right;
public BinaryNode(Comparable theElelment){
this(theElelment, null,null);
}
public BinaryNode(Comparable theElelment,BinaryNode lt,BinaryNode rt){
this.element = theElelment;
this.left = lt;
this.right = rt;
}
}
/* 二叉查找树 */
static class BinarySearchTree{
private BinaryNode root; // The root
private Comparable elementAt(BinaryNode t){
return t == null ? null : t.element;
}
public void makeEmpty(){
root = null;
}
public boolean isEmpty(){
return (root == null) ? true : false;
}
public Comparable find(Comparable x){
return elementAt(find(x, root));
}
public Comparable findMin(){
return elementAt(findMin( root));
}
public Comparable findMax(){
return elementAt(findMax( root));
}
public void insert(Comparable x){
root = insert(x, root);
}
public void remove(Comparable x){
root = remove(x, root);
}
public void printTree(){
if( isEmpty() )
{
System.out.println("Empty tree");
}
else PrintTree(root);
}
/**
* 查找节点
* @param x 要查找节点所在的值
* @param t 要开始查找的根节点
* @return
*/
private BinaryNode find(Comparable x,BinaryNode t){
if(t == null){
return null;
}
if(x.compareTo(t.element) < 0){
return find(x, t.left);
}
else if (x.compareTo(t.element) >0) {
return find(x, t.right);
}
else {
return t;
}
}
/**
* 递归查找t节点下的最小值
* @param t
* @return 最小值所在节点
*/
private BinaryNode findMin(BinaryNode t){
if(t == null){
return null;
}
else if (t.left == null) {
return t;
}
return findMin(t.left);
}
/**
* 非递归查找t节点下的最大值
* @param t
* @return 最大值所在节点
*/
private BinaryNode findMax(BinaryNode t){
if(t != null){
while(t.right != null){
t = t.right;
}
}
return t;
}
/**
* 插入节点
* @param x 要插入的值
* @param t 根树
* @return
*/
private BinaryNode insert(Comparable x,BinaryNode t){
if(t == null){
t = new BinaryNode(x,null,null);
}
else if (x.compareTo(t.element) < 0) {
t.left = insert(x, t.left);
}
else if (x.compareTo(t.element) > 0){
t.right = insert(x, t.right);
}
return t;
}
/**
* 删除x元素
* @param x
* @param t
* @return
*/
private BinaryNode remove(Comparable x,BinaryNode t){
if( t==null){
return t;
}
if(x.compareTo(t.element) < 0){
t.left = remove(x, t.left);
}
else if(x.compareTo(t.element) > 0){
t.right = remove(x, t.right);
}
else if(t.left != null && t.right != null){
t.element = findMin(t.right).element;
t.right = remove(t.element, t.right);
}
else {
t = (t.left != null) ? t.left : t.right;
}
return t;
}
private void PrintTree(BinaryNode root){
if(root != null){
PrintTree(root.left);
System.out.println(root.element);
PrintTree(root.right);
}
}
}
}
**删除:**分为两种情况,一是删除点只有一个子树,二是删除点有两个子树。
上图,删除点只有一个子树。
思路:递归从左到右删除,找到删除点,父节点的引用指向子树。
上图,删除点有两个子树。
思路:递归从左到右删除,找到删除点,将删除点的值替换为删除点右子树下的最小值,再递归删除最小值所在的节点。
三.AVL树
AVL树是带有平衡条件的二叉查找树。
特点:
- 本身首先是一棵二叉查找树。
- 带有平衡条件:每个结点的左右子树的高度之差的绝对值(平衡因子)最多为1。
增加或删除节点有可能使树不平衡。
对于插入节点造成有4种情况会引起不平衡。
package my;
import javax.security.auth.kerberos.KerberosKey;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;
public class AclTest
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
}
static class AvlNode{
Comparable element;
AvlNode left;
AvlNode right;
int height;
public AvlNode(Comparable element){
this(element,null,null);
}
public AvlNode(Comparable element,AvlNode left, AvlNode right){
this.element = element;
this.left = left;
this.right = right;
this.height = 0;
}
/**
* 计算节点t的高度
* @param t
* @return
*/
private static int height(AvlNode t){
return t == null ? -1:t.height;
}
private AvlNode insert(Comparable x,AvlNode t){
if(t==null){
t = new AvlNode(x,null,null);
}
else if(x.compareTo(t.element) < 0){
t.left = insert(x, t.left);
if( height(t.left) - height(t.right) == 2){
if(x.compareTo(t.left.element) < 0){
t = rorateWithLeftChild(t);
}
else t=doubleWithLeftChild(t);
}
}
else if(x.compareTo(t.element) >0){
t.right = insert(x, t.right);
if( height(t.right) - height(t.left) == 2){
if(x.compareTo(t.right.element) > 0){
t = rorateWithRightChild(t);
}
else t=doubleWithRightChild(t);
}
}
else;
t.height = Math.max(height(t.left), height(t.right)) + 1;
return t;
}
/**
* 单旋转 左-左
* @param k2
* @return
*/
private static AvlNode rorateWithLeftChild(AvlNode k2){
AvlNode k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = Math.max(height(k2.left),height(k2.right));
k1.height = Math.max(height(k1.left),k2.height);
return k1;
}
/**
* 单旋转 右-右
* @param k1
* @return
*/
private static AvlNode rorateWithRightChild(AvlNode k1){
AvlNode k2 = k1.right;
k2.left = k1;
k1.right = k2.left;
k1.height = Math.max(height(k1.left),height(k1.right));
k2.height = Math.max(height(k2.right),k1.height);
return k2;
}
/**
* 双旋转 右-左
* @param k3
* @return
*/
private static AvlNode doubleWithLeftChild(AvlNode k3){
k3.right = rorateWithRightChild(k3.right);
return rorateWithLeftChild(k3);
}
/**
* 双旋转 左-右
* @param k4
* @return
*/
private static AvlNode doubleWithRightChild(AvlNode k4){
k4.left = rorateWithRightChild(k4.left);
return rorateWithLeftChild(k4);
}
}
}