Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************/
- /* DSA tree program example D.F. ROSS */
- /****************************************************************************/
- /****************************************************************************/
- /* include files and global data objects */
- /****************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- /****************************************************************************/
- /* define constants & types */
- /****************************************************************************/
- #define ARRLEN 100
- #define NULLREF NULL
- /****************************************************************************/
- /* tree element definition (this is hidden!) */
- /****************************************************************************/
- typedef struct treenode * treeref;
- typedef struct treenode
- {
- int value;
- int height;
- treeref LC;
- treeref RC;
- } treenode;
- /****************************************************************************/
- /* define tree instance */
- /****************************************************************************/
- static treeref T = (treeref) NULL;
- /****************************************************************************/
- /* define tree array */
- /****************************************************************************/
- static treeref treearray[ARRLEN];
- /****************************************************************************/
- /* basic operations on the tree */
- /****************************************************************************/
- static int is_empty(treeref T) { return T == NULLREF; }
- static int get_value(treeref T) { return T->value; }
- static int get_height(treeref T) { return T->height; }
- static treeref get_LC(treeref T) { return T->LC; }
- static treeref get_RC(treeref T) { return T->RC; }
- static treeref set_value(treeref T, int v) { T->value = v; return T; }
- static treeref set_height(treeref T, int h) { T->height = h; return T; }
- static treeref set_LC(treeref T, treeref L) { T->LC = L; return T; }
- static treeref set_RC(treeref T, treeref R) { T->RC = R; return T; }
- /****************************************************************************/
- /* create and initialise an element in the tree */
- /****************************************************************************/
- static treeref create_node(int v)
- {
- return set_RC(
- set_LC(
- set_height(
- set_value(malloc(sizeof(treenode)), v),
- 0),
- NULLREF),
- NULLREF);
- }
- /****************************************************************************/
- /* basic operations on the tree */
- /* LC, Node, RC - a RECURSIVE view of the tree */
- /****************************************************************************/
- static treeref node(treeref T) { return T; }
- static treeref LC(treeref T) { return get_LC(T); }
- static treeref RC(treeref T) { return get_RC(T); }
- /****************************************************************************/
- /* CONStruct a new tree from a LC, Node and RC */
- /****************************************************************************/
- static treeref cons(treeref LC, treeref N, treeref RC) {
- set_LC(N, LC); set_RC(N, RC); return N;
- }
- /****************************************************************************/
- /* FIND the height of the tree */
- /****************************************************************************/
- static int max(int a, int b) { return a>b ? a : b; }
- static int b_height(treeref T)
- {
- if(T == NULL)
- return 0;
- else{
- T->height = 1 + max(b_height(T->LC), b_height(T->RC));
- return T->height;
- }
- }
- /****************************************************************************/
- /* FIND the minimum value node */
- /****************************************************************************/
- int min(treeref T)
- {
- if (LC(T) == NULL) return get_value(T);
- else return min(LC(T));
- }
- /****************************************************************************/
- /* ADD to the tree in BST order */
- /****************************************************************************/
- static treeref b_add(treeref T, treeref N)
- {
- return is_empty(T) ? N
- : is_empty(N) ? T
- : get_value(node(N)) < get_value(node(T)) ?
- cons(b_add(LC(T), N), node(T), RC(T))
- : get_value(node(N)) > get_value(node(T)) ?
- cons(LC(T), node(T), b_add(RC(T), N))
- : T;
- }
- /****************************************************************************/
- /* REMove an element from the tree / BST order */
- /****************************************************************************/
- static treeref b_rem(treeref T, int v) //Remove given element from tree and return reordered tree
- {
- if (T == NULL){
- printf("\n Value [%d] could NOT be removed! (not found...) ");
- return NULL;
- }
- if (v == get_value(T)) {
- // Remove node T
- printf("\n Value [%d] removed! ", v);
- if (LC(T) == NULL && RC(T) == NULL) return NULL;
- if (LC(T) == NULL) return RC(T);
- if (RC(T) == NULL) return LC(T);
- // If there is 2 children
- int tmp = min(RC(T));
- // Copy tmp to T
- set_value(T, tmp);
- // Delete tmp from T right
- set_RC(T, (b_rem(RC(T), tmp)));
- return T;
- }
- else if (v < get_value(T)) return set_LC(T, (b_rem(LC(T), v)));
- else return set_RC(T, (b_rem(RC(T), v)));
- }
- // Simple method, gets unbalanced easy
- /*
- if(is_empty(T)){
- printf("\n Tree empty.. ");
- return T;
- }
- if(v < get_value(T)){ // Check left child
- printf("\n Checking left child: %d < %d ", v, get_value(T));
- return cons(b_rem(LC(T), v), T, RC(T));
- }
- if(v > get_value(T)){ // Check right child
- printf("\n Checking right child: %d > %d ", v, get_value(T));
- return cons(LC(T), T, b_rem(RC(T), v));
- }
- return b_add(LC(T), RC(T));
- */
- /****************************************************************************/
- /* FIND an element in the BST (Binary Search Tree) */
- /****************************************************************************/
- static int b_findb(treeref T, int v)
- {
- if (T == NULL){
- printf("\n Value NOT found! ");
- return -1;
- }
- if (v == get_value(T)) {
- printf("\n Value found! ");
- return 1;
- }
- else if (v < get_value(T)) return b_findb(get_LC(T),v);
- else return b_findb(get_RC(T),v);
- }
- /****************************************************************************/
- /* FIND the number of elements in the tree (cardinality) */
- /****************************************************************************/
- static int b_size(treeref T)
- {
- int elements = 1;
- if(T == NULL)
- return 0;
- elements+=b_size(T->LC);
- elements+=b_size(T->RC);
- return elements;
- }
- /****************************************************************************/
- /* display the tree ELEMENT */
- /****************************************************************************/
- static void b_disp_el(treeref T)
- {
- // if (!is_empty(T)) printf(" %2d[%d] ", get_value(node(T)), b_height(T));
- if (!is_empty(T)) printf(" %2d ", get_value(node(T)));
- else printf(" * ");
- }
- /****************************************************************************/
- /* display the tree (pre-order) */
- /****************************************************************************/
- static void b_disp_pre(treeref T) {
- if (!is_empty(T)){
- b_disp_el(node(T));
- b_disp_pre(LC(T));
- b_disp_pre(RC(T));
- }
- }
- /****************************************************************************/
- /* display the tree (in-order) */
- /****************************************************************************/
- static void b_disp_in(treeref T) {
- if (!is_empty(T)) {
- b_disp_in(LC(T));
- b_disp_el(node(T));
- b_disp_in(RC(T));
- }
- }
- /****************************************************************************/
- /* display the tree (post-order) */
- /****************************************************************************/
- static void b_disp_post(treeref T) {
- //printf("\n TO BE DONE b_disp_post");
- if (!is_empty(T)){
- b_disp_post(LC(T));
- b_disp_post(RC(T));
- b_disp_el(T);
- }
- }
- /****************************************************************************/
- /* display the treearray array */
- /****************************************************************************/
- static void b_disp_array()
- {
- int k, count;
- printf(" Treearray is: ");
- for(k = 0; k < 99; k++){
- if(treearray[k] == NULL)
- printf("*");
- else
- {
- printf("%d", treearray[k]);
- count++;
- }
- if(count == be_size())
- break;
- }
- printf("\n");
- }
- /****************************************************************************/
- /* Tree to array via a treearray (breadth-first search) */
- /****************************************************************************/
- /* Transforms a binary tree to a sequence (including NULL (*) references) */
- /* e.g. the following tree: 5 */
- /* 2 7 */
- /* * 3 6 * */
- /* */
- /* becomes: [5] [2] [7] [*] [3] [6] [*] */
- /****************************************************************************/
- static void T2Q(treeref T, int qpos) {
- if(T != NULL){
- if(qpos < 99) {
- treearray[qpos-1] = get_value(T);
- printf("\n Arr[%d] = [%d]", qpos-1, get_value(T));
- T2Q(LC(T), qpos*2);
- T2Q(RC(T), qpos*2+1);
- }
- }
- }
- /****************************************************************************/
- /* display the tree in 2D */
- /****************************************************************************/
- /* step 1: transform the tree to an array (Q) using T2Q() above */
- /* e.g. array [5] | [2] [7] | [*] [3] [6] [*] | etc. */
- /* index (1) | (2) (3) | (4) (5) (6) (7) | (8) ... */
- /* level (1) | (2) (2) | (3) (3) (3) (3) | (4) ... */
- /* step 2: then print the nodes at each level of the tree to give */
- /* 5 */
- /* 2 7 */
- /* * 3 6 * */
- /****************************************************************************/
- static void b_disp_2D()
- {
- T2Q(T, 1);
- printf("\n TREE (%d nodes, height %d) is inorder: ", be_size(), be_height());
- be_disp_in();
- printf("\n");
- be_disp_array();
- // Pretty print the tree
- int i = 0, count = 0, j = 1;
- for(i; i < 99; i++){
- if(i == (myPow(2, j)-2)/2){ // Indent
- indent(j);
- }
- if(count == be_size())
- break;
- if(treearray[i] == NULL)
- printf("*");
- else
- {
- printf("%d", treearray[i]);
- count++;
- }
- spacing(j); // Spacing on same line
- if(i == myPow(2, j)-2){ // New line for childs
- if(j < 6){
- printf("\n");
- j++;
- }
- else{
- break;
- }
- }
- }
- }
- /*
- Example print:
- 5
- 3 7
- 1 4 6 8
- 2
- 4
- 8
- TODO: When numbers contain more than 1 digit they will get displaced..
- */
- void indent(int level){ // Indent calculated 2 ^ (max level - level) - 1
- int i, n = myPow(2, be_height()-level);
- for(i=0; i<n-1; i++)
- printf(" ");
- }
- void spacing(int level){ // Spacing calculated 2 ^ (max level - level + 1) - 1
- int i, n = myPow(2, be_height()-level+1);
- for(i=0; i<n-1; i++)
- printf(" ");
- }
- int myPow(int base, int exponent) {
- int i, n = 1;
- for (i = 0; i < exponent; i++) {
- n *= base;
- }
- return n;
- }
- /****************************************************************************/
- /* public operations on the tree */
- /****************************************************************************/
- void be_disp_2D() { b_disp_2D(); }
- void be_disp_array() { b_disp_array(); }
- void be_disp_pre() { b_disp_pre(T); }
- void be_disp_in() { b_disp_in(T); }
- void be_disp_post() { b_disp_post(T); }
- void be_add(int v) { T = b_add(T, create_node(v)); }
- void be_rem(int v) { T = b_rem(T, v); }
- int be_is_memberb(int v) { return b_findb(T, v); }
- int be_size() { return b_size(T); }
- int be_height() { return b_height(T); }
- /****************************************************************************/
- /* end of basic functions */
- /****************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement