很多很多书上代码

在学习二叉树的过程中按照书上打出来的代码 记下来要经常看看 希望我和我的树玩得开心 :-)

①二叉搜索树的插入删除搜索输出

#include <bits/stdc++.h>
using namespace std;

struct Node {
    int key;
    Node *left, *right, *parent;
};

Node *root, *NIL;

void Insert(int k) {
    Node *y = NIL;
    Node *x = root;
    Node *z;

    z = (Node *) malloc(sizeof(Node));
    z -> key = k;
    z -> left = NIL;
    z -> right = NIL;

    while(x != NIL) {
        y = x;
        if(z -> key < x -> key)
            x = x -> left;
        else x = x -> right;
    }

    z -> parent = y;
    if(y == NIL)
        root = z;
    else {
        if(z -> key < y -> key)
            y -> left = z;
        else y -> right = z;
    }
}

void inorder(Node *u) {
    if(u == NIL) return;
    inorder(u -> left);
    printf(" %d", u -> key);
    inorder(u -> right);
}

void preorder(Node *u) {
    if(u == NIL) return;
    printf(" %d", u -> key);
    preorder(u -> left);
    preorder(u -> right);
}

Node *Find(Node *u, int k) {
    while(u != NIL && k != u -> key) {
        if(k < u -> key) u = u -> left;
        else u = u -> right;
    }
    return u;
}

Node *treeMinimum(Node *x) {
    while(x -> left != NIL) x = x -> left;
    return x;
}

Node *treeSuccessor(Node *x) {
    if(x -> right != NIL) return treeMinimum(x -> right);
    Node *y = x -> parent;
    while(y != NIL && x == y -> right) {
        x = y;
        y = y -> parent;
    }
    return y;
}

void treeDelete(Node *z) {
    Node *y;
    Node *x;

    if(z -> left == NIL || z -> right == NIL) y = z;
    else y = treeSuccessor(z);

    if(y -> left != NIL)
        x = y -> left;
    else x = y -> right;

    if(x != NIL)
        x -> parent = y -> parent;

    if(y -> parent == NIL) root = x;
    else {
        if(y == y -> parent -> left)
            y -> parent -> left = x;
        else y -> parent -> right = x;
    }

    if(y != z)
        z -> key = y -> key;

    free(y);
}

int main() {
    int n, i, x;
    string com;
    scanf("%d", &n);
    for(int i = 0; i < n; i ++) {
        cin >> com;
        if(com == "insert") {
            scanf("%d", &x);
            Insert(x);
        } else if(com == "print") {
            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
        } else if(com == "delete") {
            scanf("%d", &x);
            treeDelete(Find(root, x));
        } else {
            scanf("%d", &x);
            Node *t = Find(root, x);
            if(t != NIL) printf("yes\n");
            else printf("no\n");
        }

    }
    return 0;
}
View Code

②输出二叉树深度高度以及父节点和兄弟节点 + 前中后遍历

代码:

#include <bits/stdc++.h>
using namespace std;

#define MAX 10000
#define NIL -1

struct Node{
    int parent;
    int left;
    int right;
};

Node T[MAX];
int n, D[MAX], H[MAX];


void setDepth(int u, int d) {
    if(u == NIL) return ;
    D[u] = d;
    setDepth(T[u].left, d + 1);
    setDepth(T[u].right, d + 1);
}

int setHeight(int u) {
    int h1 = 0, h2 = 0;
    if(T[u].left != NIL)
        h1 = setHeight(T[u].left) + 1;
    if(T[u].right != NIL)
        h2 = setHeight(T[u].right) + 1;
    return H[u] = max(h1, h2);
}

int getSibling(int u) {
    if(T[u].parent == NIL) return NIL;
    if(T[T[u].parent].left != u && T[T[u].parent].left != NIL)
        return T[T[u].parent].left;
    if(T[T[u].parent].right != u && T[T[u].parent].right != NIL)
        return T[T[u].parent].right;

    return NIL;
}

void print(int u) {
    printf("node %d: ", u);
    printf("parent = %d, ", T[u].parent);
    printf("sibling = %d, ", getSibling(u));

    int deg = 0;
    if(T[u].left != NIL) deg ++;
    if(T[u].right != NIL) deg ++;
    printf("degree = %d, ", deg);
    printf("depth = %d, ", D[u]);
    printf("height = %d, ", H[u]);

    if(T[u].parent == NIL)
        printf("root\n");
    else if(T[u].left == NIL && T[u].right == NIL)
        printf("leaf\n");
    else printf("internal node\n");
}

void prePares(int u) {
    if(u == NIL) return ;
    printf(" %d", u);
    prePares(T[u].left);
    prePares(T[u].right);
}

void inParse(int u) {
    if(u == NIL) return ;
    inParse(T[u].left);
    printf(" %d", u);
    inParse(T[u].right);
}

void postParse(int u) {
    if(u == NIL) return ;
    postParse(T[u].left);
    postParse(T[u].right);
    printf(" %d", u);
}

