Share Binary Tree C++: Học Cách Sử Dụng Cây Nhị Phân Trong C++

dinhdon724

New member
## Cây nhị phân trong C ++

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 kiểu phân cấp.Mỗi nút có một giá trị và hai nút con: một đứa trẻ trái và một đứa trẻ phải.Đứa trẻ bên trái của một nút luôn nhỏ hơn giá trị của nút và đứa trẻ bên phải luôn lớn hơn giá trị của nút.

Cây nhị phân được sử dụng trong một loạt các ứng dụng, chẳng hạn như các thuật toán sắp xếp, thuật toán tìm kiếm và nén dữ liệu.Chúng cũng được sử dụng để đại diện cho dữ liệu phân cấp, chẳng hạn như hệ thống tệp trên máy tính.

## 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 `struct` để biểu diễn một nút trong cây.Cấu trúc nên bao gồm các thành viên sau:

* `value`: giá trị của nút.
* `trái ': Một con trỏ đến đứa con trái của nút.
* `Right`: Một con trỏ đến đúng đứa con của nút.

2. Tạo một chức năng để tạo một nút mới trong cây.Hàm sẽ lấy giá trị của nút làm đối số và nó sẽ trả lại một con trỏ về nút mới.

3. Tạo một chức năng để chèn một nút vào cây.Hàm sẽ lấy giá trị của nút làm đối số và nó sẽ chèn nút vào cây ở đúng vị trí.

4. Tạo một chức năng để in cây.Hàm nên in các giá trị của các nút trong cây theo thứ tự trước.

## Mã ví dụ

Mã sau đây cho thấy một ví dụ về cách tạo một cây nhị phân trong C ++:

`` `C ++
#include <Istream>

// Xác định cấu trúc cho một nút trong cây.
Nút cấu trúc {
giá trị int;
Nút *trái;
Nút *phải;
};

// Tạo một chức năng để tạo một nút mới trong cây.
Nút *createnode (int value) {
Nút *newNode = new node ();
newnode-> value = value;
newnode-> trái = nullptr;
newNode-> right = nullPtr;
trả về newnode;
}

// Tạo một hàm để chèn một nút vào cây.
void insertNode (nút *root, int value) {
// Tạo một nút mới cho giá trị.
Nút *newnode = createnode (value);

// Tìm vị trí chính xác cho nút mới trong cây.
while (true) {
if (value <gốc-> value) {
// Nút mới sẽ đi vào bên trái của nút hiện tại.
if (root-> left == nullPtr) {
// Nút hiện tại không có con trái, vì vậy chúng tôi có thể chèn nút mới ở đây.
root-> trái = newNode;
phá vỡ;
} khác {
// Nút hiện tại có con trái, vì vậy chúng ta cần di chuyển sang đứa trẻ bên trái.
root = root-> trái;
}
} khác {
// Nút mới sẽ đi sang bên phải của nút hiện tại.
if (root-> right == nullPtr) {
// Nút hiện tại không có con đúng, vì vậy chúng ta có thể chèn nút mới ở đây.
root-> right = newNode;
phá vỡ;
} khác {
// Nút hiện tại có một đứa con phù hợp, vì vậy chúng ta cần chuyển sang đúng đứa trẻ.
root = root-> right;
}
}
}
}

// Tạo một chức năng để in cây.
void printTree (nút *root) {
// Nếu nút gốc là null, thì cây trống.
if (root == nullptr) {
trở lại;
}

// In giá trị của nút gốc.
std :: cout << root-> value << std :: endl;

// In Subtree bên trái.
printtree (root-> trái);

// In Subtree bên phải.
printtree (root-> phải);
}

int main () {
// Tạo nút gốc của cây.
=======================================
## Binary Tree in C++

A binary tree is a data structure that consists of nodes arranged in a hierarchical fashion. Each node has a value, and two child nodes: a left child and a right child. The left child of a node is always less than the value of the node, and the right child is always greater than the value of the node.

Binary trees are used in a variety of applications, such as sorting algorithms, search algorithms, and data compression. They are also used to represent hierarchical data, such as the file system on a computer.

## Creating a Binary Tree in C++

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

1. Create a `struct` to represent a node in the tree. The struct should include the following members:

* `value`: The value of the node.
* `left`: A pointer to the left child of the node.
* `right`: A pointer to the right child of the node.

2. Create a function to create a new node in the tree. The function should take the value of the node as an argument, and it should return a pointer to the new node.

3. Create a function to insert a node into the tree. The function should take the value of the node as an argument, and it should insert the node into the tree in the correct location.

4. Create a function to print the tree. The function should print the values of the nodes in the tree in preorder traversal order.

## Example Code

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

```c++
#include <iostream>

// Define the struct for a node in the tree.
struct Node {
int value;
Node *left;
Node *right;
};

// Create a function to create a new node in the tree.
Node *createNode(int value) {
Node *newNode = new Node();
newNode->value = value;
newNode->left = nullptr;
newNode->right = nullptr;
return newNode;
}

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

// Find the correct location for the new node in the tree.
while (true) {
if (value < root->value) {
// The new node should go to the left of the current node.
if (root->left == nullptr) {
// The current node does not have a left child, so we can insert the new node here.
root->left = newNode;
break;
} else {
// The current node has a left child, so we need to move to the left child.
root = root->left;
}
} else {
// The new node should go to the right of the current node.
if (root->right == nullptr) {
// The current node does not have a right child, so we can insert the new node here.
root->right = newNode;
break;
} else {
// The current node has a right child, so we need to move to the right child.
root = root->right;
}
}
}
}

// Create a function to print the tree.
void printTree(Node *root) {
// If the root node is null, then the tree is empty.
if (root == nullptr) {
return;
}

// Print the value of the root node.
std::cout << root->value << std::endl;

// Print the left subtree.
printTree(root->left);

// Print the right subtree.
printTree(root->right);
}

int main() {
// Create the root node of the tree.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top