Data Structures and Algorithms - binary tree of depth-first and breadth-first

Preparatory knowledge

Binary Tree Basics: Data Structures and Algorithms - Data Structure Knowledge induction

 

First, the breadth-first algorithm

Traverse the level called, from top to bottom of each layer sequentially accessed, in each layer, from left to right (right to left may be) access node, one complete access to the next level, until no nodes can access so far.
Traversal rules:

  1. First complete access to all current neighbor vertices.
  2. First visits vertices adjacent point before the access point is accessed adjacent vertices.

Through the results:

root->A->B->C->D->E

Program:

/*二叉树结构*/
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/*广度优先*/
void breadthFirstSearch(TreeNode* root){
    queue<TreeNode*> nodeQueue;  //使用队列
    nodeQueue.push(root);
    TreeNode* node;
    while(!nodeQueue.empty()){
        node = nodeQueue.front();
        nodeQueue.pop();
        printf(format, node->val);
        if(node->left){
            nodeQueue.push(node->left);  //先将左子树入队
        }
        if(node->right){
            nodeQueue.push(node->right);  //再将右子树入队
        }
    }
}

 

Second, the depth-first algorithm

For each possible path deep into the branch can not go any further so far, and each node can only be accessed once. Pay special attention that can be subdivided into the depth of a binary tree preorder, preorder, postorder.

Traversal rules :

Continuously traversing vertices along the depth direction. It refers to the depth direction of the vertex direction of its neighbors.

  • Preorder: for any sub-tree, the root access first, and then traverse the left subtree, finally traversing its right subtree.
  • Preorder: for any sub-tree, the first to traverse the left subtree, then visit the root, its last traverse right subtree.
  • Postorder: for any sub-tree, the first to traverse the left subtree, then traverse its right subtree, root last visit.

Through the results:

  • 先序:root->A->C->D->B->E

  • 中序:C->A->D->root->B->E

  • 后序:C->D->A->E->B->root

Program:

/*二叉树结构*/
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

(1) preorder

/*递归*/
void preOrderTraverse(TreeNode*root) {  
        printf(format,root->val);
        if (root->left)  
            preOrderTraverse(root->left);  
        if (root->right)  
            preOrderTraverse(root->right);  
    }  

/*非递归*/
void preOrderTraverse(TreeNode* root){
    stack<TreeNode*> nodeStack;  //使用栈
    nodeStack.push(root);
    TreeNode* node;
    while(!nodeStack.empty()){
        node = nodeStack.top();
        printf(format, node->val); 
        nodeStack.pop();
        if(node->right){
            nodeStack.push(node->right);  //先将右子树压栈
        }
        if(node->left){
            nodeStack.push(node->left);  //再将左子树压栈
        }
    }
}

(2) preorder

/*递归*/
void inOrderTraverse(TreeNode* root) {  
        if (root->left)  
            inOrderTraverse(root->left);  
        printf(format,root->val);
        if (root->right)  
           inOrderTraverse(root->right);  
    }  

/*非递归*/
void inOrderTraverse(TreeNode* root){
        stack<TreeNode*> nodeStack; //使用栈 
        TreeNode* currentNode = root;  
        while (currentNode || !nodeStack.empty()){    
            if (currentNode) {  
                nodeStack.push(currentNode);  
                currentNode = currentNode->left;  
            }
            else
            {
                currentNode = nodeStack.top();
                nodeStack.pop();
                printf(format,root->val);
                currentNode = currentNode->right;  
            }  
        }     
}

(3) after preorder

/*递归*/
void postOrderTraverse(TreeNode* root) {  
        if (root->left)  
           postOrderTraverse(root->left);
        if (root->right)  
          postOrderTraverse(root->right);    
        printf(format,root->val);
    }  

/*非递归*/
void postOrderTraverse(TreeNode* root) {  
        stack<TreeNode*node>nodeStack; //使用栈
        TreeNode* currentNode = root;  
        TreeNode* rightNode = null;  
        while (currentNode|| !stack.empty()) {  
            // 一直循环到二叉排序树最左端的叶子结点
            while (currentNode) {  
                nodeStack.push(currentNode);  
                currentNode = currentNode->left;  
            } 
            currentNode = stack.top();
            stack.pop();  
            // 当前结点没有右结点或上一个结点(已经输出的结点)是当前结点的右结点,则输出当前结点  
            while (currentNode.right == null || currentNode.right == rightNode) {  
                printf(format,root->val);
                rightNode = currentNode;  
                if (stack.isEmpty()) {  
                    return; //root以输出,则遍历结束
                    }  
                 currentNode = stack.top();
                 stack.pop();  
            }  
            stack.push(currentNode); //还有右结点没有遍历  
            currentNode = currentNode->right;  
        }  
    }  

 

reference

https://www.cnblogs.com/attitudeY/p/6790219.html

Published 53 original articles · won praise 186 · views 180 000 +

Guess you like

Origin blog.csdn.net/Kalenee/article/details/83715770