1. - (void)viewDidLoad {
    2. [super viewDidLoad];
    3. Node *node1 = [[Node alloc] init];
    4. Node *node2 = [[Node alloc] init];
    5. Node *node3 = [[Node alloc] init];
    6. Node *node4 = [[Node alloc] init];
    7. Node *node5 = [[Node alloc] init];
    8. Node *node6 = [[Node alloc] init];
    9. Node *node7 = [[Node alloc] init];
    10. Node *node8 = [[Node alloc] init];
    11. node1.value = 1;
    12. node2.value = 2;
    13. node3.value = 3;
    14. node4.value = 4;
    15. node5.value = 5;
    16. node6.value = 6;
    17. node7.value = 7;
    18. node8.value = 8;
    19. node1.left = node2;
    20. node1.right = node3;
    21. node2.left = node4;
    22. node2.right = node5;
    23. node3.left = node6;
    24. node3.right = node7;
    25. // node4.right = node8;
    26. // 先序 头左右 1245367
    27. // 中序 左头右 4251637
    28. // 后序 左右头 4526731
    29. // [self preOrderRecur:node1];
    30. // [self midOrderRecur:node1];
    31. // [self postOrderRecur:node1];
    32. //创建二叉树
    33. self.head = [self creatTree:@[@1,@2,@3,@4,@5,@6,@7]];
    34. [self widthOrder:node1];
    35. // Do any additional setup after loading the view.
    36. }
    37. //递归-先序遍历(头左右)
    38. -(void)preOrderRecur:(Node *)head{
    39. if (head == nil) {
    40. return;
    41. }
    42. NSLog(@"%i",head.value);
    43. [self preOrderRecur:head.left];
    44. [self preOrderRecur:head.right];
    45. }
    46. -(Node *)creatTree:(NSArray *)arry{
    47. static int count = 0;
    48. if (arry.count== 0) {
    49. return nil;
    50. }
    51. Node *node = [[Node alloc]init];
    52. if (count < arry.count) {
    53. node.value = [arry[count++] intValue];
    54. if (node.value == 0) {
    55. return nil;
    56. }
    57. node.left = [self creatTree:arry];
    58. node.right = [self creatTree:arry];
    59. }
    60. return node;
    61. }
    62. //递归-中序遍历(左头右)
    63. -(void)midOrderRecur:(Node *)head{
    64. if (head == nil) {
    65. return;
    66. }
    67. [self midOrderRecur:head.left];
    68. NSLog(@"%i",head.value);
    69. [self midOrderRecur:head.right];
    70. }
    71. //递归-后序遍历(左右头)
    72. -(void)postOrderRecur:(Node *)head{
    73. if (head == nil) {
    74. return;
    75. }
    76. [self postOrderRecur:head.left];
    77. [self postOrderRecur:head.right];
    78. }
    79. /**
    80. //非递归-先序遍历(头左右)
    81. 使用栈,先进后出
    82. 1先把头节点放入站中,从栈中弹出一个节点cur
    83. 2 打印(处理)cur
    84. 3 先压cur的右节点,再压左节点(如果有的话)
    85. 4周而复始
    86. */
    87. -(void)preOrderUnRecur:(Node *)head{
    88. NSLog(@"pre-order-unrecur");
    89. if (head != nil) {
    90. NSMutableArray *stack = [NSMutableArray array];
    91. [stack addObject:head];
    92. while (stack.count) {
    93. head = stack.lastObject;
    94. [stack removeLastObject];
    95. NSLog(@"%i",head.value);
    96. if (head.right != nil) {
    97. [stack addObject:head.right];
    98. }
    99. if (head.left != nil) {
    100. [stack addObject:head.left];
    101. }
    102. }
    103. }
    104. }
    105. /**
    106. //非递归-后序遍历
    107. 使用两个栈,先进后出
    108. 1初始化两个栈,stack, shoustack(回收站),先把头节点放入站中,从栈中弹出一个节点cur
    109. 2 将cur放入 回收栈,
    110. 3 先压cur的左节点,再压右节点(如果有的话)
    111. 4周而复始
    112. 5打印回收栈里的节点(先进后出)
    113. */
    114. -(void)postOrderUnRecur:(Node *)head{
    115. NSLog(@"post-order-unrecur");
    116. if (head != nil) {
    117. NSMutableArray *stack = [NSMutableArray array];
    118. NSMutableArray *shouStack = [NSMutableArray array];
    119. [stack addObject:head];
    120. while (stack.count) {
    121. head = stack.lastObject;
    122. [stack removeLastObject];
    123. [shouStack addObject:head];
    124. if (head.left != nil) {
    125. [stack addObject:head.left];
    126. }
    127. if (head.right != nil) {
    128. [stack addObject:head.right];
    129. }
    130. }
    131. while (shouStack.count) {
    132. NSLog(@"%i",((Node *)shouStack.lastObject).value);
    133. [shouStack removeLastObject];
    134. }
    135. }
    136. }
    137. /**
    138. //非递归-中序遍历
    139. 使用栈
    140. 1每颗子树,整棵树左边界进栈,
    141. 2然后依次弹出的过程中,打印弹出的节点,并对弹出的节点的右树进行第一步的循环
    142. 3周而复始
    143. */
    144. -(void)midOrderUnRecur:(Node *)head{
    145. NSLog(@"min-order-unrecur");
    146. if (head != nil) {
    147. NSMutableArray *stack = [NSMutableArray array];
    148. while (stack.count || head!= nil) {
    149. if (head != nil) {
    150. [stack addObject:head];
    151. head = head.left;
    152. } else {
    153. head = stack.lastObject;
    154. [stack removeLastObject];
    155. NSLog(@"%i",head.value);
    156. head = head.right;
    157. }
    158. }
    159. }
    160. }
    161. //深度优先遍历:
    162. //对于二叉树来说,它的深度优先遍历就是它的先序遍历
    163. /**
    164. 宽度优先遍历 :
    165. 使用队列的特性:先进先出
    166. 1 先把头结点放进队列中
    167. 2 取出一节点记作cur,然后打印cur
    168. 3 先压cur的左节点,再压cur的右节点,
    169. 4周而复始就可以了。
    170. */
    171. -(void)widthOrder:(Node *)head{
    172. if (head == nil) {
    173. return;
    174. }
    175. NSMutableArray *queue = [NSMutableArray array];
    176. [queue addObject:head];
    177. while (queue.count) {
    178. Node *cur = queue.firstObject;
    179. [queue removeObjectAtIndex:0];
    180. NSLog(@"%i",cur.value);
    181. if (cur.left != nil) {
    182. [queue addObject:cur.left];
    183. }
    184. if (cur.right != nil) {
    185. [queue addObject:cur.right];
    186. }
    187. }
    188. }
    189. //求和
    190. -(void)sum1:(Node *)head lastnum:(int) lastnum{
    191. if (head == nil) {
    192. return;
    193. }
    194. static int sum = 0;
    195. int lastnumber = lastnum*10 + head.value;
    196. if (head.left == nil && head.right == nil) {
    197. sum+= lastnumber;
    198. NSLog(@"求和%i",sum);
    199. }
    200. if (head.left != nil) {
    201. [self sum1:head.left lastnum:lastnumber];
    202. }
    203. if (head.right != nil) {
    204. [self sum1:head.right lastnum:lastnumber];
    205. }
    206. }
    207. //void DepthFirstSearch(BitNode *root)
    208. //41 {
    209. //42 stack<BitNode*> nodeStack;
    210. //43 nodeStack.push(root);
    211. //44 while (!nodeStack.empty())
    212. //45 {
    213. //46 BitNode *node = nodeStack.top();
    214. //47 cout << node->data << ' ';
    215. //48 nodeStack.pop();
    216. //49 if (node->right)
    217. //50 {
    218. //51 nodeStack.push(node->right);
    219. //52 }
    220. //53 if (node->left)
    221. //54 {
    222. //55 nodeStack.push(node->left);
    223. //56 }
    224. //57 }
    225. //58 }
    226. //59
    227. //60 //广度优先搜索
    228. //61 void BreadthFirstSearch(BitNode *root)
    229. //62 {
    230. //63 queue<BitNode*> nodeQueue;
    231. //64 nodeQueue.push(root);
    232. //65 while (!nodeQueue.empty())
    233. //66 {
    234. //67 BitNode *node = nodeQueue.front();
    235. //68 cout << node->data << ' ';
    236. //69 nodeQueue.pop();
    237. //70 if (node->left)
    238. //71 {
    239. //72 nodeQueue.push(node->left);
    240. //73 }
    241. //74 if (node->right)
    242. //75 {
    243. //76 nodeQueue.push(node->right);
    244. //77 }
    245. //78 }
    246. //79 }
    247. @end