微软等公司数据结构面试题1

 

tree.h
typedef struct BSTreeNode {
int m_value;
struct BSTreeNode *m_left ;
struct BSTreeNode *m_right;
} bstTreeNode;
class ActTree{
public:
ActTree();
void InsertValue() ;
void DeleteValue() ;
void Sort();
void PrintTree();
void PrintNode(bstTreeNode *node);
void ChangeDoubleList();
void Change(bstTreeNode *node);
void addlist(bstTreeNode *node);
void PrintList();
private:
bstTreeNode *currentList;
bstTreeNode *root;
bstTreeNode *first;
};
tree.cpp
#include "tree.h"
#include <cstdlib>
#include <iostream>
ActTree::ActTree()
{
root = NULL;
first = NULL;
// std::cout<<"construction error!"<<std::endl;
}
void ActTree::InsertValue()
{
int temp,flag = 0;
std::cout<<"Input  ";
std::cin>>temp;
bstTreeNode *pnow  = root;
bstTreeNode *pparent  = root;
while( pnow ) {
if( pnow->m_value == temp ){
flag = 1;
pnow = NULL;
std::cout<<"%d has existed!"<<std::endl;
}
else if( pnow->m_value > temp ) {
pparent = pnow ;
pnow = pnow ->m_left ;
}
else {
pparent = pnow ;
pnow = pnow ->m_right ;
}
}
if(!flag) {
if( bstTreeNode *ptemp = (bstTreeNode *) malloc(sizeof (bstTreeNode))) {
ptemp->m_value = temp;
ptemp->m_left = ptemp->m_right = NULL;
if (pparent)
{
if( pparent->m_value > temp ) 
pparent->m_left =ptemp ;
else 
pparent->m_right =ptemp ;
}
else {
root = ptemp;
}
}
else
std::cerr<<"malloc failed!"<<std::endl;
}
}
void ActTree::PrintTree()
{
PrintNode(root);
}
void ActTree::PrintNode(bstTreeNode *node)
{
if( node ){
PrintNode(node->m_left);
std::cout<<node->m_value<<" ";
PrintNode(node->m_right);
}
}
void ActTree::addlist(bstTreeNode *node)
{
if (!first) {
first = node ;
currentList =first;
first->m_left = NULL;
first->m_right = NULL;
}
else {
currentList->m_right = node;
node->m_left =currentList;
node->m_right =NULL;
currentList = node;
}
}
void ActTree::Change(bstTreeNode *node)
{
if (node) {
Change(node->m_left);
bstTreeNode * right = node ->m_right;
addlist(node);
Change(right);
}
else
return ;
}
void ActTree::ChangeDoubleList()
{
this->Change(root);
}
void ActTree::PrintList()
{
bstTreeNode * node = first;
while(node) {
std::cout<<node->m_value<<"=";
node = node->m_right;
}
}
main.cpp
#include "tree.h"
#include <iostream>
int main()
{
ActTree test ;
int select;
//while (std::cin<<select )
test.InsertValue();
test.InsertValue();
test.InsertValue();
test.InsertValue();
test.PrintTree();
std::cout<<std::endl;
test.ChangeDoubleList();
test.PrintList();
getchar();
getchar();
return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CSND的数据结构面试题往往涵盖了各种经典的问题,需要在有限的时间内展示出自己的理论知识和解决问题的能力。回答这类问题时,通常需要从以下几个方面来展开回答。 首先,对于常见的数据结构,如数组、链表、栈、队列、树、图等,需要了解它们的特性、操作以及常见的应用场景。这样一来,在面试题中能够很好地判断使用何种数据结构,并理解其优缺点及适用范围。 其次,应对针对具体数据结构的问题,我们应该熟悉并能够灵活运用各种操作和算法,如遍历、插入、删除、查找等。例如,对于链表,我们需要清楚如何反转链表、检测环、寻找中间节点等;对于树,我们需要熟悉二叉树的遍历方式,以及常见的二叉搜索树的操作和平衡二叉树的实现。 此外,还需了解常用的排序和查找算法,如快速排序、归并排序、二分查找等,并熟悉它们的时间复杂度和空间复杂度,以及在不同情况下的使用场景。 最后,面试官可能会要求我们在具体场景中应用数据结构来解决问题,并分析算法的效率。在这种情况下,我们需要将问题抽象成相应的数据结构,然后给出解决方案,并且合理解释算法的复杂度,并认真考虑算法的边缘情况。 总之,在面试过程中,要全面展示自己对于数据结构的理解和应用能力,同时要注重理论知识的掌握和实际问题解决能力的展示,这样才能在数据结构面试中脱颖而出。 ### 回答2: 数据结构是计算机科学中的重要基础知识,面试中经常会涉及到相关问题。以下是关于“数据结构面试题”的回答。 数据结构面试题通常包括以下内容:数组、链表、栈、队列、树、图、哈希表等。对于数组,常见的问题包括数组的查找、插入、删除操作,以及数组的有序性,如二分查找等。链表问题包括链表的初始化、插入、删除操作,以及链表的环检测等。栈和队列问题主要考察栈和队列的基本操作,如压栈、出栈、入队、出队等。 树问题是数据结构面试中的重点,包括二叉树、平衡树、二叉搜索树等。常见问题包括二叉树的遍历(前序、中序、后序)、树的深度和高度、树的层次遍历等。对于图问题,主要考察图的遍历(深度优先搜索、广度优先搜索)、最短路径问题(Dijkstra算法、Floyd-Warshall算法)以及最小生成树问题(Prim算法、Kruskal算法)等。哈希表问题主要考察哈希函数的设计、哈希冲突的解决方法,以及哈希表的性能分析等。 在准备数据结构面试时,需要牢固掌握各种数据结构的基本操作,理解其原理和实现方式,并能够熟练应用到具体问题中。另外,面试过程中还需要注重对算法的分析和优化思路的展示,展示自己对问题的思考能力和解决能力。在回答问题时要清晰表达自己的思路,思路清晰、逻辑严谨的回答更容易给面试官留下好印象。 最后,通过刷题、模拟面试等多种方式来增强自己的实际操作能力和解决问题的能力可以更好地应对数据结构面试。多练习、多总结可以提高自己的数据结构和算法水平,为面试打下坚实基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值