0% found this document useful (0 votes)
4 views

CTDL 1

Uploaded by

reprep063
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)
4 views

CTDL 1

Uploaded by

reprep063
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/ 11

#ifndef BST_H

#define BST_H

#include "Node.h"

#include<map>

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

bool isInteger();

int SumTree(Node*);

int SumTree();

int FindMax(Node*);

int FindMin(Node*);

int FindMax();

int FindMin();

bool isEmpty();

int Count(Node *r);


int CountNode();

int CountLeafNodes(Node* r);

int GetLeafNodes();

void CountDistinctValues(int[], int, map<int, int>&);

int GetHeight();

int GetSumAtLevel(int);

int GetPrimeCount();

int CountNodes(Node*);

int Height(Node*);

int SumAtLevel(Node*, int);

int CountPrimes(Node*);

protected:

private:

Node* root;

};

#endif // BST_H

#include "BST.h"

#include <iostream>

using namespace std;

BST::BST()

//ctor

this->root=NULL;

BST::~BST()

{
//dtor

bool BST::InsertNode(Node* n){

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

bool BST::InsertNodeRe(Node* root,Node*p){

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

else return InsertNodeRe(root->Getright(),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());

cout<<r->Getkey() << "\n";

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* BST::search_x(int k){

Node* p = this->root;

while (p != NULL) {

if (p->Getkey() == k)

return p;

else if (p->Getkey() > k)

p = p->Getleft();

else

p = p->Getright();

return NULL;

void BST::deleteNode(Node* n){

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 leftSum = SumTree(r->Getleft());

int rightSum = SumTree(r->Getright());

return r->Getkey() + leftSum + rightSum;

int BST::SumTree() {

return SumTree(this->root);

int BST::FindMin(Node *r)

if (isEmpty())

while (r->Getleft() != NULL) {

r = r->Getleft();

return r->Getkey();

bool BST::isEmpty()

if (root==NULL)

return false;

return true;

int BST::FindMax(Node* r) {

if (isEmpty())

while (r->Getright() != NULL) {

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;

return 1 + Count(r->Getleft()) + Count(r->Getright());

int BST::CountNode() {

return Count(this->root);

int BST::CountLeafNodes(Node* r) {

if (r == NULL) {

return 0;

if (r->Getleft() == NULL && r->Getright() == NULL) {

return 1;

return CountLeafNodes(r->Getleft()) + CountLeafNodes(r->Getright());

int BST::GetLeafNodes() {

return CountLeafNodes(this->root);
}

void BST::CountDistinctValues(int arr[], int size, map<int, int>& countMap) {

for (int i = 0; i < size; i++) {

Node* node = this->search_x(arr[i]);

if (node == NULL) {

Node* newNode = new Node(arr[i]);

this->InsertNode(newNode);

countMap[arr[i]] = 1;

} else {

countMap[arr[i]]++;

int BST::Height(Node* r) {

if (r == NULL) {

return 0;

int leftHeight = Height(r->Getleft());

int rightHeight = Height(r->Getright());

return 1 + max(leftHeight, rightHeight);

int BST::GetHeight() {

return Height(this->root);

int BST::SumAtLevel(Node* r, int level) {

if (r == NULL) {

return 0;

if (level == 0) {

return r->Getkey();
}

return SumAtLevel(r->Getleft(), level - 1) + SumAtLevel(r->Getright(), level - 1);

int BST::GetSumAtLevel(int level) {

return SumAtLevel(this->root, level);

bool IsPrime(int num) {

if (num <= 1) return false;

if (num <= 3) return true;

if (num % 2 == 0 || num % 3 == 0) return false;

for (int i = 5; i * i <= num; i += 6) {

if (num % i == 0 || num % (i + 2) == 0) return false;

return true;

int BST::CountPrimes(Node* r) {

if (r == NULL) {

return 0;

int count = 0;

if (IsPrime(r->Getkey())) {

count = 1;

return count + CountPrimes(r->Getleft()) + CountPrimes(r->Getright());

int BST::GetPrimeCount() {

return CountPrimes(this->root);

You might also like