BSTree.java

package cn.com.amazon.basic.one;

import java.io.ByteArrayInputStream;  
import java.io.ByteArrayOutputStream;  
import java.io.ObjectInputStream;  
import java.io.ObjectOutputStream;  
import java.io.Serializable;

/** 
1.把二元查找树转变成排序的双向链表 
题目: 
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。 
要求不能创建任何新的结点,只调整指针的指向。 
  10 
 / \ 
 6  14 
/ \ / \ 
4  8 12 16 
转换成双向链表 
4=6=8=10=12=14=16。 
 
首先我们定义的二元查找树 节点的数据结构如下: 
struct BSTreeNode 
{ 
 int m_nValue; // value of node 
 BSTreeNode *m_pLeft; // left child of node 
 BSTreeNode *m_pRight; // right child of node 
}; 


思路汇总:

(1)每访问到一个parent,递归左子树,递归右子树,连接左子树的最左边,右子树的最右边与根节点的结合处。故递归函数必须标明当前子树左右情况。

(2)中序遍历树,记录一个pre,然后每访问到一个节点,就将其加入双向链表中,具体即为与pre的操作。
*/  


public class BSTree implements Cloneable, Serializable {
	/** 
     * v0.8 
     */  
    private static final long serialVersionUID = -7240326774488306261L;  
  
    private BSNode m_root;  // 根节点  
      
    private BSNode tempListNode;    // 创建doubleList的时候使用的临时变量  
    private BSNode tempListHead;    // 创建doubleList的时候使用的临时变量  
      
    public void setM_root(BSNode mRoot) {  
        m_root = mRoot;  
    }  
  
    public BSNode getM_root() {  
        return m_root;  
    }  
      
