Before the interview you need to master the binary tree create binary sort tree traversal, and four

Create a binary tree

Create a common binary tree complete binary tree where to create, for example, the so-called complete binary tree, that is, starting from the root, the back layer by layer access node, first we create a structure for storing binary tree node, each node in addition to a deposit data, there is a left child and a right child.

#include<stdio.h>
#include<algorithm>
using namespace std;

struct BiTree{
    int data;
    BiTree *left;
    BiTree *right;
};

void createBiTree(BiTree* &T) {
    int data;
    scanf("%d", &data);
    if(data == -1)
        return;
    T = new BiTree();
    T->data = data;
    createBiTree(T->left);
    createBiTree(T->right);
}

int main() {
        BiTree *T;
        createBiTree(T);
        return 0;
}

If we want to create a tree shown below, enter
36-1-17-1-1
because first 3 is the root node, then left the child left child of this node 3, enter 6, then 6 left child of this node, there is no input -1, then this is the right child node 6, nor, enter -1, then this is the right child node 3, enter 7, then left 7 children of this node, no It is -1, there is no right child, is -1.
Here Insert Picture Description

Create a binary sort tree

Create a core binary sort tree node is inserted, each node is to be inserted from start comparing the root, the small node into the left into a large node on the right, where the preorder that is small to All large output node of the tree, for a complete binary sort tree, its search speed is O (logn), but have to say about the limit, that is, when we create a binary sort tree, all the children in on one side, then the tree became a linear structure, its time to look for complexity is O (n), in order to solve this problem, there is something called balanced binary tree, an algorithm is more famous red-black binary tree implementation the self-balancing, red-black tree in JDK1.8, HashMap used in there, you can look in detail HashMap bottom .

#include<stdio.h>
#include<algorithm>
using namespace std;

struct BiSortTree{
    int data;
    BiSortTree *left;
    BiSortTree *right;
};

void insertChild(BiSortTree* T,int data) {
        if (data < T->data) {
                if(T->left == NULL) {
                        T->left = new BiSortTree();
                        T->left->data = data;
                        //printf("left***if\n");
                } else {
                        //printf("left***else");
                        insertChild(T->left,data);
                }
        }
        else {
                if(T->right == NULL) {
                        T->right = new BiSortTree();
                        T->right->data = data;
                        //printf("right***if\n");
                } else {
                        //printf("right***else\n");
                        insertChild(T->right,data);
                }
        }
}


void createBiSortTree(BiSortTree* T) {
        int data;
        scanf("%d",&data);
        T->data = data;
        while(1) {
                scanf("%d", &data);
                if (data == -1)
                        break;
                insertChild(T,data);
        }
}

int main() {
        BiSortTree *T = new BiSortTree();
        createBiSortTree(T);
        return 0;
}

If we enter the figure below six figures, then he will automatically construct a binary sort tree in the following figure.
Here Insert Picture Description

Preorder tree, in order, a subsequent traversal

Preorder is " about root ", preorder is " left and right root ", after order traversal is " around the root ."
These three binary tree traversal through recursive implementation for preorder, with a full binary tree has only three nodes, the first output of the root node, and then traverse its left child, and the child left output, then traverse the root right child node and the right child output. Preorder and postorder now preorder traversal are similar ideas, it is worth mentioning that for the binary sort tree traversal sequence which is its small to large (or descending) output, because the preorder is root left and right, while the binary sort tree roots than just a small child is left, the right and the root than a small child.

void pre(BiTree* T)
{
    if(T!=NULL)
    {
        printf("%d ",T->data);
        pre(T->left);
        pre(T->right);
    }
}

void mid(BiTree* T)
{
    if(T!=NULL)
    {
        mid(T->left);
        printf("%d ",T->data);
        mid(T->right);
    }
}

void post(BiTree* T)
{
    if(T!=NULL)
    {
        post(T->left);
        post(T->right);
        printf("%d ",T->data);
    }
}

Level tree traversal

Level is achieved by traversing the data structure this queue, a FIFO queue, there is a full binary tree to be described only three nodes, the root node is first queues, and then eject the root, and outputs the roots node, and then determine whether the left child node is empty, if not empty, then into the queue, and then determine whether the right child is null, non-empty it into the queue, so that is from left to right into the queue, so, when output is output from left to right, that is the purpose traverse the level of

void level(BiTree* T) {
        queue<BiTree*> que;
        if (T != NULL) {
                que.push(T);
        }
        BiTree* p;
        while(!que.empty()) {
                p = que.front();
                que.pop();
                printf("%d ",p->data);
                if(p->left != NULL)
                        que.push(p->left);
                if(p->right != NULL)
                        que.push(p->right);
        }
}
Published 464 original articles · won praise 317 · views 80000 +

Guess you like

Origin blog.csdn.net/HeZhiYing_/article/details/105315570