1. //原文:
  2. //
  3. // 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.
  4. //
  5. //译文:
  6. //
  7. // 给定二叉查找树的一个结点, 写一个算法查找它的“下一个”结点(即中序遍历后它的后继结点), 其中每个结点都有指向其父亲的链接。
  8.  
  9. // 下个节点为父节点或者右子树的最左叶子
  10. #include <iostream>
  11. #include <string>
  12. #include <queue>
  13. #include <stack>
  14. #include <list>
  15. using namespace std;
  16.  
  17. struct treenode
  18. {
  19. char data;
  20. treenode * left;
  21. treenode * right;
  22. treenode * parent;
  23. };
  24.  
  25. class tree
  26. {
  27. public:
  28.  
  29. tree()
  30. {
  31. //root = create();
  32. root = NULL;
  33. index = 0;
  34. }
  35.  
  36. ~tree()
  37. {
  38. /***清空二叉树***/
  39. }
  40.  
  41. /***二叉排序树:插入。***/
  42. void insert(char *s)
  43. {
  44. if (s == NULL)
  45. {
  46. return;
  47. }
  48.  
  49. int size = strlen(s);
  50. for (int i = 0; i<size; i++)
  51. {
  52. insert(&root, s[i], NULL);
  53. }
  54. }
  55.  
  56. void levelOrder()
  57. {
  58. queue<treenode *> q;
  59. if (root != NULL)
  60. {
  61. q.push(root);
  62. }
  63. while(!q.empty())
  64. {
  65. treenode *t = q.front();
  66. cout<<t->data<<endl;
  67. q.pop();
  68. if (t->left != NULL)
  69. {
  70. q.push(t->left);
  71. }
  72. if (t->right != NULL)
  73. {
  74. q.push(t->right);
  75. }
  76. }
  77. }
  78.  
  79. treenode * findNext(treenode *p)
  80. {
  81.  
  82. if (p->right == NULL)
  83. {
  84. return p->parent;
  85. }
  86. else
  87. {
  88. treenode *s = p->right;
  89. while(s->left != NULL)
  90. {
  91. s = s->left;
  92. }
  93. return s;
  94. }
  95. }
  96.  
  97. void preOrder(){ pOrder(root);}
  98. void inOreder(){ zOrder(root);}
  99. void postOreder(){ hOrder(root);}
  100. treenode *root;
  101.  
  102. private:
  103.  
  104. int index;
  105.  
  106. void insert(treenode **p, char s, treenode *parent)
  107. {
  108. if (((*p) == NULL) && s != '\0')
  109. {
  110. *p = new treenode;
  111. (*p)->data = s;
  112. (*p)->left = NULL;
  113. (*p)->right = NULL;
  114. (*p)->parent = parent;
  115. }
  116. else
  117. {
  118. if ((*p)->data > s)
  119. {
  120. insert(&((*p)->left) , s, *p);
  121. }
  122. else
  123. {
  124. insert(&((*p)->right) , s, *p);
  125. }
  126. }
  127. }
  128.  
  129. void pOrder(treenode *p)
  130. {
  131. if (p==NULL)
  132. {
  133. return;
  134. }
  135.  
  136. cout<<p->data<<" "<<endl;
  137. pOrder(p->left);
  138. pOrder(p->right);
  139. }
  140.  
  141. void zOrder(treenode *p)
  142. {
  143. if (p==NULL)
  144. {
  145. return;
  146. }
  147. zOrder(p->left);
  148. cout<<p->data<<" "<<endl;
  149. zOrder(p->right);
  150. }
  151.  
  152. void hOrder(treenode *p)
  153. {
  154. if (p==NULL)
  155. {
  156. return;
  157. }
  158. hOrder(p->left);
  159. cout<<p->data<<" "<<endl;
  160. hOrder(p->right);
  161. }
  162. };
  163.  
  164. int main()
  165. {
  166. /**非递归层次遍历*************************/
  167. tree s;
  168. char t[8] = "3289654";
  169. s.insert(t);
  170. //s.levelOrder();
  171. treenode *p =s.findNext((s.root)->right);
  172. cout<<p->data<<endl;
  173. return 0;
  174. }

Cracking The Coding Interview4.5的更多相关文章

  1. Cracking The Coding Interview4.8

    //You are given a binary tree in which each node contains a value. Design an algorithm to print all ...

  2. Cracking The Coding Interview4.3

    //Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal h ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  5. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  6. 《cracking the coding intreview》——链表

    前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...

  7. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  8. 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 ...

  9. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

随机推荐

  1. (转)基于C#的socket编程的TCP异步实现

    一.摘要 本篇博文阐述基于TCP通信协议的异步实现. 二.实验平台 Visual Studio 2010 三.异步通信实现原理及常用方法 3.1 建立连接 在同步模式中,在服务器上使用Accept方法 ...

  2. Polygenic score

    We estimate the maximum prediction accuracy for the risk of Alzheimer's disease based on disease pre ...

  3. Bulk RNA-Seq转录组学习

    与之对应的是single cell RNA-Seq,后面也会有类似文章. 参考:https://github.com/xuzhougeng/Learn-Bioinformatics/ 作业:RNA-s ...

  4. .net WinForm 的数据绑定

    .net WinForm 的数据绑定相当灵活 http://www.cnblogs.com/ydong/archive/2006/04/22/381847.html 原来只知道 Control 类上的 ...

  5. LeetCode--155--最小栈(java版)

    设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. ...

  6. 20190102xlVBA_多表按姓名同时拆分

    Sub 多表按姓名同时拆分20190102() AppSettings Dim StartTime As Variant Dim UsedTime As Variant StartTime = VBA ...

  7. 1. windows 下redis数据库的安装

    安装 window系统的redis是微软团队根据官方的linux版本高仿的 官方原版: https://redis.io/ 中文官网:http://www.redis.cn 下载地址: https:/ ...

  8. Alyona and a tree CodeForces - 739B (线段树合并)

    大意: 给定有根树, 每个点$x$有权值$a_x$, 对于每个点$x$, 求出$x$子树内所有点$y$, 需要满足$dist(x,y)<=a_y$. 刚开始想错了, 直接打线段树合并了..... ...

  9. 『TensotFlow』RNN中文文本_下_暨研究生开学感想

    承前 接上节代码『TensotFlow』RNN中文文本_上, import numpy as np import tensorflow as tf from collections import Co ...

  10. [java]转:String Date Calendar之间的转换

    String Date Calendar之间的转换 String Date Calendar  1.Calendar 转化 String Calendar calendat = Calendar.ge ...