Binary Tree

Unlocking the Digital Forest: Journey into the World of Binary Trees!

What is Binary Tree?

A binary tree is a hierarchical data structure composed of nodes, each having at most two children: a left child and a right child. The topmost node is called the root, and nodes with no children are leaves. Binary trees provide a flexible and efficient way to organize and retrieve data.

Applications:

  • Binary Search:Binary trees are widely used for implementing efficient search algorithms, particularly in binary search where the tree's structure allows for rapid elimination of half the remaining search space with each comparison.
  • Expression Trees:In compiler design and mathematical expression evaluation, binary trees are employed as expression trees. Each node represents an operator or operand, facilitating efficient expression parsing and evaluation.
  • Hierarchical Structures:Binary trees serve as the foundation for hierarchical structures like file systems and organizational charts, enabling the efficient representation and navigation of parent-child relationships.
  • Huffman Coding:Binary trees find application in data compression algorithms such as Huffman coding, where a binary tree is used to represent variable-length codes assigned to different characters based on their frequencies.

Pre Order:

In Order

Post Order


1



2



3



4



13



6



9



10



12


#include <iostream>
using namespace std;

struct node {
  struct node *left;
  int val;
  struct node *right;
  node() : val(0), left(nullptr), right(nullptr){};
  node(int v) : val(v), left(nullptr), right(nullptr){};
  node(int v, node *l, node *r) : val(v), left(l), right(r){};
};
class BinaryTree {
private:
  node *root;
  int level = 0;
  void createTree() {}
  void preOrder(node *n);
  void inOrder(node *n);
  void postOrder(node *n);

public:
  BinaryTree();
  void showTree();
};

// ====================== DRIVER FUNCTION ===========================
int main() {
  BinaryTree b;
  b.showTree();
  return 0;
}
// ==================================================================

BinaryTree::BinaryTree() {
  node *newNode = new node(1);
  root = newNode;
  root->left = new node(2);
  root->right = new node(3);
  root->left->left = new node(4);
  root->left->right = new node(8);
  root->right->right = new node(5);
  root->right->left = new node(6);
}

void BinaryTree::postOrder(node *n) {
  if (n == nullptr)
    return;
  postOrder(n->left);
  postOrder(n->right);
  cout << n->val << " ";
};
void BinaryTree::inOrder(node *n) {
  if (n == nullptr)
    return;
  inOrder(n->left);
  cout << n->val << " ";
  inOrder(n->right);
};
void BinaryTree::preOrder(node *n) {
  if (n == nullptr)
    return;
  cout << n->val << " ";
  preOrder(n->left);
  preOrder(n->right);
};
void BinaryTree::showTree() {
  cout << "Pre-Order: ";
  preOrder(root);
  cout << endl << "In-Order: ";
  inOrder(root);
  cout << endl << "Post-Order: ";
  postOrder(root);
}

Reviews

Based on 0 reviews

No Comments Found

Developed By Aryan Thakur