Trees
Dive into this core data structure concept.
A **Tree** is a hierarchical data structure that consists of nodes connected by edges. It starts from a **root** node and branches out into **child** nodes. Each child has a parent, and nodes with no children are called **leaf** nodes. A **Binary Search Tree (BST)** is a special type of binary tree with a specific ordering property: 1. The value of all nodes in the left subtree is less than the value of the root. 2. The value of all nodes in the right subtree is greater than the value of the root. 3. Both the left and right subtrees must also be binary search trees. This property makes searching for elements very efficient.
#include <stdlib.h>
typedef struct TreeNode {
int value;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
// Creating the root of a simple BST
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->value = 10;
root->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->value = 5;
root->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->value = 15;
root->left->left = NULL;
root->left->right = NULL;
root->right->left = NULL;
root->right->right = NULL;Trees are used in many areas, such as representing file systems (directories and files), organizing data for quick searching in databases, and in computer networking for routing algorithms.