Skip to content

Handling binary Trees in C and difference between binary trees and binary search Trees.

Notifications You must be signed in to change notification settings

kegera/binary_trees

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Handling Binary Trees in C

Binary trees are a fundamental data structure in computer science. In C, binary trees can be implemented using structures and pointers. Here's an example of a basic binary tree node structure:

struct TreeNode {
    int data;
    struct TreeNode* left;
    struct TreeNode* right;
};

To handle binary trees in C, you can perform various operations such as creating a new node, inserting nodes, deleting nodes, traversing the tree, searching for a value, and more. Here are some common operations:


Creating a new node:

                              struct TreeNode* createNode(int data) {
    struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    newNode->data = data;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

                          

Inserting a node:


                              struct TreeNode* insertNode(struct TreeNode* root, int data) {
    if (root == NULL) {
        return createNode(data);
    }
    if (data < root->data) {
        root->left = insertNode(root->left, data);
    } else if (data > root->data) {
        root->right = insertNode(root->right, data);
    }
    return root;
}

                          

Traversing the tree (in-order traversal):


                              void inorderTraversal(struct TreeNode* root) {
    if (root != NULL) {
        inorderTraversal(root->left);
        printf("%d ", root->data);
        inorderTraversal(root->right);
    }
}

                          

Binary Trees vs Binary Search Trees

Binary trees and binary search trees (BSTs) are related but have some key differences:


Structure: A binary tree is a hierarchical structure where each node can have at most two children (left and right). In contrast, a binary search tree is a binary tree with an additional property: for any node, all values in its left subtree are less than its value, and all values in its right subtree are greater than its value.

Ordering: Binary trees do not have any specific ordering of values. In contrast, binary search trees maintain an ordering property, making it efficient for searching, insertion, and deletion operations.

Search Efficiency: Binary search trees provide efficient searching compared to binary trees. Due to the ordering property, you can eliminate half of the search space at each step, resulting in a time complexity of O(log n) for search operations in balanced BSTs.

Applications: Binary trees are used in various scenarios, such as representing hierarchical structures, expression trees, and decision trees. Binary search trees are commonly used for efficient searching, sorting, and range queries.


It's important to note that the efficiency of binary search trees depends on their balance. If a BST becomes unbalanced, it can lead to performance degradation. Techniques like AVL trees and Red-Black trees are used to maintain balance in BSTs.


This README provides a brief overview of handling binary trees in C and highlights the differences between binary trees and binary search trees. You can further expand on these topics by including code examples, more detailed explanations, and additional operations or concepts related to binary trees and binary search trees.

About

Handling binary Trees in C and difference between binary trees and binary search Trees.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages