- (void)viewDidLoad {
[super viewDidLoad];
Node *node1 = [[Node alloc] init];
Node *node2 = [[Node alloc] init];
Node *node3 = [[Node alloc] init];
Node *node4 = [[Node alloc] init];
Node *node5 = [[Node alloc] init];
Node *node6 = [[Node alloc] init];
Node *node7 = [[Node alloc] init];
Node *node8 = [[Node alloc] init];
node1.value = 1;
node2.value = 2;
node3.value = 3;
node4.value = 4;
node5.value = 5;
node6.value = 6;
node7.value = 7;
node8.value = 8;
node1.left = node2;
node1.right = node3;
node2.left = node4;
node2.right = node5;
node3.left = node6;
node3.right = node7;
// node4.right = node8;
// 先序 头左右 1245367
// 中序 左头右 4251637
// 后序 左右头 4526731
// [self preOrderRecur:node1];
// [self midOrderRecur:node1];
// [self postOrderRecur:node1];
//创建二叉树
self.head = [self creatTree:@[@1,@2,@3,@4,@5,@6,@7]];
[self widthOrder:node1];
// Do any additional setup after loading the view.
}
//递归-先序遍历(头左右)
-(void)preOrderRecur:(Node *)head{
if (head == nil) {
return;
}
NSLog(@"%i",head.value);
[self preOrderRecur:head.left];
[self preOrderRecur:head.right];
}
-(Node *)creatTree:(NSArray *)arry{
static int count = 0;
if (arry.count== 0) {
return nil;
}
Node *node = [[Node alloc]init];
if (count < arry.count) {
node.value = [arry[count++] intValue];
if (node.value == 0) {
return nil;
}
node.left = [self creatTree:arry];
node.right = [self creatTree:arry];
}
return node;
}
//递归-中序遍历(左头右)
-(void)midOrderRecur:(Node *)head{
if (head == nil) {
return;
}
[self midOrderRecur:head.left];
NSLog(@"%i",head.value);
[self midOrderRecur:head.right];
}
//递归-后序遍历(左右头)
-(void)postOrderRecur:(Node *)head{
if (head == nil) {
return;
}
[self postOrderRecur:head.left];
[self postOrderRecur:head.right];
}
/**
//非递归-先序遍历(头左右)
使用栈,先进后出
1先把头节点放入站中,从栈中弹出一个节点cur
2 打印(处理)cur
3 先压cur的右节点,再压左节点(如果有的话)
4周而复始
*/
-(void)preOrderUnRecur:(Node *)head{
NSLog(@"pre-order-unrecur");
if (head != nil) {
NSMutableArray *stack = [NSMutableArray array];
[stack addObject:head];
while (stack.count) {
head = stack.lastObject;
[stack removeLastObject];
NSLog(@"%i",head.value);
if (head.right != nil) {
[stack addObject:head.right];
}
if (head.left != nil) {
[stack addObject:head.left];
}
}
}
}
/**
//非递归-后序遍历
使用两个栈,先进后出
1初始化两个栈,stack, shoustack(回收站),先把头节点放入站中,从栈中弹出一个节点cur
2 将cur放入 回收栈,
3 先压cur的左节点,再压右节点(如果有的话)
4周而复始
5打印回收栈里的节点(先进后出)
*/
-(void)postOrderUnRecur:(Node *)head{
NSLog(@"post-order-unrecur");
if (head != nil) {
NSMutableArray *stack = [NSMutableArray array];
NSMutableArray *shouStack = [NSMutableArray array];
[stack addObject:head];
while (stack.count) {
head = stack.lastObject;
[stack removeLastObject];
[shouStack addObject:head];
if (head.left != nil) {
[stack addObject:head.left];
}
if (head.right != nil) {
[stack addObject:head.right];
}
}
while (shouStack.count) {
NSLog(@"%i",((Node *)shouStack.lastObject).value);
[shouStack removeLastObject];
}
}
}
/**
//非递归-中序遍历
使用栈
1每颗子树,整棵树左边界进栈,
2然后依次弹出的过程中,打印弹出的节点,并对弹出的节点的右树进行第一步的循环
3周而复始
*/
-(void)midOrderUnRecur:(Node *)head{
NSLog(@"min-order-unrecur");
if (head != nil) {
NSMutableArray *stack = [NSMutableArray array];
while (stack.count || head!= nil) {
if (head != nil) {
[stack addObject:head];
head = head.left;
} else {
head = stack.lastObject;
[stack removeLastObject];
NSLog(@"%i",head.value);
head = head.right;
}
}
}
}
//深度优先遍历:
//对于二叉树来说,它的深度优先遍历就是它的先序遍历
/**
宽度优先遍历 :
使用队列的特性:先进先出
1 先把头结点放进队列中
2 取出一节点记作cur,然后打印cur
3 先压cur的左节点,再压cur的右节点,
4周而复始就可以了。
*/
-(void)widthOrder:(Node *)head{
if (head == nil) {
return;
}
NSMutableArray *queue = [NSMutableArray array];
[queue addObject:head];
while (queue.count) {
Node *cur = queue.firstObject;
[queue removeObjectAtIndex:0];
NSLog(@"%i",cur.value);
if (cur.left != nil) {
[queue addObject:cur.left];
}
if (cur.right != nil) {
[queue addObject:cur.right];
}
}
}
//求和
-(void)sum1:(Node *)head lastnum:(int) lastnum{
if (head == nil) {
return;
}
static int sum = 0;
int lastnumber = lastnum*10 + head.value;
if (head.left == nil && head.right == nil) {
sum+= lastnumber;
NSLog(@"求和%i",sum);
}
if (head.left != nil) {
[self sum1:head.left lastnum:lastnumber];
}
if (head.right != nil) {
[self sum1:head.right lastnum:lastnumber];
}
}
//void DepthFirstSearch(BitNode *root)
//41 {
//42 stack<BitNode*> nodeStack;
//43 nodeStack.push(root);
//44 while (!nodeStack.empty())
//45 {
//46 BitNode *node = nodeStack.top();
//47 cout << node->data << ' ';
//48 nodeStack.pop();
//49 if (node->right)
//50 {
//51 nodeStack.push(node->right);
//52 }
//53 if (node->left)
//54 {
//55 nodeStack.push(node->left);
//56 }
//57 }
//58 }
//59
//60 //广度优先搜索
//61 void BreadthFirstSearch(BitNode *root)
//62 {
//63 queue<BitNode*> nodeQueue;
//64 nodeQueue.push(root);
//65 while (!nodeQueue.empty())
//66 {
//67 BitNode *node = nodeQueue.front();
//68 cout << node->data << ' ';
//69 nodeQueue.pop();
//70 if (node->left)
//71 {
//72 nodeQueue.push(node->left);
//73 }
//74 if (node->right)
//75 {
//76 nodeQueue.push(node->right);
//77 }
//78 }
//79 }
@end