Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

BinaryTree (!!!)

A binary tree is a data structure where each node has at most two children. To implement a binary tree in C++, you define a node struct containing an data value and pointers to left and right children. You create nodes with new and link them together by assigning child pointers. Common operations on binary trees include traversing the tree by visiting nodes in inorder, preorder, or postorder sequence.

Uploaded by

Andrew Vakulyuk
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

BinaryTree (!!!)

A binary tree is a data structure where each node has at most two children. To implement a binary tree in C++, you define a node struct containing an data value and pointers to left and right children. You create nodes with new and link them together by assigning child pointers. Common operations on binary trees include traversing the tree by visiting nodes in inorder, preorder, or postorder sequence.

Uploaded by

Andrew Vakulyuk
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

### Binary tree. Full understanding!

Dynamic Data Structures


#DynamicStructure
[[Linked List]] [[BinaryTree (!!!)]] [[Stack (!!!)]] [[Queue (!)]] [[Deque
(!!!)]]
Links: [Binary Tree Data Structure -
GeeksforGeeks](https://www.geeksforgeeks.org/binary-tree-data-structure/?ref=gcse)

---
# Theory
---

![[Pasted image 20230319153804.png]]


A binary tree is a data structure composed of nodes where each node has at most two
children, referred to as the left child and the right child. A binary tree can be
empty or can contain a root node with two subtrees, a left subtree and a right
subtree, which are also binary trees. The nodes in a binary tree are often referred
to as vertices.

![[Pasted image 20230319171845.png]]

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);
}
}
```

This function takes a `TreeNode*` parameter and a value to search for. It


recursively checks the current node's data value and returns the node if the value
is found, or searches the left or right subtree depending on whether the value is
less than or greater than the current node's 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 insert(T value) {


if (root == nullptr) {
root = new Node(value);
}
else {
insert(value, root);
}
}

bool search(T value) {


return search(value, root);
}

void remove(T value) {


remove(value, root);
}

void traverseInOrder() {
traverseInOrder(root);
}

private:
void destroyTree(Node* node) {
if (node != nullptr) {
destroyTree(node->left);
destroyTree(node->right);
delete node;
}
}

void insert(T value, Node* node) {


if (value < node->data) {
if (node->left == nullptr) {
node->left = new Node(value);
}
else {
insert(value, node->left);
}
}
else {
if (node->right == nullptr) {
node->right = new Node(value);
}
else {
insert(value, node->right);
}
}
}

bool search(T value, Node* node) {


if (node == nullptr) {
return false;
}
else if (node->data == value) {
return true;
}
else if (value < node->data) {
return search(value, node->left);
}
else {
return search(value, node->right);
}
}

void remove(T value, Node* node) {


if (node == nullptr) {
return;
}
else if (value < node->data) {
remove(value, node->left);
}
else if (value > node->data) {
remove(value, node->right);
}
else {
if (node->left == nullptr && node->right == nullptr) {
delete node;
node = nullptr;
}
else if (node->left == nullptr) {
Node* temp = node;
node = node->right;
delete temp;
}
else if (node->right == nullptr) {
Node* temp = node;
node = node->left;
delete temp;
}
else {
Node* temp = findMin(node->right);
node->data = temp->data;
remove(temp->data, node->right);
}
}
}

Node* findMin(Node* node) {


while (node->left != nullptr) {
node = node->left;
}
return node;
}

void traverseInOrder(Node* node) {


if (node != nullptr) {
traverseInOrder(node->left);
std::cout << node->data << " ";
traverseInOrder(node->right);
}
}
};
```

---
# My Example
---
```cpp
// Binary Tree in C++

#include <stdlib.h>

#include <iostream>

using namespace std;

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

// New node creation


struct node *newNode(int data) {
struct node *node = (struct node *)malloc(sizeof(struct node));

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);

cout << "preorder traversal: ";


traversePreOrder(root);
cout << "\nInorder traversal: ";
traverseInOrder(root);
cout << "\nPostorder traversal: ";
traversePostOrder(root);
}
```

---

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;

// Create left child of root with key 2


BSTNode * left = new BSTNode();
left->Key = 2;
left->Parent = root; // Set parent pointer
root->Left = left; // Set left pointer of root

// Create right child of root with key 3


BSTNode * right = new BSTNode();
right->Key = 3;
right->Parent = root; // Set parent pointer
root->Right = right; // Set right pointer of root

// Create left child of left with key 4


BSTNode * left_left = new BSTNode();
left_left->Key = 4;
left_left->Parent = left; // Set parent pointer
left->Left = left_left; // Set left pointer of left
```

The resulting binary tree looks like this:

```
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)

You might also like