您的位置:首页 > 理论基础 > 数据结构算法

常见数据结构面试题目(一)

2015-03-19 21:41 351 查看
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

};

解决思路:先搭建一棵二元查找树,每次取出上面最小的节点,即最左侧节点,并从树上删除该节点。以此将取到的最小节点添加到双向链表的末端。

源代码如下:

public class DataStruct {
//插入二元查找树
public static Node insertNode(Node nds ,Node nd){

Node start = nds;
Node pr = null;
boolean isRight = false;;
while(nds != null){
if(nds.getValue() > nd.getValue()){
pr = nds;
nds = nds.getLeft();
isRight = false;
}else{
pr = nds;
nds = nds.getRight();
isRight = true;
}
}
if(!isRight){
pr.setLeft(nd);
}else{
pr.setRight(nd);
}
return start;
}
//删除二叉查找树上的最小节点
public static List<Node> deleteMin(Node nds){

List<Node> list = new ArrayList<Node>();
if(nds.getLeft() == null){
Node min = nds;
nds = nds.getRight();
list.add(nds);
list.add(min);
return list;
}else{

Node minP = getMinP(nds);
Node min = minP.getLeft();
minP.setLeft(null);

list.add(nds);
list.add(min);
return list;
}
}
//获得最大节点的父节点
public static Node getMaxP(Node nds){

Node p = null;
while(nds.getRight() != null){
p = nds;
nds = nds.getRight();
}
return p;
}
//获得最小节点的父节点
public static Node getMinP(Node nds){

Node p = null;
while(nds.getLeft() != null){
p = nds;
nds = nds.getLeft();
}
return p;
}
public static void printMiddle(Node nds){

if(nds.getLeft() == null){
            System.out.print(nds.getValue() + "   ");
        }else{
            Node left = nds.getLeft();
            Node right = nds.getRight();
            printMiddle(left);
            System.out.print(nds.getValue() + "   ");
            if(right != null){
                printMiddle(right);
            }
        }    
}

public static void main(String[] args){

Node start = new Node(10);
            Node n2 = new Node(14);
            Node n3 = new Node(6);
            Node n4 = new Node(4);
            Node n5 = new Node(8);

            Node n6 = new Node(12);
            Node n7 = new Node(16);
        
            insertNode(start, n2);
            insertNode(start, n3);
            insertNode(start, n4);
            insertNode(start, n5);
            insertNode(start, n6);
            insertNode(start, n7);
            //输出二元树的顺序排列
            printMiddle(start);    
       
            Node nodeStart = null;
            Node node = null;
            while(start != null){
            
                List<Node> list = deleteMin(start);
                start = list.get(0);
                Node min = list.get(1);
                System.out.println(min.getValue());

                if(nodeStart == null){
                    nodeStart = min;
                    node = nodeStart;
                }else{
                    node.setRight(min);
                    min.setLeft(node);
                    node = min;
                }
            }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: