Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- // Осуществить нерекурсивный проход по бинарному дереву,
- // чтобы печатался сначала предок, а потом потомок.
- struct tree{
- int value;
- struct tree *left;
- struct tree *right;
- };
- struct stack{
- struct tree *node;
- struct stack *next;
- };
- void makeTree(struct tree* node){
- node -> value = 100;
- node -> left = (struct tree*) malloc(sizeof(struct tree));
- node -> left -> value = 70;
- node -> left -> left = (struct tree*) malloc(sizeof(struct tree));
- node -> left -> left -> value = 50;
- node -> left -> left -> left = (struct tree*) malloc(sizeof(struct tree));
- node -> left -> left -> left -> value = 10;
- node -> left -> left -> left -> right = (struct tree*) malloc(sizeof(struct tree));
- node -> left -> left -> left -> right -> value = 40;
- node -> left -> left -> right = (struct tree*) malloc(sizeof(struct tree));
- node -> left -> left -> right -> value = 55;
- node -> left -> left -> right -> right = (struct tree*) malloc(sizeof(struct tree));
- node -> left -> left -> right -> right -> value = 60;
- node -> left -> left -> right -> right -> left = (struct tree*) malloc(sizeof(struct tree));
- node -> left -> left -> right -> right -> left -> value = 58;
- node -> left -> left -> right -> right -> right= (struct tree*) malloc(sizeof(struct tree));
- node -> left -> left -> right -> right -> right -> value = 65;
- node -> left -> right = (struct tree*) malloc(sizeof(struct tree));
- node -> left -> right -> value = 90;
- node -> right = (struct tree*) malloc(sizeof(struct tree));
- node -> right -> value = 150;
- node -> right -> right = (struct tree*) malloc(sizeof(struct tree));
- node -> right -> right -> value = 200;
- node -> right -> right -> left = (struct tree*) malloc(sizeof(struct tree));
- node -> right -> right -> left -> value = 170;
- node -> right -> right -> right = (struct tree*) malloc(sizeof(struct tree));
- node -> right -> right -> right -> value = 210;
- }
- void showTree(struct tree* node){
- if(!node){
- printf("error\n");
- exit(1);
- }
- int flag = 1;
- struct stack *stackEl = (struct stack*) calloc(1, sizeof(struct stack));
- stackEl -> node = node;
- struct stack *stackPtr = NULL;
- stackEl -> next = stackPtr;
- printf("Node stores: %d\n", node -> value);
- while (stackEl || node -> right){
- do {
- if (flag && node -> left){
- node = node -> left;
- } else {
- if (node -> right){
- node = node -> right;
- }
- }
- flag = 1;
- if (node -> left && node -> right){
- stackPtr = stackEl;
- stackEl = (struct stack*) malloc(sizeof(struct stack));
- stackEl -> node = node;
- stackEl -> next = stackPtr;
- }
- printf("Node stores: %d\n", node -> value);
- } while (node -> left || node -> right);
- if (stackEl){
- node = stackEl -> node;
- stackPtr = stackEl -> next;
- free(stackEl);
- }
- stackEl = stackPtr;
- if (node -> right){
- flag = 0;
- }
- }
- }
- void showTreeRec(struct tree* node, int depth){
- if (node) {
- showTreeRec(node -> right, depth + 1);
- }
- for (int i = 0; i < depth; i++) {
- printf(" ");
- }
- if (node){
- printf("%d\n", node -> value);
- } else {
- printf("#\n");
- }
- if (node){
- showTreeRec(node -> left, depth + 1);
- }
- }
- int main(){
- struct tree *root = (struct tree*) malloc (sizeof(struct tree));
- makeTree(root);
- showTreeRec(root, 0);
- showTree(root);
- return 0;
- }
Add Comment
Please, Sign In to add comment