Share binary search tree c++,

vanhanhtranvu

New member
#BinarySearchTree #C ++ #Datcate

Cây tìm kiếm nhị phân (BST) là một cấu trúc dữ liệu có thể được sử dụng để lưu trữ và truy xuất dữ liệu một cách hiệu quả.Đó là một cấu trúc dữ liệu dựa trên cây, trong đó mỗi nút có nhiều nhất là hai đứa trẻ, được gọi là đứa trẻ trái và đứa trẻ phải.Đứa trẻ bên trái của một nút phải nhỏ hơn chính nút và đứa trẻ bên phải phải lớn hơn chính nút.Thứ tự này của các nút cho phép các hoạt động tìm kiếm và chèn hiệu quả.

Để tìm kiếm một giá trị trong một BST, chúng tôi bắt đầu ở nút gốc và so sánh giá trị chúng tôi đang tìm kiếm với giá trị của nút gốc.Nếu giá trị chúng tôi đang tìm kiếm ít hơn giá trị của nút gốc, thì chúng tôi sẽ tìm kiếm phần con bên trái của nút gốc.Nếu giá trị chúng tôi đang tìm kiếm lớn hơn giá trị của nút gốc, thì chúng tôi sẽ tìm kiếm phần con bên phải của nút gốc.Quá trình này tiếp tục cho đến khi chúng tôi tìm thấy giá trị mà chúng tôi đang tìm kiếm hoặc chúng tôi tiếp cận một nút lá, điều này cho thấy rằng giá trị không tồn tại trong cây.

Để chèn một giá trị vào một BST, trước tiên chúng tôi tìm thấy nút lá sẽ là cha mẹ của nút mới.Nếu giá trị của nút mới nhỏ hơn giá trị của nút cha, thì chúng tôi sẽ chèn nút mới làm con trái của nút cha.Nếu giá trị của nút mới lớn hơn giá trị của nút cha, thì chúng tôi sẽ chèn nút mới làm con phải của nút cha.

BST là một cấu trúc dữ liệu rất hiệu quả để tìm kiếm và chèn dữ liệu.Tuy nhiên, chúng có thể kém hiệu quả hơn cho các hoạt động như tìm giá trị tối thiểu hoặc tối đa trong cây hoặc để tìm giá trị nhỏ nhất hoặc lớn nhất trong cây.

#### C ++ Triển khai cây tìm kiếm nhị phân

Sau đây là triển khai C ++ của cây tìm kiếm nhị phân:

`` `C ++
#include <Istream>

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

// một nút trong cây tìm kiếm nhị phân
Nút cấu trúc {
dữ liệu int;
Nút *trái;
Nút *phải;
};

// một chức năng để tạo một nút mới
Nút *newnode (int dữ liệu) {
Nút *node = new node ();
nút-> data = data;
nút-> trái = null;
nút-> right = null;
trả về nút;
}

// một chức năng để chèn một nút mới vào cây tìm kiếm nhị phân
void chèn (nút *gốc, dữ liệu int) {
// Nếu cây trống, hãy tạo một nút mới và biến nó thành nút gốc
if (root == null) {
root = newNode (dữ liệu);
trở lại;
}

// Nếu dữ liệu mới nhỏ hơn dữ liệu trong nút hiện tại, hãy đệ quy nó vào cây con bên trái
if (data <gốc-> dữ liệu) {
chèn (root-> trái, dữ liệu);
}

// Nếu dữ liệu mới lớn hơn dữ liệu trong nút hiện tại, hãy đệ quy nó vào cây con bên phải
khác {
chèn (gốc-> bên phải, dữ liệu);
}
}

// một chức năng để tìm kiếm giá trị trong cây tìm kiếm nhị phân
Nút *Tìm kiếm (nút *gốc, dữ liệu int) {
// Nếu cây trống, hãy trả lại null
if (root == null) {
trả lại null;
}

// Nếu dữ liệu được tìm thấy trong nút hiện tại, hãy trả lại nút
if (root-> data == data) {
trả lại gốc;
}

// Nếu dữ liệu nhỏ hơn dữ liệu trong nút hiện tại, hãy tìm kiếm đệ quy Subtree bên trái
if (data <gốc-> dữ liệu) {
Trả về tìm kiếm (root-> trái, dữ liệu);
}

// Nếu dữ liệu lớn hơn dữ liệu trong nút hiện tại, hãy tìm kiếm đệ quy Subtree đúng
khác {
Trả về tìm kiếm (root-> bên phải, dữ liệu);
}
}

// một chức năng để in các giá trị trong cây tìm kiếm nhị phân trong bộ phận chuyển đổi inorder
khoảng trống (
=======================================
#BinarySearchTree #C++ #datastructures #Tree #AlGorithM ### Binary Search Tree in C++

A binary search tree (BST) is a data structure that can be used to efficiently store and retrieve data. It is a tree-based data structure, where each node has at most two children, called the left child and the right child. The left child of a node must be less than the node itself, and the right child must be greater than the node itself. This ordering of the nodes allows for efficient searching and insertion operations.

To search for a value in a BST, we start at the root node and compare the value we are looking for with the value of the root node. If the value we are looking for is less than the value of the root node, we then search the left subtree of the root node. If the value we are looking for is greater than the value of the root node, we then search the right subtree of the root node. This process continues until we either find the value we are looking for or we reach a leaf node, which indicates that the value does not exist in the tree.

To insert a value into a BST, we first find the leaf node that would be the parent of the new node. If the value of the new node is less than the value of the parent node, we then insert the new node as the left child of the parent node. If the value of the new node is greater than the value of the parent node, we then insert the new node as the right child of the parent node.

BSTs are a very efficient data structure for searching and inserting data. However, they can be less efficient for operations such as finding the minimum or maximum value in the tree, or for finding the kth smallest or largest value in the tree.

#### C++ Implementation of a Binary Search Tree

The following is a C++ implementation of a binary search tree:

```c++
#include <iostream>

using namespace std;

// A node in a binary search tree
struct Node {
int data;
Node *left;
Node *right;
};

// A function to create a new node
Node *newNode(int data) {
Node *node = new Node();
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}

// A function to insert a new node into a binary search tree
void insert(Node *root, int data) {
// If the tree is empty, create a new node and make it the root node
if (root == NULL) {
root = newNode(data);
return;
}

// If the new data is less than the data in the current node, recursively insert it into the left subtree
if (data < root->data) {
insert(root->left, data);
}

// If the new data is greater than the data in the current node, recursively insert it into the right subtree
else {
insert(root->right, data);
}
}

// A function to search for a value in a binary search tree
Node *search(Node *root, int data) {
// If the tree is empty, return NULL
if (root == NULL) {
return NULL;
}

// If the data is found in the current node, return the node
if (root->data == data) {
return root;
}

// If the data is less than the data in the current node, recursively search the left subtree
if (data < root->data) {
return search(root->left, data);
}

// If the data is greater than the data in the current node, recursively search the right subtree
else {
return search(root->right, data);
}
}

// A function to print the values in a binary search tree in inorder traversal
void inorder(
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top