Compare commits
2 Commits
c460e95304
...
8fdaad7037
| Author | SHA1 | Date | |
|---|---|---|---|
| 8fdaad7037 | |||
| 6cbd12b772 |
2
7.c
2
7.c
@@ -13,8 +13,6 @@ struct Queue {
|
||||
node *tail;
|
||||
};
|
||||
|
||||
int peek(queue *q) { return q->head->id; }
|
||||
|
||||
void insert(queue *q, int id) {
|
||||
node *new = malloc(sizeof(node));
|
||||
new->id = id;
|
||||
|
||||
72
8.c
Normal file
72
8.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct Node node;
|
||||
struct Node {
|
||||
int id;
|
||||
struct Node *left;
|
||||
struct Node *right;
|
||||
};
|
||||
|
||||
node *new(int id) {
|
||||
node *n = malloc(sizeof(node));
|
||||
n->left = n->right = NULL;
|
||||
n->id = id;
|
||||
return n;
|
||||
}
|
||||
|
||||
void preorder(node *n) {
|
||||
if (!n)
|
||||
return;
|
||||
|
||||
printf("%d ", n->id);
|
||||
preorder(n->left);
|
||||
preorder(n->right);
|
||||
}
|
||||
|
||||
void postorder(node *n) {
|
||||
if (!n)
|
||||
return;
|
||||
|
||||
postorder(n->left);
|
||||
postorder(n->right);
|
||||
printf("%d ", n->id);
|
||||
}
|
||||
|
||||
void inorder(node *n) {
|
||||
if (!n)
|
||||
return;
|
||||
|
||||
inorder(n->left);
|
||||
printf("%d ", n->id);
|
||||
inorder(n->right);
|
||||
}
|
||||
|
||||
void free_tree(node *n) {
|
||||
if(!n)
|
||||
return;
|
||||
|
||||
free_tree(n->left);
|
||||
free_tree(n->right);
|
||||
free(n);
|
||||
}
|
||||
|
||||
int main() {
|
||||
node *root = new(1);
|
||||
root->left = new(4);
|
||||
root->right = new(6);
|
||||
root->left->left = new(2);
|
||||
root->left->right = new(1783);
|
||||
root->left->right->left = new(100);
|
||||
|
||||
printf("Preorder: ");
|
||||
preorder(root);
|
||||
printf("\n");
|
||||
printf("Postorder: ");
|
||||
postorder(root);
|
||||
printf("\n");
|
||||
printf("Inorder: ");
|
||||
inorder(root);
|
||||
|
||||
free_tree(root);
|
||||
}
|
||||
Reference in New Issue
Block a user