    /** 
     * 深拷贝 
     */  
    public Object clone() {  
        try {  
            // 没有去想树的节点拷贝算法,先用这种简便方法,再复杂的类型拷贝也不怕,不过所有相关类都必须是Serializable  
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            ObjectOutputStream oos = new ObjectOutputStream(baos);  
            oos.writeObject(this);  
            oos.close();  
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());  
            ObjectInputStream ois = new ObjectInputStream(bais);  
            Object ob = ois.readObject();  
            ois.close();  
            return (BSTree) ob;  
        } catch (Exception e) {  
            System.out.println("CloneNotSupportedException: " + e);  
            e.printStackTrace();  
        }  
        return null;  
    }  
      
    /** 
     * 增加子节点 
     * @param 二元查找树节点 
     */  
    public synchronized void addNode(BSNode node) {  
        if (null == this.m_root) {  
            this.m_root = node;  
            return;  
        }  
          
        BSNode tempNode = this.m_root;  
          
        while (true) {  
            if (node.getM_nValue() > tempNode.getM_nValue()) {   // 大于父节点  
                if (null == tempNode.getM_pRight()) {  
                    tempNode.setM_pRight(node);  
                    return;  
                } else {  
                    tempNode = tempNode.getM_pRight();  
                    continue;  
                }  
            } else if (node.getM_nValue() < tempNode.getM_nValue()) {    // 小于父节点  
                if (null == tempNode.getM_pLeft()) {  
                    tempNode.setM_pLeft(node);  
                    return;  
                } else {  
                    tempNode = tempNode.getM_pLeft();  
                    continue;  
                }  
            } else {    // 等于父节点  
                return;  
            }  
        }  
    }  
      
    /** 
     * 生成双向链表 
     * @return 双向链表 
     * @throws CloneNotSupportedException 
     */  
    public synchronized BSDoubleList changeToDoubleList() {  
        BSTree tempTree = (BSTree) this.clone();    // 临时树,替死鬼,被转换得面目全非,垃圾回收了吧...  
        // 其实改为changeTreeToDoubleList(this.getM_root());才符合题意,不过个人喜欢使用深拷贝,不破坏原来的树  
        if (null != tempTree) {  
            changeTreeToDoubleList(tempTree.getM_root());  
        }  
        BSDoubleList dlist = new BSDoubleList();  
        dlist.setHead(tempListHead);  
        return dlist;  
    }  
      
    private void changeTreeToDoubleList(BSNode node) {  
        if (null == node) {  
            return;  
        }  
        if (null != node.getM_pLeft()) {  
            changeTreeToDoubleList(node.getM_pLeft());  
        }  
        // -------------转换---------------  
        node.setM_pLeft(tempListNode);  
        if (null == tempListNode){  
            tempListHead = node;  
        } else {  
            tempListNode.setM_pRight(node);  
        }  
        //---------------------------------  
        tempListNode = node;  
        if (null != node.getM_pRight()) {  
            changeTreeToDoubleList(node.getM_pRight());  
        }  
    }  
      
      
    /** 
     * 打印中序遍历 
     */  
    public synchronized void print() {  
        if (null == this.m_root) {  
            System.out.print("HashCode: " + this.hashCode() +  "; 空树;");  
            return;  
        }  
        System.out.print("HashCode: " + this.hashCode() +  "; 树: ");  
        print(this.m_root);  
        System.out.println();  
    }  
      
    private void print(BSNode node) {  
        if (null != node) {  
            print(node.getM_pLeft());  
            System.out.print(node.getM_nValue() + " ");  
            print(node.getM_pRight());  
        }  
    }  
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * AVL树 * 基于BSTree的扩充 */ package dsa; public class AVLTree extends BSTree implements Dictionary { /**************************** 构造方法 ****************************/ public AVLTree() { super(); } public AVLTree(BinTreePosition r) { super(r); } public AVLTree(BinTreePosition r, Comparator c) { super(r, c); } /**************************** 词典方法(覆盖父类BSTree) ****************************/ //插入条目(key, value),并返回该条目 public Entry insert(Object key, Object value) { Entry e = super.insert(key, value);//调用父类方法完成插入 root = rebalance(lastV.getParent(), root);//从插入节点的父亲开始重新平衡化 return e; } //若词典中存在以key为关键码的条目,则将摘除其中的一个并返回;否则,返回null public Entry remove(Object key) { Entry e = super.remove(key);//调用父类方法完成删除 if (null != e) root = rebalance(lastV, root);//从删除节点的父亲开始重新平衡化 return e; } /**************************** 辅助方法 ****************************/ //从节点z开始,自上而下重新平衡化 //返回后,root仍为平衡后的(整棵)树的根节点 protected static BinTreePosition rebalance(BinTreePosition z, BinTreePosition r) { if (null == z) return r; while (true) {//从z开始,向上逐一检查z的祖先 if (!isBalanced(z)) rotate(z);//若z节点失去平衡,则通过旋转使之重新平衡 if (!z.hasParent()) return z; z = z.getParent();//继续检查其父亲 }//while } //判断节点v是否平衡 protected static boolean isBalanced(BinTreePosition v) { if (null == v) return true; int lH = (v.hasLChild()) ? (v.getLChild().getHeight()) : -1; int rH = (v.hasRChild()) ? (v.getRChild().getHeight()) : -1; int deltaH = lH - rH; return (-1 <= deltaH) && (deltaH <= 1); } //通过旋转,使节点z的平衡因子的绝对值不超过1(支持AVL树) //返回新的子树根 public static BinTreePosition rotate(BinTreePosition z) { BinTreePosition y = tallerChild(z);//取y为z更高的孩子 BinTreePosition x = tallerChild(y);//取x为y更高的孩子 boolean cType = z.isLChild();//记录:z是否左孩子 BinTreePosition p = z.getParent();//p为z的父亲 BinTreePosition a, b, c;//自左向右,三个节点 BinTreePosition t0, t1, t2, t3;//自左向右,四棵子树 /******** 以下分四种情况 ********/ if (y.isLChild()) {//若y是左孩子,则 c = z; t3 = z.getRChild(); if (x.isLChild()) {//若x是左孩子 b = y; t2 = y.getRChild(); a = x; t1 = x.getRChild(); t0 = (BSTreeNode)x.getLChild(); } else {//若x是右孩子 a = y; t0 = y.getLChild(); b = x; t1 = x.getLChild(); t2 = (BSTreeNode)x.getRChild(); } } else {//若y是右孩子,则 a = z; t0 = z.getLChild(); if (x.isRChild()) {//若x是右孩子 b = y; t1 = y.getLChild(); c = x; t2 = x.getLChild(); t3 = (BSTreeNode)x.getRChild(); } else {//若x是左孩子 c = y; t3 = y.getRChild(); b = x; t1 = x.getLChild(); t2 = (BSTreeNode)x.getRChild(); } } //摘下三个节点 z.secede(); y.secede(); x.secede(); //摘下四棵子树 if (null != t0) t0.secede(); if (null != t1) t1.secede(); if (null != t2) t2.secede(); if (null != t3) t3.secede(); //重新链接 a.attachL(t0); a.attachR(t1); b.attachL(a); c.attachL(t2); c.attachR(t3); b.attachR(c); //子树重新接入原树 if (null != p) if (cType) p.attachL(b); else p.attachR(b); return b;//返回新的子树根 }//rotate //返回节点p的孩子中的更高者 protected static BinTreePosition tallerChild(BinTreePosition v) { int lH = v.hasLChild() ? v.getLChild().getHeight() : -1; int rH = v.hasRChild() ? v.getRChild().getHeight() : -1; if (lH > rH) return v.getLChild(); if (lH < rH) return v.getRChild(); if (v.isLChild()) return v.getLChild(); else return v.getRChild(); } //返回节点p的孩子中的更矮者 protected static BinTreePosition shorterChild(BinTreePosition v) { int lH = v.hasLChild() ? v.getLChild().getHeight() : -1; int rH = v.hasRChild() ? v.getRChild().getHeight() : -1; if (lH > rH) return v.getRChild(); if (lH < rH) return v.getLChild(); if (v.isLChild()) return v.getRChild(); else return v.getLChild(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值