Lesson Image

Video File Is Not Added.

Audio File Is Not Added.

Youtube Video Is Not Added.


A Binary Tree is a fundamental data structure in computer science, consisting of nodes where each node can have at most two children, referred to as the left child and the right child. It is called "binary" because each node can have zero, one, or two children.

Characteristics

  • Root: The topmost node in the tree.
  • Parent and Child: Every node, except the root, is associated with one parent node and can have up to two child nodes.
  • Leaf Node: A node that does not have any children.
  • Depth: The length of the path from the root to the node.
  • Height: The length of the path from the node to the deepest leaf.

Types of Binary Trees

  • Full Binary Tree: Every node has 0 or 2 children.
  • Complete Binary Tree: All levels are completely filled except possibly the last level, which is filled from left to right.
  • Balanced Binary Tree: The height of the left and right subtrees of any node differ by at most one.

Operations

Common operations performed on binary trees include:

  • Insertion: Adding a node to the tree.
  • Deletion: Removing a node from the tree.
  • Traversal: Accessing each node in the tree exactly once. Common methods of traversal include in-order, pre-order, and post-order.

Implementation in C++

Here's a simple example of implementing a binary tree in C++, with basic operations like insertion and in-order traversal:

 

#include <iostream>

class Node {
public:
   int value;
   Node* left;
   Node* right;

   Node(int value) : value(value), left(nullptr), right(nullptr) {}
};

class BinaryTree {
public:
   Node* root;

   BinaryTree() : root(nullptr) {}

   void insert(int value) {
       root = insertRecursive(root, value);
   }

   void inOrderTraversal() {
       inOrderRecursive(root);
       std::cout << std::endl;
   }

private:
   Node* insertRecursive(Node* node, int value) {
       if (node == nullptr) {
           return new Node(value);
       }
       if (value < node->value) {
           node->left = insertRecursive(node->left, value);
       } else if (value > node->value) {
           node->right = insertRecursive(node->right, value);
       }
       return node;
   }

   void inOrderRecursive(Node* node) {
       if (node != nullptr) {
           inOrderRecursive(node->left);
           std::cout << node->value << " ";
           inOrderRecursive(node->right);
       }
   }
};

int main() {
   BinaryTree bt;
   bt.insert(50);
   bt.insert(30);
   bt.insert(70);
   bt.insert(20);
   bt.insert(40);
   bt.insert(60);
   bt.insert(80);

   std::cout << "In-order traversal of the binary tree:" << std::endl;
   bt.inOrderTraversal();  // Outputs: 20 30 40 50 60 70 80

   return 0;
}
 

In this example, a BinaryTree class is defined with a nested Node class. The Node class represents each node of the tree, storing integer values and pointers to the left and right children. The BinaryTree class includes methods to insert new nodes and perform in-order traversal, which visits nodes in ascending order of their values (left, root, right).

This straightforward example provides a foundational understanding of how binary trees are structured and manipulated in C++.

Suggestions for Next Steps: 

a. Implement other traversal methods (pre-order, post-order) to understand different ways nodes can be visited. 

b. Explore binary search trees (BSTs) where each node's left subtree contains only nodes with values lesser than the node’s value, and the right subtree only nodes with values greater.

 







Comments

 

We have introduced an AI-powered feature to enhance your learning experience. Explore personalized insights and interactive tools powered by artificial intelligence.

Try AI Feature
WORD OF THE DAY

Company