0% found this document useful (0 votes)
13 views6 pages

BST H

BST_Tree

Uploaded by

nguyenppp21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

BST H

BST_Tree

Uploaded by

nguyenppp21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

//BST.

h
#ifndef BST_H
#define BST_H
#include "Node.h"

class BST
{
public:
BST();
virtual ~BST();

Node* Getroot() { return root; }


void Setroot(Node* val) { root = val; }
bool InsertNode(Node*);
bool InsertNodeRe(Node*, Node*);
void deleteNode(Node*);
void TravelNLR();
void TravelLNR();
void TravelLRN();
void NLR(Node*);
void LNR(Node*);
void LRN(Node*);
Node* search_x(int);
int SumTree(Node*);
int FindMin(Node*);
int FindMax(Node*);
int CountNode(Node*);
int CountLeaves();
int CountLeaves(Node*);

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;

if (p->Getkey() > n->Getkey())


p = p->Getleft();
else
if (p->Getkey() < n->Getkey())
p = p->Getright();
else
if (p->Getkey() == n->Getkey())
return false;
}

if (T->Getkey() > n->Getkey())


T->Setleft(n);

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

Node* Getleft() { return left; }


void Setleft(Node* val) { left = val; }
Node* Getright() { return right; }
void Setright(Node* val) { right = val; }
Node* Getparent() { return parent; }
void Setparent(Node* val) { parent = val; }
int Getkey() { return key; }
void Setkey(int val) { key = val; }

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

You might also like