First traversal of a binary tree depth and breadth-first traversal of Man is her own worst enemy

Biggest enemy is ourselves

Recently reported that the 21 days of punch card geeks time, today is the seventh day Sunday, I feel the time is the most difficult insist on Saturday, because they want to take advantage of this algorithm punch training camp practice a bit, though attended, and Finally graduated, but he is still feeling commonly used algorithms and data structures smattering smattering of evaluation are a bit high, it should be less solution, so to review and consolidate it

From the beginning of recursion, recursive main idea is:

  • Find repetitive, resist the temptation of human flesh recursive
  • The easiest way to find the nearest, which is broken down into problem (repeats question) can be repeated to solve
  • Mathematical induction thinking

Then to the tree, binary tree preamble, in sequence, after traversal, which requires the use of stacks and queues, queue especially appreciated that no code read, or that do not understand the use of the binary tree traversal queue, it should be understood that there is no implementation and use of queues, because using c write algorithms and data structures, so they did not get to the essence of the queue (from here you can see their own infrastructure is relatively weak, so they need a lot of practice, lots of practice)

You need to use the queue when still feel more difficult, including when you want to give up, but give it up, the next time you meet to it or not, still do not know how to write, so bite teeth, overcome off it will be much better next time. Also you must know what you want, you just might to where you want. The same is true for the program, you must know and understand what effect you want to achieve, and then to analyze, disassemble, debug, feedback, optimization, it is possible to write code that you want.

Why do people say that the biggest enemy is yourself?

In times of difficulties or knowledge not of the heart, there are two that you, the one you say: this put off to the back face will slowly learn, the one you say: You put this behind you still will not, it is better to get rid of it the moment to learn, of course, in reality the majority of the latter, of course, if you go beyond your current level of knowledge is particularly difficult to learn things when I suggest you choose the former, if the problem you jump it may encounter, be sure to choose the latter, it is currently rid of, fix out. This latter fact is human laziness.

There are not greedy, good things encountered, both want A also want to B, you know he did not have so much time and effort you want, put their best time and energy left for the most important person and things!

code show as below:

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 100
#define OK 1
#define ERROR 0

struct TreeNode {
   int val;
   struct TreeNode *left;
   struct TreeNode *right;
}TreeNode;

typedef struct {
   struct TreeNode* data[MAXSIZE];
   int front;
   int rear;
}SqQueue;


struct TreeNode* initTree() {
   struct TreeNode *root;
   int data;
   scanf("%d",&data);

   if (data == 0) {
    root = NULL;
    return root;
   }else {
    root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    if (!root) {
       return NULL;
    }
    root->val = data;
    printf("输入%d的左节点", data);
    root->left = initTree();
    printf("输入%d的右节点", data);
    root->right = initTree();
   }
   return root;
}

int EnQueue(SqQueue *Q,struct TreeNode *root) {
   if ((Q->rear+1)%MAXSIZE == Q->front) {
    return ERROR;
    }
    Q->data[Q->rear] = root;
    Q->rear = (Q->rear+1) % MAXSIZE;
    return OK;
}

int InitQueue(SqQueue *Q) {
   Q->front = 0;
   Q->rear = 0;
   return 1;
}

struct TreeNode* DeQueue(SqQueue *Q) {
    struct TreeNode *root;
    if (Q->front == Q->rear) {
       return ERROR;
    }
    root = Q->data[Q->front];
    Q->front = (Q->front+1) % MAXSIZE;
    return root;
}

void queueTraversal(struct TreeNode *root) {
   // 利用队列实现层序遍历
   if (!root) {
       return;
   }
   // 创建队列
   SqQueue q;
   InitQueue(&q);
   EnQueue(&q,root);
   int i = 0;
   printf("层序遍历是\n");
   while (q.front != q.rear) {
       root = DeQueue(&q);
       printf("%d ",root->val);
       if (root->left)
       EnQueue(&q,root->left);
       if (root->right)
       EnQueue(&q,root->right);
    }
   printf("\n");
}

void stackTraversal(struct TreeNode *root) {
    struct TreeNode* stack[100];
    int top = -1;
    printf("非递归中序比那里如下:\n");
    while (root || top != -1) {
    while (root) {
        // 把所有左树入栈
        top++;
        stack[top] = root;
        root = root->left;
    }
    if (top != -1) {
        root = stack[top];
        top--;
        printf("%d ",root->val);    // 输出节点数据
        root=root->right;
    }
    }
    printf("\n");
}



int main() {
   // 开辟头节点
   struct TreeNode* tree;
   // 将头节点指针传入函数参数为节点指针的参数
   tree = initTree();

   // 然后根据条件输入创建二叉树

   // 使用队列层序遍历二叉树
   queueTraversal(tree);
   // 使用栈深度遍历二叉树
   stackTraversal(tree);
   printf("Hello world\n");
   return 0;
}

Reference article:
C language binary tree (create, preorder, inorder, postorder recursive implementation, and non-recursive preorder (stack), find the height of the tree) http://www.manongzj.com/blog /3-mcmgrgfddalajnn.html
binary tree traversal using the queue to achieve levels https://blog.csdn.net/lafengxiaoyu/article/details/53240346

Guess you like

Origin www.cnblogs.com/zhangpengfei5945/p/12182172.html