main
test-branch
|
Last change
on this file since 397ae5f was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago |
|
Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.
git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c
|
-
Property mode
set to
100644
|
|
File size:
1.2 KB
|
| Line | |
|---|
| 1 | #include <seq.cvh>
|
|---|
| 2 | #include <stdlib.h>
|
|---|
| 3 | #include "treebuffer.h"
|
|---|
| 4 |
|
|---|
| 5 | struct Node {
|
|---|
| 6 | Node * parent;
|
|---|
| 7 | int data;
|
|---|
| 8 | };
|
|---|
| 9 |
|
|---|
| 10 | struct Tree {
|
|---|
| 11 | int history;
|
|---|
| 12 | Node * last; // reference to the last added node
|
|---|
| 13 | };
|
|---|
| 14 |
|
|---|
| 15 | Node * make_node(int data) {
|
|---|
| 16 | Node * node = (Node*)malloc(sizeof(Node));
|
|---|
| 17 |
|
|---|
| 18 | node->parent = NULL;
|
|---|
| 19 | node->data = data;
|
|---|
| 20 | return node;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | int get_data(Node * node) {
|
|---|
| 24 | $assert(node, "attempt to get data from a invalid node reference");
|
|---|
| 25 | return node->data;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | Tree * empty(int history) {
|
|---|
| 29 | Tree * tree = (Tree*)malloc(sizeof(Tree));
|
|---|
| 30 |
|
|---|
| 31 | tree->history = history;
|
|---|
| 32 | tree->last = NULL;
|
|---|
| 33 | return tree;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | Tree * add(Tree * tree, int data) {
|
|---|
| 37 | Node * child = make_node(data);
|
|---|
| 38 | Tree * new_tree = empty(tree->history);
|
|---|
| 39 |
|
|---|
| 40 | if (tree->last == NULL)
|
|---|
| 41 | new_tree->last = child;
|
|---|
| 42 | else {
|
|---|
| 43 | child->parent = tree->last;
|
|---|
| 44 | new_tree->last = child;
|
|---|
| 45 | }
|
|---|
| 46 | return new_tree;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | // Note: "ancestors" is an 0-terminated array:
|
|---|
| 50 | void take(Tree * tree, Node * ancestors[]) {
|
|---|
| 51 | int history = tree->history;
|
|---|
| 52 | int i = 0;
|
|---|
| 53 | Node * ancestor = tree->last;
|
|---|
| 54 |
|
|---|
| 55 | while (i < history && ancestor != NULL) {
|
|---|
| 56 | ancestors[i++] = ancestor;
|
|---|
| 57 | ancestor = ancestor->parent;
|
|---|
| 58 | }
|
|---|
| 59 | ancestors[i] = 0;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | void delete(Tree * tree) {}
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.