DevPacho / holbertonschool-binary_trees

🌳 - Different operations with the binary trees data structure in C!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🌳 Binary trees! 🌳


A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right child. 

A Binary Tree node contains the following parts.

  1. Data
  2. Pointer to left child
  3. Pointer to right child

Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. Following are the generally used ways for traversing trees.


Example Tree

Depth First Traversals: 
(a) Inorder (Left, Root, Right) : 4 2 5 1 3 
(b) Preorder (Root, Left, Right) : 1 2 4 5 3 
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
Breadth-First or Level Order Traversal: 1 2 3 4 5 
Please see this post for Breadth-First Traversal. 



Now, let's see a maybe slightly larger example of binary trees. Perhaps with more numbers it will be easier to understand how it works

One more example: 


For this project we are going to take into account several requirements for its correct and formal development, so I am going to list them

Requirements

General

  • Allowed editors: vivimemacs
  • All your files will be compiled on Ubuntu 20.04 LTS using gcc, using the options -Wall -Werror -Wextra -pedantic -std=gnu89
  • All your files should end with a new line
  • README.md file, at the root of the folder of the project, is mandatory
  • Your code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl
  • You are not allowed to use global variables
  • No more than 5 functions per file
  • You are allowed to use the standard library
  • In the following examples, the main.c files are shown as examples. You can use them to test your functions, but you don’t have to push them to your repo (if you do we won’t take them into account). We will use our own main.c files at compilation. Our main.c files might be different from the one shown in the examples
  • The prototypes of all your functions should be included in your header file called binary_trees.h
  • Don’t forget to push your header file
  • All your header files should be include guarded

We will also use a defined structure to be able to guide us and apply the various ways that there are to traverse a binary tree.

Data structures

Please use the following data structures and types for binary trees. Don’t forget to include them in your header file.

Basic Binary Tree

/**
 * struct binary_tree_s - Binary tree node
 *
 * @n: Integer stored in the node
 * @parent: Pointer to the parent node
 * @left: Pointer to the left child node
 * @right: Pointer to the right child node
 */
struct binary_tree_s
{
    int n;
    struct binary_tree_s *parent;
    struct binary_tree_s *left;
    struct binary_tree_s *right;
};

typedef struct binary_tree_s binary_tree_t;

Binary Search Tree

typedef struct binary_tree_s bst_t;

AVL Tree

typedef struct binary_tree_s avl_t;

Max Binary Heap

typedef struct binary_tree_s heap_t;
Print function To match the examples in the tasks, you are given this function

This function is used only for visualization purposes. You don’t have to push it to your repo. It may not be used during the correction

Initially we will perform the perhaps basic operations that we can perform

Initially we will perform the perhaps basic operations that we can perform. Some conditions must also be taken into account, for the most efficient development of our code and always thinking about the possible minimum errors that could occur:

Write a function that creates a binary tree node

  • Prototype: binary_tree_t *binary_tree_node(binary_tree_t *parent, int value);
  • Where parent is a pointer to the parent node of the node to create
  • And value is the value to put in the new node
  • When created, a node does not have any child
  • Your function must return a pointer to the new node, or NULL on failure

Now the next step is to insert a node precisely to the left, so let's try that

Write a function that inserts a node as the left-child of another node

  • Prototype: binary_tree_t *binary_tree_insert_left(binary_tree_t *parent, int value);
  • Where parent is a pointer to the node to insert the left-child in
  • And value is the value to store in the new node
  • Your function must return a pointer to the created node, or NULL on failure or if parent is NULL
  • If parent already has a left-child, the new node must take its place, and the old left-child must be set as the left-child of the new node.

About

🌳 - Different operations with the binary trees data structure in C!


Languages

Language:C 100.0%