把二元查找树转变成排序的双向链表

题目:

输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
要求不能创建任何新的结点,只调整指针的指向。
   10
 / \
 6 14
 / \ / \
4 8 12 16
 转换成双向链表
4=6=8=10=12=14=16。
 1 /**
 2  * 二叉树结点的定义
 3  * @author Burke
 4  *
 5  * @param <T>
 6  */
 7 public class BinaryTreeNode <T>{
 8     T key;
 9     BinaryTreeNode<T> leftChildTree;
10     BinaryTreeNode<T> rightChildTree;
11     public BinaryTreeNode(T key) {
12         this.key = key;
13         leftChildTree = null;
14         rightChildTree = null;
15     }
16 }
/**
     * 对于左子树返回最大值的结点
     * @param root
     * @return
     */
    public static <T> BinaryTreeNode<T>  leftChange(BinaryTreeNode<T> root){
        
        
        if (null == root || (root.leftChildTree == null && root.rightChildTree == null)) {
            return root;
        }
        
        BinaryTreeNode<T> preNode;//相对于根节点
        BinaryTreeNode<T> nextNode;
        
        //直接带入
        preNode = leftChange(root.leftChildTree);
        nextNode = rightChange(root.rightChildTree);
        ////引用的调整
        if (null != preNode) {
            preNode.rightChildTree = root;
            root.leftChildTree = preNode;
        }
        
        if(null != nextNode) {
            nextNode.leftChildTree = root;
            root.rightChildTree = nextNode;
            return nextNode;
        } else {
            return root;
        }
    }
/**
     * 对于右子树,返回最小值的结点引用
     * @param root
     * @return
     */
    public static <T> BinaryTreeNode<T> rightChange(BinaryTreeNode<T> root){
        if (null == root || (root.leftChildTree == null && root.rightChildTree == null)) {
            return root;
        }
        
        BinaryTreeNode<T> preNode;//相对于根节点
        BinaryTreeNode<T> nextNode;
        
        //直接带入
        preNode = leftChange(root.leftChildTree);
        nextNode = rightChange(root.rightChildTree);
        
        //引用的调整
        if (null != nextNode) {
            nextNode.leftChildTree = root;
            root.rightChildTree = nextNode;
            
        }
        
        if(null != preNode) {
            preNode.rightChildTree = root;
            root.leftChildTree = preNode;
            return preNode;
        } else {
            return root;
        }
    }

测试:

public static void main(String[] args) {
        
        BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(10);
        BinaryTreeNode<Integer> node6 = new BinaryTreeNode<Integer>(6);
        BinaryTreeNode<Integer> node14 = new BinaryTreeNode<Integer>(14);
        BinaryTreeNode<Integer> node4 = new BinaryTreeNode<Integer>(4);
        BinaryTreeNode<Integer> node8 = new BinaryTreeNode<Integer>(8);
        BinaryTreeNode<Integer> node12 = new BinaryTreeNode<Integer>(12);
        BinaryTreeNode<Integer> node16 = new BinaryTreeNode<Integer>(16);
        root.leftChildTree = node6;
        root.rightChildTree = node14;
        
        node6.leftChildTree = node4;
        node6.rightChildTree = node8;
        
        node14.leftChildTree = node12;
        node14.rightChildTree = node16;
        
        BinaryTreeNode<Integer> curr = root;//保存双向链表的头结点
        BinaryTreeNode<Integer> curr1 = root;//保存双向链表的尾结点
        while(curr.leftChildTree != null){
            curr = curr.leftChildTree;
        }
        System.out.println(curr.key);
        
        while(curr1.rightChildTree != null){
            curr1 = curr1.rightChildTree;
        }
        System.out.println(curr1.key);
        
        change(root);
        //从头结点输出
        while(curr != null){
            System.out.print(curr.key + " ");
            curr = curr.rightChildTree;
        }
        
        System.out.println();
        //从尾结点输出
        while(curr1 != null){
            System.out.print(curr1.key + " ");
            curr1 = curr1.leftChildTree;
        }

 

posted @ 2014-09-03 16:29  soul390  阅读(161)  评论(0)    收藏  举报