BST H
BST H
h
#ifndef BST_H
#define BST_H
#include "Node.h"
class BST
{
public:
BST();
virtual ~BST();
protected:
private:
Node* root;
};
#endif // BST_H
//BST.cpp
#include "BST.h"
#include <iostream>
#include <unordered_map>
using namespace std;
BST::BST()
{
//ctor
this->root = nullptr;
}
BST::~BST()
{
//dtor
}
bool BST::InsertNode(Node* n) {
Node* p = this->root;
Node* T;
if (root == nullptr)
{
this->root = n;
return true;
}
while (p != nullptr) {
T = p;
else T->Setright(n);
n->Setparent(T);
return true;
}
bool BST::InsertNodeRe(Node* root, Node* p) {
if (root == nullptr) {
root = p;
return true;
}
if (root->Getkey() == p->Getkey())
return false;
else if (root->Getkey() > p->Getkey())
return InsertNodeRe(root->Getleft(), p);
else return InsertNodeRe(root->Getright(), p);
}
void BST::NLR(Node* r) {
if (r != nullptr) {
cout << r->Getkey() << "\n";
NLR(r->Getleft());
NLR(r->Getright());
}
}
void BST::LNR(Node* r) {
//
if (r != nullptr) {
LNR(r->Getleft());
cout << r->Getkey() << "\n";
LNR(r->Getright());
}
}
void BST::LRN(Node* r) {
//
if (r != nullptr) {
LRN(r->Getleft());
LRN(r->Getright());
cout << r->Getkey() << "\n";
}
}
void BST::TravelNLR() {
NLR(this->root);
}
void BST::TravelLNR() {
//
LNR(this->root);
}
void BST::TravelLRN() {
//
LRN(this->root);
}
Node* BST::search_x(int k) {
//
Node* current = this->root;
while (current != nullptr && current->Getkey() != k) {
if (k < current->Getkey())
current = current->Getleft();
else
current = current->Getright();
}
return current;
}
void BST::deleteNode(Node* n) {
Node* p = n;
if (p->Getleft() == nullptr && n->Getright() == nullptr) {
//
// Truong hop node can xoa khong co con.
if (p->Getleft() == nullptr && p->Getright() == nullptr) {
if (p->Getparent() != nullptr) {
if (p->Getparent()->Getleft() == p) {
p->Getparent()->Setleft(nullptr);
}
else {
p->Getparent()->Setright(nullptr);
}
}
else {
root = nullptr;
}
delete p;
}
// Truong hop Node can xoa co 2 con.
else if (p->Getleft() != nullptr && p->Getright() != nullptr) {
Node* successor = p->Getright();
while (successor->Getleft() != nullptr) {
successor = successor->Getleft();
}
p->Setkey(successor->Getkey());
deleteNode(successor);
}
// Truong hop Node can xoa chi co 1 con.
else {
Node* child = (p->Getleft() != nullptr) ? p->Getleft() : p->Getright();
if (p->Getparent() != nullptr) {
if (p->Getparent()->Getleft() == p) {
p->Getparent()->Setleft(child);
}
else {
p->Getparent()->Setright(child);
}
}
else {
//
root = child;
}
child->Setparent(p->Getparent());
delete p;
}
}
}
int BST::SumTree(Node* r) {
if (r == nullptr) {
return 0;
}
return r->Getkey() + SumTree(r->Getleft()) + SumTree(r->Getright());
}
int BST::FindMin(Node* r) {
if (r == nullptr) {
throw std::invalid_argument("Cay trong");
}
Node* current = r;
while (current->Getleft() != nullptr) {
current = current->Getleft();
}
return current->Getkey();
}
int BST::FindMax(Node* r) {
if (r == nullptr) {
throw std::invalid_argument("Cay trong");
}
Node* current = r;
while (current->Getright() != nullptr) {
current = current->Getright();
}
return current->Getkey();
}
int BST::CountNode(Node* r) {
if (r == nullptr) {
return 0;
}
return 1 + CountNode(r->Getleft()) + CountNode(r->Getright());
}
int BST::CountLeaves() {
return CountLeaves(root);
}
int BST::CountLeaves(Node* r) {
if (r == nullptr) {
return 0;
}
if (r->Getleft() == nullptr && r->Getright() == nullptr) {
return 1; // Nếu là nút lá, trả về 1
}
// Đệ quy đếm số lượng nút lá trong cây con trái và cây con phải
return CountLeaves(r->Getleft()) + CountLeaves(r->Getright());
}
//Node.h
#ifndef NODE_H
#define NODE_H
class Node
{
public:
Node();
Node(int);
virtual ~Node();
protected:
private:
Node* left;
Node* right;
Node* parent;
int key;
};
#endif // NODE_H
//Node.cpp
#include "Node.h"
Node::Node()
{
//ctor
this->key = 0;
this->left = nullptr;
this->right = nullptr;
this->parent = nullptr;
}
Node::Node(int k) {
//ctor
this->key = k;
this->left = nullptr;
this->right = nullptr;
this->parent = nullptr;
}
Node::~Node()
{
//dtor
}
//main.cpp
#include <iostream>
#include "BST.h"
#include "Node.h"
using namespace std;
int main()
{
BST *tree = new BST();
Node* n;
n = new Node(10);
tree->InsertNode(n);
n = new Node(19);
tree->InsertNode(n);
n = new Node(9);
tree->InsertNode(n);
n = new Node(3);
tree->InsertNode(n);
n = new Node(19);
tree->InsertNode(n);
n = new Node(8);
tree->InsertNode(n);
n = new Node(4);
tree->InsertNode(n);
n = new Node(1);
tree->InsertNode(n);
n = new Node(15);
tree->InsertNode(n);
tree->TravelNLR();
return 0;
}