常见数据结构面试题目(一)
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
};
解决思路:先搭建一棵二元查找树,每次取出上面最小的节点,即最左侧节点,并从树上删除该节点。以此将取到的最小节点添加到双向链表的末端。
源代码如下:
要求不能创建任何新的结点,只调整指针的指向。
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; } } } }
相关文章推荐
- 常见数据结构面试题目(二)
- 常见数据结构面试题目(四)
- 常见数据结构面试题目(三)
- C++面试常见题目问与答(汇总一)
- Java常见笔试面试题目解析(十):Java中检查的异常与未检查的异常
- 我思故我在系列—数据结构面试NO.24(题目搜集整理者JULY,非常感谢!!)
- 我思故我在系列—数据结构面试NO.30题(题目搜集整理者JULY,非常感谢!!)
- 数据结构常见面试(1)
- Oracle常见面试题目
- 数据结构面试之八——图的常见操作2之最短路径
- 最全常见算法工程师面试题目整理
- 面试10大算法汇总以及常见题目解答
- Java面试常见题目汇总(主要是概念)
- 常见的abap面试题目,请大家对照学习
- 面试中常见递归题目 Java版
- PHP常见面试题目深入解答分析(二)
- Android面试题目之常见的填空题和简答题
- 常见面试笔试编程题目
- Linux 笔试面试常见题目
- 大数据常见面试题目