| 1 | /* Solution to problem 2, trees
|
|---|
| 2 | * Stephen Siegel
|
|---|
| 3 | */
|
|---|
| 4 | #include <civlc.cvh>
|
|---|
| 5 | #include <stdbool.h>
|
|---|
| 6 | #include <stdlib.h>
|
|---|
| 7 | #include <stdio.h>
|
|---|
| 8 |
|
|---|
| 9 | $input int DB = 5; // depth bound
|
|---|
| 10 |
|
|---|
| 11 | typedef struct _tree {
|
|---|
| 12 | struct _tree *left;
|
|---|
| 13 | struct _tree *right;
|
|---|
| 14 | struct _tree *parent;
|
|---|
| 15 | _Bool mark;
|
|---|
| 16 | } *Tree;
|
|---|
| 17 |
|
|---|
| 18 | // algorithm to mark every node in tree...
|
|---|
| 19 | void markTree(Tree root) {
|
|---|
| 20 | Tree x, y;
|
|---|
| 21 |
|
|---|
| 22 | x = root;
|
|---|
| 23 | while (true) {
|
|---|
| 24 | x->mark = $true;
|
|---|
| 25 | if (x->left == NULL && x->right == NULL) {
|
|---|
| 26 | y = x->parent;
|
|---|
| 27 | } else {
|
|---|
| 28 | y = x->left;
|
|---|
| 29 | x->left = x->right;
|
|---|
| 30 | x->right = x->parent;
|
|---|
| 31 | x->parent = y;
|
|---|
| 32 | }
|
|---|
| 33 | x = y;
|
|---|
| 34 | if (x==NULL)
|
|---|
| 35 | break;
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | // make a new Tree from given children, allocating on heap
|
|---|
| 40 | Tree makeTree(Tree left, Tree right, _Bool mark) {
|
|---|
| 41 | Tree result = (Tree)malloc(sizeof(struct _tree));
|
|---|
| 42 |
|
|---|
| 43 | result->left = left;
|
|---|
| 44 | result->right = right;
|
|---|
| 45 | result->mark = mark;
|
|---|
| 46 | result->parent = NULL;
|
|---|
| 47 | if (left != NULL)
|
|---|
| 48 | left->parent = result;
|
|---|
| 49 | if (right != NULL)
|
|---|
| 50 | right->parent = result;
|
|---|
| 51 | return result;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | // construct an arbitrary tree of depth at most "depth"
|
|---|
| 55 | Tree makeArbitrary(int depth) {
|
|---|
| 56 | if (depth == 0)
|
|---|
| 57 | return NULL;
|
|---|
| 58 |
|
|---|
| 59 | // nondeterministic choice between two things:
|
|---|
| 60 | // 0: return tree with 0 nodes
|
|---|
| 61 | // 1: return tree with 2 nodes
|
|---|
| 62 | if ($choose_int(2) == 0)
|
|---|
| 63 | return makeTree(NULL, NULL, false);
|
|---|
| 64 |
|
|---|
| 65 | Tree t1 = makeArbitrary(depth - 1);
|
|---|
| 66 | Tree t2 = makeArbitrary(depth - 1);
|
|---|
| 67 | Tree result = makeTree(t1, t2, false);
|
|---|
| 68 |
|
|---|
| 69 | return result;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | // print tree, for debugging only...
|
|---|
| 73 | void printTree(Tree tree) {
|
|---|
| 74 | if (tree == NULL)
|
|---|
| 75 | printf("NULL\n");
|
|---|
| 76 | else {
|
|---|
| 77 | printf("Begin Tree: %p\n", tree);
|
|---|
| 78 | printf("Mark: ");
|
|---|
| 79 | if (tree->mark)
|
|---|
| 80 | printf("true\n");
|
|---|
| 81 | else
|
|---|
| 82 | printf("false\n");
|
|---|
| 83 | printf("Parent: %p\n", tree->parent);
|
|---|
| 84 | printf("Left:\n");
|
|---|
| 85 | printTree(tree->left);
|
|---|
| 86 | printf("Right:\n");
|
|---|
| 87 | printTree(tree->right);
|
|---|
| 88 | printf("End Tree\n");
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | // deep copy a tree, ignoring marks...
|
|---|
| 93 | Tree copyTree(Tree root) {
|
|---|
| 94 | if (root == NULL)
|
|---|
| 95 | return NULL;
|
|---|
| 96 |
|
|---|
| 97 | Tree t1 = copyTree(root->left);
|
|---|
| 98 | Tree t2 = copyTree(root->right);
|
|---|
| 99 |
|
|---|
| 100 | return makeTree(t1, t2, false);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | // free all nodes in the tree rooted at root...
|
|---|
| 104 | void freeTree(Tree root) {
|
|---|
| 105 | if (root != NULL) {
|
|---|
| 106 | freeTree(root->left);
|
|---|
| 107 | freeTree(root->right);
|
|---|
| 108 | free(root);
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | // example for debuggin only...
|
|---|
| 113 | Tree makeExample1() {
|
|---|
| 114 | Tree n1 = makeTree(NULL, NULL, false);
|
|---|
| 115 | Tree n2 = makeTree(NULL, NULL, true);
|
|---|
| 116 | Tree n3 = makeTree(n1, n2, false);
|
|---|
| 117 |
|
|---|
| 118 | return n3;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | // example for debuggin only...
|
|---|
| 122 | Tree makeExample2() {
|
|---|
| 123 | Tree n1 = makeTree(NULL, NULL, false);
|
|---|
| 124 | Tree n2 = makeTree(NULL, n1, false);
|
|---|
| 125 | Tree n3 = makeTree(NULL, n2, false);
|
|---|
| 126 |
|
|---|
| 127 | return n3;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | // checks every node in Tree t is marked...
|
|---|
| 131 | _Bool checkAllMarked(Tree t) {
|
|---|
| 132 | if (t != NULL) {
|
|---|
| 133 | if (!(t->mark)) return false;
|
|---|
| 134 | if (!checkAllMarked(t->left)) return false;
|
|---|
| 135 | if (!checkAllMarked(t->right)) return false;
|
|---|
| 136 | }
|
|---|
| 137 | return true;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | // checks two trees have same "shape"
|
|---|
| 141 | void checkIso(Tree t1, Tree t2) {
|
|---|
| 142 | if (t1 == NULL)
|
|---|
| 143 | $assert(t2 == NULL);
|
|---|
| 144 | if (t2 == NULL)
|
|---|
| 145 | $assert(t1 == NULL);
|
|---|
| 146 | if (t1 != NULL) {
|
|---|
| 147 | checkIso(t1->left, t2->left);
|
|---|
| 148 | checkIso(t1->right, t2->right);
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | // generate arbitrary tree up to depth bound
|
|---|
| 153 | // and check all marked, termination, no invalid
|
|---|
| 154 | // pointer operations, and resulting tree has unchanged
|
|---|
| 155 | // shape...
|
|---|
| 156 | int main() {
|
|---|
| 157 | Tree t1 = makeArbitrary(DB);
|
|---|
| 158 | Tree t2 = copyTree(t1);
|
|---|
| 159 |
|
|---|
| 160 | $assume(t1 != NULL);
|
|---|
| 161 | markTree(t1);
|
|---|
| 162 | $assert(checkAllMarked(t1));
|
|---|
| 163 | checkIso(t1, t2);
|
|---|
| 164 | freeTree(t1);
|
|---|
| 165 | freeTree(t2);
|
|---|
| 166 | }
|
|---|