CTDL 1
CTDL 1
#define BST_H
#include "Node.h"
#include<map>
class BST
public:
BST();
virtual ~BST();
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);
bool isInteger();
int SumTree(Node*);
int SumTree();
int FindMax(Node*);
int FindMin(Node*);
int FindMax();
int FindMin();
bool isEmpty();
int GetLeafNodes();
int GetHeight();
int GetSumAtLevel(int);
int GetPrimeCount();
int CountNodes(Node*);
int Height(Node*);
int CountPrimes(Node*);
protected:
private:
Node* root;
};
#endif // BST_H
#include "BST.h"
#include <iostream>
BST::BST()
//ctor
this->root=NULL;
BST::~BST()
{
//dtor
Node *p=this->root;
Node *T;
if(root==NULL)
this->root=n;
return true;
while(p!=NULL){
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;
}
if(root==NULL){
root=p;
return true;
if(root->Getkey()==p->Getkey())
return false;
else if(root->Getkey()>p->Getkey())
return InsertNodeRe(root->Getleft(),p);
void BST::NLR(Node*r){
if(r!=NULL){
cout<<r->Getkey()<<"\n";
NLR(r->Getleft());
NLR(r->Getright());
void BST::LNR(Node*r){
if (r!= NULL) {
LNR(r->Getleft());
LNR(r->Getright());
void BST::LRN(Node*r){
if (r != NULL) {
LRN(r->Getleft());
LRN(r->Getright());
cout << r->Getkey() << "\n";
void BST::TravelNLR(){
NLR(this->root);
cout<<endl;
void BST::TravelLNR(){
LNR(this->root);
cout<<endl;
void BST::TravelLRN(){
LRN(this->root);
cout<<endl;
Node* p = this->root;
while (p != NULL) {
if (p->Getkey() == k)
return p;
p = p->Getleft();
else
p = p->Getright();
return NULL;
Node* p=n;
if(p->Getleft()==NULL&&n->Getright()==NULL){
if (p->Getparent() == NULL)
this->root = NULL;
else if (p->Getparent()->Getleft() == p)
p->Getparent()->Setleft(NULL);
else
p->Getparent()->Setright(NULL);
delete n;
else{
if(p->Getright()!=NULL){
p=p->Getright();
while(p->Getleft()!=NULL)//
p=p->Getleft();
n->Setkey(p->Getkey());
deleteNode(p);
}else{
p=p->Getleft();
while(p->Getright()!=NULL)//
p=p->Getright();
n->Setkey(p->Getkey());
deleteNode(p);
int BST::SumTree(Node* r) {
if (r == NULL) {
return 0;
}
int BST::SumTree() {
return SumTree(this->root);
if (isEmpty())
r = r->Getleft();
return r->Getkey();
bool BST::isEmpty()
if (root==NULL)
return false;
return true;
int BST::FindMax(Node* r) {
if (isEmpty())
r = r->Getright();
return r->Getkey();
}
int BST::FindMax()
return FindMax(this->root);
int BST::FindMin()
return FindMin(this->root);
int BST::Count(Node* r) {
if (r == NULL) {
return 0;
int BST::CountNode() {
return Count(this->root);
int BST::CountLeafNodes(Node* r) {
if (r == NULL) {
return 0;
return 1;
int BST::GetLeafNodes() {
return CountLeafNodes(this->root);
}
if (node == NULL) {
this->InsertNode(newNode);
countMap[arr[i]] = 1;
} else {
countMap[arr[i]]++;
int BST::Height(Node* r) {
if (r == NULL) {
return 0;
int BST::GetHeight() {
return Height(this->root);
if (r == NULL) {
return 0;
if (level == 0) {
return r->Getkey();
}
return true;
int BST::CountPrimes(Node* r) {
if (r == NULL) {
return 0;
int count = 0;
if (IsPrime(r->Getkey())) {
count = 1;
int BST::GetPrimeCount() {
return CountPrimes(this->root);