Share binary tree c++,

bigostrich991

New member
..

Một cây nhị phân là một cấu trúc dữ liệu bao gồm các nút được sắp xếp theo cách phân cấp.Mỗi nút trong một cây nhị phân có nút con trái và nút con phải.Nút gốc là nút trên cùng trong cây và nó không có nút cha.Các nút lá là các nút ở dưới cùng của cây và chúng không có nút con.

Cây nhị phân là một cấu trúc dữ liệu rất hiệu quả để lưu trữ và truy xuất dữ liệu.Chúng có thể được sử dụng để thực hiện một loạt các cấu trúc dữ liệu và thuật toán khác nhau, chẳng hạn như cây tìm kiếm nhị phân, đống và cây cân bằng.

## Tạo cây nhị phân trong C ++

Để tạo một cây nhị phân trong C ++, bạn có thể sử dụng các bước sau:

1. Tạo một lớp cho nút cây nhị phân.Lớp nút nên có các thành viên sau:

* Một thành viên dữ liệu để lưu trữ dữ liệu cho nút.
* Một con trỏ con trái để chỉ vào nút con trái.
* Một con trỏ con phải để trỏ đến nút trẻ bên phải.

2. Tạo một chức năng để tạo một nút cây nhị phân mới.Hàm sẽ lấy dữ liệu cho nút làm đối số và trả lại một con trỏ về nút mới được tạo.

3. Tạo một hàm để chèn một nút mới vào cây nhị phân.Hàm sẽ lấy dữ liệu cho nút mới làm đối số và chèn nút vào cây ở vị trí chính xác.

4. Tạo một chức năng để xóa một nút khỏi cây nhị phân.Hàm sẽ lấy dữ liệu để nút bị xóa dưới dạng đối số và xóa nút khỏi cây.

5. Tạo một chức năng để đi qua một cây nhị phân.Chức năng sẽ truy cập từng nút trong cây theo một thứ tự cụ thể, chẳng hạn như đặt hàng trước, in có nội bộ hoặc bưu điện.

## Ví dụ về cây nhị phân trong C ++

Mã sau đây hiển thị một ví dụ về cây nhị phân trong C ++:

`` `C ++
#include <Istream>

sử dụng không gian tên STD;

// Xác định lớp nút cây nhị phân.
lớp BinaryTreenode {
công cộng:
dữ liệu int;
BinaryTreenode *LeftChild;
BinaryTreenode *phải;

BinaryTreenode (int dữ liệu) {
this-> data = data;
this-> leftChild = nullPtr;
this-> rightChild = nullPtr;
}
};

// Tạo một chức năng để tạo một nút cây nhị phân mới.
BinaryTreenode *createnode (int dữ liệu) {
trả về BinaryTreenode mới (dữ liệu);
}

// Tạo một chức năng để chèn một nút mới vào cây nhị phân.
void insertNode (BinaryTreenode *root, int data) {
// Tạo một nút mới cho dữ liệu.
BinaryTreenode *newNode = createnode (dữ liệu);

// Nếu cây trống, hãy làm nút mới thành nút gốc.
if (root == nullptr) {
root = newNode;
} khác {
// Tìm vị trí chính xác để chèn nút mới.
BinaryTreenode *currentNode = root;
while (true) {
if (data <currentNode-> data) {
if (currentNode-> retterChild == nullPtr) {
currentNode-> LeftChild = newNode;
phá vỡ;
} khác {
currentNode = currentNode-> retchild;
}
} khác {
if (currentNode-> rightChild == nullPtr) {
currentNode-> rightChild = newNode;
phá vỡ;
} khác {
currentNode = currentNode-> rightChild;
}
}
}
}
}

// Tạo một chức năng để xóa một nút khỏi cây nhị phân.
void deletenode (BinaryTreenode *root, int data) {
// Tìm nút sẽ bị xóa.
BinaryTreenode *currentNode = root;
while (currentNode! = nullPtr) {
if (data == currentNode-> data) {
phá vỡ;
} if if (data <currentNode->
=======================================
#binarytree, #C++, #datastructure, #AlGorithM, #Tree **Binary Tree in C++**

A binary tree is a data structure that consists of nodes arranged in a hierarchical manner. Each node in a binary tree has a left child node and a right child node. The root node is the topmost node in the tree, and it has no parent node. The leaf nodes are the nodes at the bottom of the tree, and they have no child nodes.

Binary trees are a very efficient data structure for storing and retrieving data. They can be used to implement a variety of different data structures and algorithms, such as binary search trees, heaps, and balanced trees.

## Creating a Binary Tree in C++

To create a binary tree in C++, you can use the following steps:

1. Create a class for the binary tree node. The node class should have the following members:

* A data member to store the data for the node.
* A left child pointer to point to the left child node.
* A right child pointer to point to the right child node.

2. Create a function to create a new binary tree node. The function should take the data for the node as an argument and return a pointer to the newly created node.

3. Create a function to insert a new node into a binary tree. The function should take the data for the new node as an argument and insert the node into the tree in the correct location.

4. Create a function to delete a node from a binary tree. The function should take the data for the node to be deleted as an argument and delete the node from the tree.

5. Create a function to traverse a binary tree. The function should visit each node in the tree in a specific order, such as preorder, inorder, or postorder.

## Example of a Binary Tree in C++

The following code shows an example of a binary tree in C++:

```c++
#include <iostream>

using namespace std;

// Define the binary tree node class.
class BinaryTreeNode {
public:
int data;
BinaryTreeNode *leftChild;
BinaryTreeNode *rightChild;

BinaryTreeNode(int data) {
this->data = data;
this->leftChild = nullptr;
this->rightChild = nullptr;
}
};

// Create a function to create a new binary tree node.
BinaryTreeNode *createNode(int data) {
return new BinaryTreeNode(data);
}

// Create a function to insert a new node into a binary tree.
void insertNode(BinaryTreeNode *root, int data) {
// Create a new node for the data.
BinaryTreeNode *newNode = createNode(data);

// If the tree is empty, make the new node the root node.
if (root == nullptr) {
root = newNode;
} else {
// Find the correct location to insert the new node.
BinaryTreeNode *currentNode = root;
while (true) {
if (data < currentNode->data) {
if (currentNode->leftChild == nullptr) {
currentNode->leftChild = newNode;
break;
} else {
currentNode = currentNode->leftChild;
}
} else {
if (currentNode->rightChild == nullptr) {
currentNode->rightChild = newNode;
break;
} else {
currentNode = currentNode->rightChild;
}
}
}
}
}

// Create a function to delete a node from a binary tree.
void deleteNode(BinaryTreeNode *root, int data) {
// Find the node to be deleted.
BinaryTreeNode *currentNode = root;
while (currentNode != nullptr) {
if (data == currentNode->data) {
break;
} else if (data < currentNode->
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top