BinaryTree (!!!)
BinaryTree (!!!)
---
# Theory
---
A binary tree can be defined using a struct or a class in C++. Here's an example
using a struct:
```cpp
struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int value) {
data = value;
left = nullptr;
right = nullptr;
}
};
```
This defines a `TreeNode` struct that has three members: an `int` data value and
two pointers to other `TreeNode` structs, one for the left child and one for the
right child. The constructor initializes the data value and sets the pointers to
`nullptr`.
To create a binary tree, you can create a root node and then add nodes as children.
Here's an example:
```cpp
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
```
This creates a binary tree with a root node containing the value 1, a left child
containing the value 2, and a right child containing the value 3.
To traverse a binary tree, there are three common methods: in-order traversal, pre-
order traversal, and post-order traversal. In-order traversal visits the left
subtree, then the root node, then the right subtree. Pre-order traversal visits the
root node, then the left subtree, then the right subtree. Post-order traversal
visits the left subtree, then the right subtree, then the root node.
Here's an example of in-order traversal using recursion:
```cpp
void inorderTraversal(TreeNode* node) {
if (node == nullptr) {
return;
}
inorderTraversal(node->left);
cout << node->data << " ";
inorderTraversal(node->right);
}
```
This function takes a `TreeNode*` parameter and recursively visits the left
subtree, then prints the node's data value, and then recursively visits the right
subtree.
To search for a value in a binary tree, you can use a recursive function that
checks the current node's data value and then searches the left or right subtree
depending on whether the value is less than or greater than the current node's
value. Here's an example:
```cpp
TreeNode* search(TreeNode* node, int value) {
if (node == nullptr || node->data == value) {
return node;
}
if (value < node->data) {
return search(node->left, value);
}
else {
return search(node->right, value);
}
}
```
To insert a value into a binary tree, you can use a recursive function that
searches for the correct location to insert the new node. Here's an example:
```cpp
void insert(TreeNode* node, int value) {
if (value < node->data) {
if (node->left == nullptr) {
node->left = new TreeNode(value);
}
else {
insert(node->left, value);
}
}
else {
if (node->right == nullptr) {
node->right = new TreeNode(value);
```
---
# Implementation
---
```cpp
#include <iostream>
template<typename T>
class BinaryTree {
private:
struct Node {
T data;
Node* left;
Node* right;
Node(T value) {
data = value;
left = nullptr;
right = nullptr;
}
};
Node* root;
public:
BinaryTree() {
root = nullptr;
}
~BinaryTree() {
destroyTree(root);
}
void traverseInOrder() {
traverseInOrder(root);
}
private:
void destroyTree(Node* node) {
if (node != nullptr) {
destroyTree(node->left);
destroyTree(node->right);
delete node;
}
}
---
# My Example
---
```cpp
// Binary Tree in C++
#include <stdlib.h>
#include <iostream>
struct node {
int data;
struct node *left;
struct node *right;
};
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
// Traverse Preorder
void traversePreOrder(struct node *temp) {
if (temp != NULL) {
cout << " " << temp->data;
traversePreOrder(temp->left);
traversePreOrder(temp->right);
}
}
// Traverse Inorder
void traverseInOrder(struct node *temp) {
if (temp != NULL) {
traverseInOrder(temp->left);
cout << " " << temp->data;
traverseInOrder(temp->right);
}
}
// Traverse Postorder
void traversePostOrder(struct node *temp) {
if (temp != NULL) {
traversePostOrder(temp->left);
traversePostOrder(temp->right);
cout << " " << temp->data;
}
}
int main() {
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
---
A binary tree is a tree data structure in which each node has at most two
children, called left and right[1](https://www.geeksforgeeks.org/binary-tree-data-
structure/). [To implement a binary tree in C++, you need to define a class for
each node that contains a data item and two pointers to other nodes of the same
type](https://www.programiz.com/dsa/binary-tree)[2](https://www.programiz.com/dsa/
binary-tree). Here is an example of how to declare a node class:
```cpp
class BSTNode {
public:
int Key;
BSTNode * Left;
BSTNode * Right;
BSTNode * Parent;
};
```
To create a binary tree, you need to create nodes using the `new` operator and
assign them to the left and right pointers of their parent nodes. You also need to
keep track of the root node of the tree. Here is an example of how to create a
simple binary tree with four nodes:
```cpp
// Create root node with key 1
BSTNode * root = new BSTNode();
root->Key = 1;
```
1
/ \
2 3
/
4
```
I hope this helps you understand how to implement a binary tree in C++. If you want
to learn more about different types and operations on binary trees, you can check
out these articles: [Binary Tree - Programiz](https://www.bing.com/search?
q=write+me+a+simple+realization+binary+tree+in+C%2B
%2B&form=ANNNB1&refig=80a7c9d501c441c18f5a031dfab91158#2)[Binary Tree Data
Structure - GeeksforGeeks](https://www.bing.com/search?
q=write+me+a+simple+realization+binary+tree+in+C%2B
%2B&form=ANNNB1&refig=80a7c9d501c441c18f5a031dfab91158#3)