#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX_OP 10
typedef struct Node{
int key;
struct Node* lchild,* rchild;
}Node;
Node* getNewNode(int key) {
Node* p = (Node*)malloc(sizeof(Node));
p->key = key;
p->lchild = p->rchild = NULL;
return p;
}
Node* insert(Node* root, int key) {
if (root == NULL)return getNewNode(key);
if (rand() % 2)root->lchild = insert(root->lchild, key);
else root->rchild = insert(root->rchild, key);
return root;
}
void clear(Node* root) {
if (root == NULL) return;
clear(root->lchild);
clear(root->rchild);
free(root);
return;
}
Node* queue[MAX_OP+5];
int head, tail;
void bfs(Node* root) {//深度优先遍历
head = tail = 0;
queue[tail++] = root;
while (head < tail) {
Node* node = queue[head];
printf("\nnode:%d\n", node->key);
if (node->lchild) {
queue[tail++] = node->lchild;
printf("\t%d->%d(left)\n", node->key, node->lchild->key);
}
if (node->rchild) {
queue[tail++] = node->rchild;
printf("\t%d->%d(right)\n", node->key, node->rchild->key);
}
head++;
}
return;
}
int tot = 0;
void dfs(Node* root) {//广度优先遍历
if (root == NULL)return;
int start, end;
tot += 1;
start = tot;
if (root->lchild)dfs(root->lchild),printf("left\n");
if (root->rchild)dfs(root->rchild),printf("right\n");
tot += 1;
end = tot;
printf("%d:[%d,%d]\n", root->key, start, end);
return;
}
int main() {
srand(time(0));
Node* root = NULL;
for (int i = 0; i < MAX_OP; i++) {
root = insert(root, rand() % 100);
}
bfs(root);
dfs(root);
return 0;
}
二叉树深度,广度遍历
于 2023-04-10 19:46:31 首次发布