二叉排序树转化为顺序双链表 JAVA实现

package AlgorithmTest;

/**
 * Created by dell on 2015/10/7.
 */
public class BinarySortToDequeTest {
    public static void main(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.insertNode(10);
        binarySearchTree.insertNode(6);
        binarySearchTree.insertNode(4);
        binarySearchTree.insertNode(8);
        binarySearchTree.insertNode(14);
        MyLinkedList myDeque = MyLinkedList.ConvertFromBinarySortTree(binarySearchTree);
        myDeque.print();
    }
}

class MyLinkedList {
    private Node firstNode;
    public void setFirstNode(Node firstNode){
        this.firstNode = firstNode;
    }

    public void print(){
        if (firstNode != null){
            for (Node iter = firstNode; iter != null; iter = iter.nodeRight){
                System.out.println(iter.val);
            }
        }
    }
    public static MyLinkedList ConvertFromBinarySortTree(BinarySearchTree tree){
        MyLinkedList myDeque = new MyLinkedList();

        if (null != tree && tree.getRootNode() != null){
            ConvertFromBinarySortTree(tree.getRootNode());
            Node minNode = tree.getRootNode();
            while (minNode.nodeLeft != null){
                minNode = minNode.nodeLeft;
            }

            myDeque.setFirstNode(minNode);
            tree.setRootNode(null);
        }
        return myDeque;
    }

    private static void ConvertFromBinarySortTree(final Node node){
        if (node != null){
            if (node.nodeLeft != null){
              ConvertFromBinarySortTree(node.nodeLeft);
                Node leftMaxNode = node.nodeLeft;
                while (leftMaxNode.nodeRight != null){
                    leftMaxNode = leftMaxNode.nodeRight;
                }
                Node1ConnectNode2(leftMaxNode, node);
            }
            if (node.nodeRight != null){
                ConvertFromBinarySortTree(node.nodeRight);
                Node rightMinNode = node.nodeRight;
                while (rightMinNode.nodeLeft != null){
                    rightMinNode = rightMinNode.nodeLeft;
                }

                Node1ConnectNode2(node, rightMinNode);
            }
        }
    }

    private static void Node1ConnectNode2(Node node1, Node node2){
        if (node1 != null){
            node1.nodeRight = node2;
            if (node2 != null){
                node2.nodeLeft = node1;
            }
        }
    }
}

class Node{
    int val;
    Node nodeLeft;
    Node nodeRight;
    public Node(int val, Node nodeLeft, Node nodeRight){
        this.val = val;
        this.nodeLeft = nodeLeft;
        this.nodeRight = nodeRight;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public Node getNodeLeft() {
        return nodeLeft;
    }

    public void setNodeLeft(Node nodeLeft) {
        this.nodeLeft = nodeLeft;
    }

    public Node getNodeRight() {
        return nodeRight;
    }

    public void setNodeRight(Node nodeRight) {
        this.nodeRight = nodeRight;
    }

}
class BinarySearchTree {
    private Node rootNode;

    public BinarySearchTree(){
        rootNode = null;
    }

    public Node getRootNode() {
        return rootNode;
    }

    public void setRootNode(Node rootNode) {
        this.rootNode = rootNode;
    }

    public boolean insertNode(int val){
        if (null == rootNode){
            rootNode = new Node(val, null, null);
            return true;
        }
        return insertNode(rootNode, val);
    }

    public void printVals(){
        printVals(rootNode);
    }

    public boolean findVal(int val){
        return findVal(rootNode, val);
    }


    private boolean insertNode(final Node node, final int val){
        if (val == node.val){
            return false;
        }

        if (val > node.val){
            if (null == node.getNodeRight()){
                node.setNodeRight(new Node(val, null, null));
                return true;
            }else {
                return insertNode(node.getNodeRight(), val);
            }
        }

        if (val < node.val){
            if (null == node.getNodeLeft()){
                node.setNodeLeft(new Node(val, null, null));
                return true;
            }else {
                return insertNode(node.getNodeLeft(), val);
            }
        }
        return true;
    }

    private void printVals(Node node){
        if (null == node){
            return;
        }

        printVals(node.getNodeLeft());
        System.out.println(node.val);
        printVals(node.getNodeRight());
    }

    public boolean findVal(final Node node, final int val){
        if (null == node){
            return false;
        }

        if (val == node.val){
            return true;
        }

        if (val > node.val){
            return findVal(node.getNodeRight(), val);
        }

        if (val < node.val){
            return findVal(node.getNodeLeft(), val);
        }

        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值