Cracking The Coding Interview4.5
//原文:
//
// Write an algorithm to find the ‘next’ node (i.e., in-order successor) of a given node in a binary search tree where each node has a link to its parent.
//
//译文:
//
// 给定二叉查找树的一个结点, 写一个算法查找它的“下一个”结点(即中序遍历后它的后继结点), 其中每个结点都有指向其父亲的链接。 // 下个节点为父节点或者右子树的最左叶子
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <list>
using namespace std; struct treenode
{
char data;
treenode * left;
treenode * right;
treenode * parent;
}; class tree
{
public: tree()
{
//root = create();
root = NULL;
index = 0;
} ~tree()
{
/***清空二叉树***/
} /***二叉排序树:插入。***/
void insert(char *s)
{
if (s == NULL)
{
return;
} int size = strlen(s);
for (int i = 0; i<size; i++)
{
insert(&root, s[i], NULL);
}
} void levelOrder()
{
queue<treenode *> q;
if (root != NULL)
{
q.push(root);
}
while(!q.empty())
{
treenode *t = q.front();
cout<<t->data<<endl;
q.pop();
if (t->left != NULL)
{
q.push(t->left);
}
if (t->right != NULL)
{
q.push(t->right);
}
}
} treenode * findNext(treenode *p)
{ if (p->right == NULL)
{
return p->parent;
}
else
{
treenode *s = p->right;
while(s->left != NULL)
{
s = s->left;
}
return s;
}
} void preOrder(){ pOrder(root);}
void inOreder(){ zOrder(root);}
void postOreder(){ hOrder(root);}
treenode *root; private: int index; void insert(treenode **p, char s, treenode *parent)
{
if (((*p) == NULL) && s != '\0')
{
*p = new treenode;
(*p)->data = s;
(*p)->left = NULL;
(*p)->right = NULL;
(*p)->parent = parent;
}
else
{
if ((*p)->data > s)
{
insert(&((*p)->left) , s, *p);
}
else
{
insert(&((*p)->right) , s, *p);
}
}
} void pOrder(treenode *p)
{
if (p==NULL)
{
return;
} cout<<p->data<<" "<<endl;
pOrder(p->left);
pOrder(p->right);
} void zOrder(treenode *p)
{
if (p==NULL)
{
return;
}
zOrder(p->left);
cout<<p->data<<" "<<endl;
zOrder(p->right);
} void hOrder(treenode *p)
{
if (p==NULL)
{
return;
}
hOrder(p->left);
cout<<p->data<<" "<<endl;
hOrder(p->right);
}
}; int main()
{
/**非递归层次遍历*************************/
tree s;
char t[8] = "3289654";
s.insert(t);
//s.levelOrder();
treenode *p =s.findNext((s.root)->right);
cout<<p->data<<endl;
return 0;
}
Cracking The Coding Interview4.5的更多相关文章
- Cracking The Coding Interview4.8
//You are given a binary tree in which each node contains a value. Design an algorithm to print all ...
- Cracking The Coding Interview4.3
//Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal h ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- 《cracking the coding intreview》——链表
前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
随机推荐
- Servlet JDBC Example
Develop a web application that should have following features. User can register and then login to t ...
- learn the python the hard way习题11~17总结
关于 input() 格式: input("prompt")功能:从 CLI 获取 User 的一个输入,显示 promt 的内容,并且返回一个 string 类型的数值其他:如果 ...
- 关于TeeChart使用我会持续更新
关于TeeChart使用我会持续更新 这篇文章中我是在Winform窗体使用TeeChart2011控件,通过定时器实现了实时绘制曲线图(三个序列). 先上一下效果图: 1.TeeChart2011. ...
- asp.net网站服务器搭建之从零开始
asp.net网站服务器搭建之从零开始 一 IIS(Internet Information Services)安装: 1.选择"控制面板". 2.点"添加或删除程序 ...
- 20170921xlVBA_SQL蒸发循环查询2
'ARRAY("1991","1992","1993","1994","1996","19 ...
- 深刻理解Web标准,对可用性、可访问性、可维护性等相关知识有实际的了解和实践经验
WEB标准不是某一个标准,而是一系列标准的集合.网页主要由三部分组成:结构(Structure).表现(Presentation)和行为(Behavior).对应的标准也分三方面:结构化标准语言主要包 ...
- linux+vs2013编译静态库和动态库
Linux下创建与使用静态库 Linux静态库命名规则 Linux静态库命名规范,必须是"lib[your_library_name].a":lib为前缀,中间是静态库名,扩展名为 ...
- python记录_day23 正则表达式 re模块
一. 正则表达式 使用python的re模块之前应该对正则表达式有一定的了解 正则表达式是对字符串操作的一种逻辑公式.我们一般使用正则表达式对字符串进行匹配和过滤. 正则的优缺点: 优点:灵活, 功能 ...
- 如何查找php-fpm监听的端口
1. 找到php的安装位置.如: /usr/local/php-7.3.3 2. 进入安装目录下的etc/php-fpm.d目录,然后你会看到: 3. 打开www.conf,搜索listen关键字,你 ...
- bzoj2300#2300. [HAOI2011]防线修建
题解:带删点的维护凸包,1.删点2.查询凸包周长 题解:倒着做就成了带加点的维护凸包,加点时维护一下周长就没了 //#pragma GCC optimize(2) //#pragma GCC opti ...