source: src/tests/avltree/avl.h @ d1969a6

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since d1969a6 was 6e4b913, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

allow 32-bit compilation of cfa-cpp, and 32-bit compilation of CFA programs (including CFA libraries)

  • Property mode set to 100644
File size: 2.8 KB
Line 
1#ifndef AVL_TREE_H
2#define AVL_TREE_H
3
4extern "C" {
5#define NULL 0
6#define assert(cond) if (! (cond)) { printf("Assertion failed: (%s) at %s:%d\n", #cond, __FILE__, __LINE__); abort(); }
7}
8
9// #include <types.h>
10// #include <lib.h>
11
12trait Comparable(otype T) {
13  int ?<?(T, T);
14};
15
16forall(otype T | Comparable(T))
17int ?==?(T t1, T t2);
18
19forall(otype T | Comparable(T))
20int ?>?(T t1, T t2);
21
22forall(dtype T | { void ^?{}(T *); })
23void delete(T * x);
24
25// To-do: properly use height or balance factor
26// Right now I'm recomputing the height for each
27// node multiple times. It's Theta-log(n), but still..
28
29// Balanced Binary Search Tree of void pointers; almost an AVL tree -
30//   just needs to make use of the balance factor properly
31// Operations:
32// ?{}, ^?{}
33// create   - allocate a new tree. Just a wrapper around malloc which also calls the tree constructor.
34// find     - search through the tree for the given key; return the associated value
35// empty    - return true if the tree is empty
36// insert   - insert node with key and value pair. Returns 0 on success
37// remove   - remove node with the given key, returns 0 on success, 1 on failure
38// copy     - deep copy of a tree
39// for_each - applies the given function to every data element in the tree
40//    assumes that a non-zero return value is an error, will return
41//    the error code from func
42
43// temporary: need forward decl to get around typedef problem
44forall(otype K | Comparable(K), otype V)
45struct tree;
46
47forall(otype K | Comparable(K), otype V)
48struct tree {
49  K key;
50  V value;
51  tree(K, V) * parent;
52  tree(K, V) * left;
53  tree(K, V) * right;
54  int balance;
55};
56
57forall(otype K | Comparable(K), otype V)
58void ?{}(tree(K, V) *t, K key, V value);
59
60forall(otype K | Comparable(K), otype V)
61void ^?{}(tree(K, V) * t);
62
63forall(otype K | Comparable(K), otype V)
64tree(K, V) * create(K key, V value);
65
66forall(otype K | Comparable(K), otype V)
67V * find(tree(K, V) * t, K key);
68
69forall(otype K | Comparable(K), otype V)
70int empty(tree(K, V) * t);
71
72// returns the root of the tree
73forall(otype K | Comparable(K), otype V)
74int insert(tree(K, V) ** t, K key, V value);
75
76forall(otype K | Comparable(K), otype V)
77int remove(tree(K, V) ** t, K key);
78
79forall(otype K | Comparable(K), otype V)
80void copy(tree(K, V) * src, tree(K, V) ** ret);
81
82forall(otype K | Comparable(K), otype V)
83void for_each(tree(K, V) * t, void (*func)(V));
84
85// // Helper function to print trees
86// forall(otype K | Comparable(K), otype V)
87// void printTree(tree * t, int level){
88//   if (empty(t)){
89//     return;
90//   }
91
92//   printTree(t->left, level+1);
93//   printf("key: %d, value: %s, level: %d\n", t->key, t->value, level);
94//   printTree(t->right, level+1);
95// }
96
97// // inorder traversal of t
98// // prints each key, followed by the value
99// forall(otype K | Comparable(K), otype V)
100// void printTree(tree(K, V) * t){
101//     printTree(t, 0);
102// }
103
104
105#endif
Note: See TracBrowser for help on using the repository browser.