/*int main() {
    int root = 0;
    scanf("%d", &n);

    for(int i = 0; i < n; i ++) T[i].parent = NIL;
    for(int i = 0; i < n; i ++) {
        int v, l, r;
        scanf("%d%d%d", &v, &l, &r);
        T[v].left = l;
        T[v].right = r;
        if(l != NIL) T[l].parent = v;
        if(r != NIL) T[r].parent = v;
    }

    for(int i = 0; i < n; i ++)
        if(T[i].parent == NIL) root = i;

    setDepth(root, 0);
    setDepth(root, 0);

    setHeight(root);
    for(int i = 0; i < n; i ++)
        print(i);

    return 0;
}*/

int main() {
    int i, v, l, r, root;
    scanf("%d", &n);
    for(int i = 0; i < n; i ++)
        T[i].parent = NIL;

    for(int i = 0; i < n; i ++) {
        scanf("%d%d%d", &v, &l, &r);
        T[v].left = l;
        T[v].right = r;
        if(l != NIL) T[l].parent = v;
        if(r != NIL) T[r].parent = v;
    }

    for(int i = 0; i < n; i ++)
        if(T[i].parent == NIL) root = i;

    printf("Preoeder\n");
    prePares(root);
    printf("\n");
    printf("Inorder\n");
    inParse(root);
    printf("\n");
    printf("Postorder\n");
    postParse(root);
    printf("\n");
    return 0;
}
View Code

③已知前序中序求后序遍历

代码:

#include <bits/stdc++.h>
using namespace std;

int n, pos;
vector<int> pre, in, post;

void rec(int l, int r) {
    if(l >= r) return;
    int root = pre[pos ++];
    int m = distance(in.begin(), find(in.begin(), in.end(), root));
    rec(l, m);
    rec(m + 1, r);
    post.push_back(root);
}

void solve() {
    pos = 0;
    rec(0, n);
    for(int i = 0; i < n; i ++) {
        if(i) printf(" ");
        printf("%d", post[i]);
    }
    printf("\n");
}

int main() {
    scanf("%d", &n);
    for(int i = 0; i < n; i ++) {
        int k;
        scanf("%d", &k);
        pre.push_back(k);
    }
    for(int i = 0; i < n; i ++) {
        int k;
        scanf("%d", &k);
        in.push_back(k);
    }
    solve();
    return 0;
}
View Code

④已知后序中序求前序遍历

#include <bits/stdc++.h>
using namespace std;

int N;
int pos;
vector<int> in, post, pre;

void rec(int root, int st, int en) {
    if(st > en) return;
    int i = st;
    while(i < en && in[i] != post[root]) i ++;
    printf("%d ", post[root]);
    rec(root - en - 1 + i, st, i - 1);
    rec(root - 1, i + 1, en);
}

int main() {
    scanf("%d", &N);
    in.resize(N), post.resize(N), pre.resize(N);
    for(int i = 0; i < N; i ++)
        scanf("%d", &in[i]);
    for(int i = 0; i < N; i ++)
        scanf("%d", &post[i]);

    rec(N - 1, 0, N - 1);
    return 0;
}
View Code

⑤生成最大堆

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
int H, A[maxn];

void maxHeapify(int x) {
    int l, r, largest;
    l = x * 2;
    r = x * 2 + 1;

    if(l <= H && A[l] > A[x]) largest = l;
    else largest = x;
    if(r <= H && A[r] > A[largest]) largest = r;

    if(largest != x) {
        swap(A[x], A[largest]);
        maxHeapify(largest);
    }
}

int main() {
    scanf("%d", &H);
    for(int i = 1; i <= H; i ++)
        scanf("%d", &A[i]);

    for(int i = H / 2; i >= 1; i --)
        maxHeapify(i);

    for(int i = 1; i <= H; i ++)
        printf("%d ", A[i]);
    return 0;
}
View Code

⑥ 手写优先队列

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
#define INFTY (1 << 30)

int H, A[maxn];

void maxHeapify(int i) {
    int largest, l, r;
    l = i * 2;f
    r = i * 2 + 1;

    if(l <= H && A[l] > A[i])
        largest = l;
    else largest = i;
    if(r <= H && A[r] > A[largest])
        largest = r;

    if(largest != i) {
        swap(A[largest], A[i]);
        maxHeapify(largest);
    }
}

int extract() {
    int maxv;
    if(H < 1) return -INFTY;
    maxv = A[1];
    A[1] = A[H --];
    maxHeapify(1);
    return maxv;
}

void increaseKey(int i, int key) {
    if(key < A[i]) return ;
    A[i] = key;
    while(i > 1 && A[i / 2] < A[i]) {
        swap(A[i], A[i / 2]);
        i /= 2;
    }
}

void Insert(int key) {
    H ++;
    A[H] = -INFTY;
    increaseKey(H, key);
}

int main() {
    int key;
    char com[10];
    while(1) {
        scanf("%s", com);
        if(com[0] == 'e' && com[1] == 'n') break;
        if(com[0] == 'i') {
            scanf("%d", &key);
            Insert(key);
        } else {
            printf("%d\n", extract());
        }
    }
    return 0;
}
View Code

 

posted @ 2018-12-10 15:01  丧心病狂工科女  阅读(272)  评论(0编辑  收藏  举报