Skip to content

Console trees is a C library for binary tree visualization in terminal, includes some methods for working with binary trees.

License

Notifications You must be signed in to change notification settings

RikiTikkiTavi/ConsoleTrees

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ConsoleTrees

Console trees is a C library for binary tree visualization in terminal, includes some methods for working with binary trees. ConsoleTrees screenshot 1 ConsoleTrees screenshot 1

Table of contents:

Installation
Usage guide
Types

Installation

Clone library to your project folder:

git clone https://github.com/RikiTikkiTavi/ConsoleTrees.git

Using CMake:

In your CMakeLists add:

If you want use static library:

target_link_libraries(YourProjectName ${CMAKE_SOURCE_DIR}/ConsoleTrees/src/build/static/ConsoleTrees.a)

If you want use dynamic/shared library:

Using GCC

If you want use static library:

gcc -o test.out test.c path/to/.../ConsoleTrees/src/build/static/ConsoleTrees.a

If you want use dynamic/shared library:

Usage guide

Include ConsoleTrees.h:

#include "./path/to/.../ConsoleTrees/src/lib/ConsoleTrees.h"

Tree visualization:

void ct_visualize_tree(ct_tree t, int step_v0, char *val_to_string(void *val));

Accepts 3 arguments:
ct_tree t - root of tree
int step_v0 - initial margin between 2 vertices (recommend step_v0 = 5 )
char *val_to_string(void *val) - pointer on function to convert value of tree node to string

So:
1: You must convert your tree to following type, declared in /ConsoleTrees/src/lib/utils/types.h.
( For that purpose you can use ct_tree ct_create_node(void *value_ptr) )

typedef struct ct_node *ct_tree;
typedef struct ct_node {
  void *value_ptr; // pointer on node value
  ct_tree left; // pointer on left node
  ct_tree right; // pointer on right node
} ct_node;

2: You must create function, that accepts void pointer on value and returns value converted to string. For example ready int to string function from ConsoleTrees library: char *ct_int_to_string(void *val)

char *ct_int_to_string(void *val) {
  if(val==NULL){
    printf("\nl: %d ::: Error in ct_int_to_string(void *val) ::: val is NULL\n", __LINE__);
    char *str_buffer = calloc(1, 15);
    sprintf(str_buffer, "%d", 0);
    return str_buffer;
  }
  char *str_buffer = calloc(1, 15);
  sprintf(str_buffer, "%d", (*(int *) val));
  return str_buffer;
}

Finally:
root - root of tree from 1.
ct_int_to_string - tree node value to string convertion function from 2.

ct_visualize_tree(root, 5, ct_int_to_string);

Convert array to tree:

ct_tree ct_array_to_tree(int *arr, int arr_length);

If you are working with for example with Heap Sort it might be useful to visualize your array as tree.

Takes 2 arguments:
int *arr - pointer on integer array
int arr_length - length of array

Returns:
ct_tree variable - root of tree

Create node of tree:

ct_tree ct_create_node(void *value_ptr);

Creates nodes (ct_node) for ct_tree (pointer on ct_node)

Takes 1 argument:
void *value_ptr - pointer on node value;

Returns:
ct_tree variable - pointer on t_node

Delete tree

void ct_delete_tree(ct_tree t);

Deletes tree

Takes 1 argument:
ct_tree t - pointer on tree root

Calculate tree height:

 int ct_calc_tree_height(ct_tree t);

Calculates height of tree

Takes 1 argument:
ct_tree t - pointer on tree root

Returns:
int variable - tree height

Create right-only tree:

ct_tree ct_create_rightist_tree(int *values, int values_q);

Creates tree with only right nodes.

Takes 2 arguments:
int *values - integer array
int values_q - array length

Returns:
ct_tree variable - pointer on tree root

Create random 1-regular tree:

ct_tree ct_create_random_single_child_tree(int values[], int values_q);

Creates random 1-regular tree.

Takes 2 arguments:
int values[] - integer array
int values_q - array length

Returns:
ct_tree variable - pointer on tree root

Types

Types declarations

License

MIT

About

Console trees is a C library for binary tree visualization in terminal, includes some methods for working with binary trees.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published