those trees in the data structure

data structure

Hierarchical construction: :

Create a queue to temporarily store nodes, first store the root node in the queue, and process other nodes, you need to determine whether the parent node and this node are virtual nodes, if not, let the parent node connect to this node Point, because the root node does not have a direct predecessor, it does not participate in this step, and does special processing at the beginning. If the right subtree is connected, the parent node should be dequeued after the connection, that is, front++.

pointer* createsequence() {
int front, rear;
char c, j;
pointer *s, *r, *q[20];
front = rear = 0;
while (1) {
/*多写一行输入来过滤回车键*/
scanf("%c", &c);
scanf("%c", &j);
if (c == '#') {
return r;
}
s = (pointer *)malloc(sizeof(pointer));
if (c != '@') {
s->c = c;
s->lchild = s->rchild = NULL;
}
else {
s = NULL;
}
rear++;
q[rear] = s;
/*对根结点特殊处理,因为根结点没有直接前驱*/
if (rear == 1) {
r = s;
front = 1;
}
else {
/*新结点和前一个已有的结点都不为null*/
if (s&&q[front]) {
/*左子树*/
if (rear % 2 == 0) {
q[front]->lchild = s;
}
/*右子树,根据层次序列排序算法需要双亲结点出队*/
else {
q[front]->rchild = s;
front++;
}
}
}
}
}

Hierarchical Traversal:

front = rear = 0;
if (s == NULL) {
return;
}
rear++;
/*根结点入队1*/
q[rear] = s;
while (front != rear) {
/*根结点出队*/
front++;
/*输出根结点*/
printf("%c ", q[front]->c);
/*获取左右子树,然后通过头指针向后移来输出所有子树*/
if (q[front]->lchild != NULL) {
rear++;
q[rear] = q[front]->lchild;
}
if (q[front]->rchild != NULL) {
rear++;
q[rear] = q[front]->rchild;
}
}
break;

Build a tree first:

pointer* createfirst() {
pointer *s;
char c; 
scanf("%c", &c);
if (c == '@') {
return NULL;
}
s = (pointer *)malloc(sizeof(pointer));
s->c = c;
s->lchild = createfirst();
s->rchild = createfirst();
return s;
} 

Root traversal:

printf("%c ", s->c);
if (s == NULL) {
return;
}
show(s->lchild,2);
show(s->rchild,2);
break;

When recursing to D, if the left subtree is null, it will return to the next recursion, but its right subtree is still null. Finally, it will return to s, and at most it will be built to D, but E and C are not added?

First root + middle root build tree:

pointer* createfirstandmiddle() {
char Pre[20];
char In[20];
int ps, pe, is, ie;
pointer *s;
int i;
if (ps > pe) {
return NULL;
}
s = (pointer *)malloc(sizeof(pointer));
s->c = Pre[ps];
i = is;
while (In[i] != Pre[ps]) {
i++;
}
s->lchild = createfirstandmiddle(Pre, ps + 1, ps + i - is, In, is, i - 1);
s->rchild = createfirstandmiddle(Pre, ps + i - is + 1, pe, In, i + 1, ie);
return s;
}

Traversal algorithm:

#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int n;
struct node *lchild, *rchild;
}pointer;
pointer *t;
//*创建一棵树
pointer* create() {
pointer *p1, *p2, *p3, *p4;
p1 = (pointer *)malloc(sizeof(pointer));
p2 = (pointer *)malloc(sizeof(pointer));
p3 = (pointer *)malloc(sizeof(pointer));
p4 = (pointer *)malloc(sizeof(pointer));
p1->n = 1;
p1->lchild = p2;
p1->rchild = p3;
p2->n = 2;
p3->n = 3;
p2->lchild = p4;
p2->rchild = NULL;
p4->n = 4;
p3->lchild = NULL;
p3->rchild = NULL;
p4->lchild = NULL;
p4->rchild = NULL;
t = p1;
printf("%d\n", p1);
return t;
}
//递归前序遍历
void show1(pointer *t) {
if (t == NULL)
return;
printf("%d,%d\n", t->n,t);
show(t->lchild);
show(t->rchild);
}
//递归中序遍历
void show2(pointer *t) {
if (t == NULL)
return;
show(t->lchild);
printf("%d,%d\n", t->n,t);
show(t->rchild);
}

//递归后序遍历
void show3(pointer *t) {
if (t == NULL)
return;
show(t->lchild);
show(t->rchild);

printf("%d,%d\n", t->n,t);
}
void main() {
t=create();
show1(t);
}

Full binary tree: All nodes at each level except the last level have two children.

Complete binary tree: Except for the last layer, the number of nodes on each layer reaches the maximum value, and only a few nodes on the right are missing on the last layer.

Application of linked list:

#include<stdio.h>

struct student{

int num;

float score;

struct  Student *next;

}

void main(){

struct Student a,b,c,*head,*p;

a.num=10101;a.score=89.5;

b .num=10102;a.score=99.5;

c.num=10103;a.score=100;

head=&a;

a.next=&b;

b.next=&c;

c.next=null;

p=head;

do{

printf("%ld %5.lf\n,p->num,p->score")

p=p->next;

}while(p!=null);

}

Traversal of a binary tree:

traversal of binary tree

Preorder traversal: traverse the root node first, then traverse the left subtree, and finally traverse the right subtree. ABDHECFG

In-order traversal: traverse the left subtree first, then the root node, and finally the right subtree. HDBEAFCG

Post-root order traversal: traverse the left subtree first, then the right subtree, and finally the root node. HDEBFGCA

The leaf node of any binary tree with degree 0 is always one more than the node with degree 2.

In the chain storage structure of the linear table, the storage sequence numbers of each data node are discontinuous, and the positional and logical relationships of each node in the storage space are also inconsistent.

The sequential storage structure of the queue is generally in the form of a circular queue

Deleting numbers in linear table: 2. Alternate scan, 1. One step

#include <stdio.h>
typedef char datatype;

typedef struct {
    datatype data[10];
    int n;
}sqlist;

void init(sqlist *p) {
    int i, j;
    for (i = 97, j = 0; i < 108, j<7; i++, j++) {
        p->data[j] = i;
    }
    p->data[7] = 49;
    p->data[8] = 52;
    p->data[9] = 53;
    p->n = 10;
    printf("初始线性表为: ");
    for (j = 0; j < 10; j++) {
        printf("%c  ", p->data[j]);
    }
    printf("\n");
}

void delect1(sqlist *p) {
    int n;
    n= p->n;
    int s = 0;
    int i;
    if (n == 0) {
        printf("下溢\n");
        return;
    }
    for (i = 0; i<n; i++) {
        if (p->data[i]>=48&&p->data[i]<=57) {
            s++;
        }
        else if (s > 0) {
            p->data[i - s] = p->data[i];
        }
    }
    n -= s;
    printf("\n删除后的表长为:%d",n);
    printf("\n删除后的线性表为: ");
    for (i = 0; i < n; i++) {
        printf("%c ",p->data[i]);
    }
    printf("\n");
}

void delect2(sqlist *p) {
    int n;
    n = p->n;
    int i,j,s;
    char c;
    s = 0;
    if (n == 0) {
        printf("下溢\n");
        return;
    }
    for (i = 0,j=n; i < j; i++) {
        if (p->data[i] >= 48 && p->data[i] <= 57) {
            for (j = n; j > i; j--,n--) {
                if (p->data[j] < 48 && p->data[j] >57) {
                    c = p->data[i];
                    p->data[j] = p->data[i];
                    p->data[j] = c;
                    s++;
                    break;
                }
            }
        }
    }
    n -= s;
    printf("\n删除后的表长为:%d", n);
    printf("\n删除后的线性表为: ");
    for (i = 0; i < n; i++) {
        printf("%c ", p->data[i]);
    }
}

void main() {
    int i;
    sqlist Sqlist;
    init(&Sqlist);
    printf("方法一(一步到位):\n");
    delect1(&Sqlist);
    printf("方法二(交替扫描):\n");
    delect2(&Sqlist);
    gets();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325586507&siteId=291194637