I was solving a problem and it stated:
Write a program that processes the following queries on a Binary Search Tree:
i x: Insert x in the BST
d x: Delete x from the BST
Input format
Line 1 contains an integer Q, the number of queries
The next Q lines are of the form i x or d x
Output format
For each query, print the position of x in the BST
If the position of a node is p, the positions of its left and right children are 2*p and 2*p+1 respectively
Position of the root node is 1
Question's link
11 //Queries
i 15 //i=insert; d=delete
i 9
i 25
i 8
i 13
i 18
i 19
i 7
i 11
d 9
i 14
Everything is working fine until I delete node 9. then the position of node 14 is coming out to be 5. see the diagram:Initially,
15
/ \
9 25
/ \ /
8 13 18
/ / \
7 11 19
After deleting 9;
15
/ \
11 25
/ \ /
8 13 18
/ \
7 19
After inserting 14
15
/ \
11 25
/ \ /
8 14 18
/ / \
7 13 19
Correct format should be
15
/ \
11 25
/ \ /
8 13 18
/ \ \
7 14 19
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll position=1;
struct BSTNode
{
int data;
BSTNode *left,*right;
};
BSTNode *getNewNode(int data)
{
BSTNode *newNode = new BSTNode();
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode; //returns address of new node
}
BSTNode* insert(BSTNode *root,int data)
{
if(root==NULL){
root = getNewNode(data);
}
else if(data<root->data)
{
root->left = insert(root->left,data);
}
else if(data>root->data)
{
root->right = insert(root->right,data);
}
return root;
}
BSTNode *findMin(BSTNode *root)
{
if(root->left ==NULL)
{
return root;
}
else
findMin(root->left);
}
bool search(BSTNode *root,int data)
{
if(root == NULL)
{
return 0;
}
if(data<root->data)
{
position=2*position;
search(root->left,data);
}
else if(data>root->data)
{
position=2*position+1;
search(root->right,data);
}
else
{
cout<<"Found";
return 1;
}
}
BSTNode* delet(BSTNode* root,int data)
{
if(root == NULL)
{
return 0;
}
else if(data<root->data)
{
root->left = delet(root->left,data);
}
else if(data>root->data)
{
root->right = delet(root->right,data);
}
else //Found
{
//CASE 1:No child
if(root->left == root->right ==NULL)
{
delete root;
root = NULL;
}
//CASE2: One child
else if(root->left == NULL)
{
BSTNode *temp= root;
root = root->right;
delete temp;
}
else if(root->right == NULL)
{
BSTNode *temp=root;
root= root->left;
delete temp;
}
//CASE 3: TWO CHILD
else
{
BSTNode *temp = findMin(root->right);
root->data = temp->data;
root->right = delet(root->right,root->data);
}
return root;
}
}
int main()
{
BSTNode* root = NULL; //rootptr- pointer to node
//tree is empty
int n,input,data,del;
char c;
cin>>n;
while(n--)
{
cin>>c;
cin>>input;
if(c=='i')
{
root = insert(root,input);
search(root,input);
}
if(c=='d')
{
search(root,input);
delet(root,input);
}
cout<<position<<endl;
position=1;
}
return 0;
}
How is this possible insertion is being done as a leaf node Then?
in the line
Correct format should be
your diagram is wrong. 14 is the right child of 13.
Assume that you want to insert a node is a binary search tree, you need to save the last parent node in the your searching path if the given node is not found. and insert the new node to the last parent node got in the function insert(). The program is not recursive.
here is an example in C.
#include<stdio.h>
#include<stdlib.h>
struct t_Data
{
int m_Info;
};
struct t_Node
{
struct t_Data m_Data;
struct t_Node* m_LeftChild;
struct t_Node* m_RightChild;
};
typedef struct t_Node* t_BinarySortTree;
/* return targetNode or the last node in the path */
int SearchBST(t_BinarySortTree T, int aGivenInfo, struct t_Node* lastParentNode, struct t_Node* *result)
{
if(!T)
{
*result = lastParentNode;
return 0;
}
else if (aGivenInfo == (*T).m_Data.m_Info)
{
*result = T;
return 1;
}
else if(aGivenInfo < (*T).m_Data.m_Info)
{
lastParentNode = T;
return SearchBST((*T).m_LeftChild,aGivenInfo,lastParentNode,result);
}
else
{
lastParentNode = T;
return SearchBST((*T).m_RightChild,aGivenInfo,lastParentNode,result);
}
}
void InsertBST(t_BinarySortTree *T, struct t_Data newData)
{
int status;
struct t_Node* result;
status = SearchBST(*T,newData.m_Info,NULL,&result);
/* condition: fail to search for 'newData' in the binary sort tree */
if (!status)
{
struct t_Node* newNode;
newNode = (struct t_Node*)malloc(sizeof(struct t_Node));
(*newNode).m_Data = newData;
(*newNode).m_LeftChild = NULL;
(*newNode).m_RightChild = NULL;
/* condition: result == NULL */
if (!result)
{
*T = newNode;
}
else if (newData.m_Info < (*result).m_Data.m_Info)
{
(*result).m_LeftChild = newNode;
}
/* condition: newData.m_Info > (*result).m_Data.m_Info */
else
{
(*result).m_RightChild = newNode;
}
}
}
int main(void)
{
t_BinarySortTree aBST;
aBST = NULL;
struct t_Data d1,d2,d3,d4,d5,d6,d7,d8,d9,d10;
d1.m_Info = 62;
d2.m_Info = 88;
d3.m_Info = 58;
d4.m_Info = 47;
d5.m_Info = 35;
d6.m_Info = 73;
d7.m_Info = 51;
d8.m_Info = 99;
d9.m_Info = 37;
d10.m_Info = 93;
InsertBST(&aBST,d1);
InsertBST(&aBST,d2);
InsertBST(&aBST,d3);
InsertBST(&aBST,d4);
InsertBST(&aBST,d5);
InsertBST(&aBST,d6);
InsertBST(&aBST,d7);
InsertBST(&aBST,d8);
InsertBST(&aBST,d9);
InsertBST(&aBST,d10);
}
Related
I have implemented the code as follows, In this I have made two functions to calculate the height of binary search tree using recursion and without recursion.
#include <iostream>
#include <list>
using namespace std;
struct node
{
int key;
struct node *left, *right;
};
struct node *newNode(int item)
{
struct node *temp = new node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
struct node *insert(struct node *node, int key)
{
if (node == NULL)
return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
return node;
}
int heightRecursive(struct node *node)
{
if (node == NULL)
return -1;
else
{
int lDepth = heightRecursive(node->left);
int rDepth = heightRecursive(node->right);
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
int heightNonRecursive(node* root)
{
if (root == NULL) {
return 0;
}
list<node*> queue;
queue.push_back(root);
node* front = NULL;
int height = 0;
while (!queue.empty())
{
int size = queue.size();
while (size--)
{
front = queue.front();
queue.pop_front();
if (front->left) {
queue.push_back(front->left);
}
if (front->right) {
queue.push_back(front->right);
}
}
height++;
}
return height;
}
int main()
{
struct node *root = NULL;
root = insert(root, 10);
insert(root, 20);
insert(root, 30);
insert(root, 40);
insert(root, 50);
insert(root, 60);
insert(root, 70);
insert(root, 75);
insert(root, 80);
inorder(root);
int h = heightRecursive(root);
cout << "\n\nHeight of tree using recursive function: " << heightRecursive(root);
cout << "\nHeight of tree using non-recursive function: " << heightNonRecursive(root);
return 0;
}
I have implemented a skewed binary tree like 10->20->30->40->50->60->70->75->80, but in the heightNonRecursive() function, I am getting the height of this binary search tree as 9. Please help where I am doing mistake.
Output of above code:
10 20 30 40 50 60 70 75 80
Height of tree using recursive function: 8
Height of tree using non-recursive function: 9
You have 9 different numbers in increasing order, in unbalanced tree, so the height should be 8, which is correct with recursive function.
10
20
30
40
50
60
70
75
80
With non-recursive function, you just have to start with height = -1;, it should return 0 if there is only one item in the tree.
int heightNonRecursive(node* root)
{
if (root == NULL)
return 0;
list<node*> queue;
queue.push_back(root);
node* front = NULL;
int height = -1; //<-start at -1
while (!queue.empty())
{
int size = queue.size();
while (size--)
{
front = queue.front();
queue.pop_front();
if (front->left)
queue.push_back(front->left);
if (front->right)
queue.push_back(front->right);
}
height++;
}
return height;
}
Well, i'm tried to build something like Binary Search Tree. And after some iterations i'm creating newnode and it has pointer which has already used. How to solve this problem, without classes. For example test,
9
1
7
5
21
22
27
25
20
10
Build it in reverse order (last is root, first cnt of vertex)
Here code:
#include <bits/stdc++.h>
using namespace std;
const int N = 3000;
int n;
int a[N];
struct node {
int v;
node *left, *right;
};
vector<int> ans;
node qwe;
void add(node *root, int elem) {
if (elem > root->v) {
if (root->right != NULL) {
add(root->right, elem);
} else {
node newnode{};
newnode.v = elem;
newnode.right = NULL;
newnode.left = NULL;
node *lsls;
lsls = &newnode;
root->right = lsls;
}
} else {
if (root->left != NULL) {
add(root->left, elem);
} else {
node newnode;
newnode.v = elem;
newnode.right = NULL;
newnode.left = NULL;
node *lsls;
lsls = &newnode;
root->left = lsls;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
qwe.v = a[n - 1];
qwe.left = NULL;
qwe.right = NULL;
node *pointer;
pointer = &qwe;
for (int i = n - 2; i > -1; --i) {
add(pointer, a[i]);
}
pointer = &qwe;
return 0;
}
When I delete 25 from BT than it works perfectly.
But when I want to delete another number like 17,
it deletes 17 with some other number are deleted from BT.
#include<iostream>
using namespace std;
struct node
{
int data;
node *left;
node *right;
};
node *root = NULL;
node* createnode(int data)
{
node *t = new node();
t->data = data;
t->left = NULL;
t->right = NULL;
return t;
}
node* min(node*);
node* insert(node *root, int data)
{
if (root == NULL)
{
root = createnode(data);
return root;
}
else{
node *t = root;
if (data <= t->data)
t->left = insert(t->left, data);
else{
t->right = insert(t->right, data);
}
}
}
node* print(node *root)
{
if (root == NULL)
{
return root;
}
else{
cout << root->data << " ";
print(root->left);
print(root->right);
}
return root;
}
node* deleten(node *root, int data)
{
if (root == NULL)
return root;
else{
if (data<root->data)
root->left = deleten(root->left, data);
if (data>root->data)
root->right = deleten(root->right, data);
else{
if (root->left == NULL && root->right == NULL)
{
node *t = root;
root = NULL;
delete t;
}
else if (root->left == NULL)
{
node *t = root;
root = root->right;
delete t;
}
else if (root->right == NULL)
{
node *t = root;
root = root->left;
delete t;
}
else{
node *t = min(root->right);
root->data = t->data;
root->right = deleten(root->right, t->data);
}
}
return root;
}
}
node* min(node *root)
{
if (root == NULL)
return root;
else{
if (root->left != NULL)
min(root->left);
else{
return root;
}
}
}
int main()
{
root = insert(root, 15);
root = insert(root, 10);
root = insert(root, 8);
root = insert(root, 12);
root = insert(root, 11);
root = insert(root, 20);
root = insert(root, 23);
root = insert(root, 17);
root = insert(root, 25);
root = print(root);
cout << endl;
root = deleten(root, 17);
root = print(root);
}
case 1:
before deletion : 15 10 8 12 11 20 17 23 25
after deletion : 15 10 8 12 11 23 25 // root=deleten(root,17);
expectation : 15 10 8 12 11 20 23 25
case 2:
before deletion : 15 10 8 12 11 20 17 23 25
after deletion : 15 10 8 12 11 20 17 23 // root=deleten(root,25);
expectation : 15 10 8 12 11 20 17 23
case 3:
before deletion : 15 10 8 12 11 20 17 23 25
after deletion : 17 12 11 23 25 // root= deleten(root,8);
expectation : 15 10 12 11 20 17 23 25
The error is here:
if (data<root->data)
root->left = deleten(root->left, data);
if (data>root->data)
root->right = deleten(root->right, data);
else{
//...
}
If data is less than root->data, it will delete the left side of the tree, and the node, since you are using if not else if to test for the right.
The answer is simple, change to an else if instead:
if (data<root->data)
root->left = deleten(root->left, data);
else if (data>root->data)
root->right = deleten(root->right, data);
else{
//...
}
I am trying to insert 0 through 11 into avl and then delete 4, 5, 6 in that order. I am getting sigserv error while deleting 6 in rr_rotation function. This is the first time I am implementing avl and I am new to programming. Where am I going wrong? I added a few comments for my own understanding and to track where the error has occurred. Here is my code:
#include<bits/stdc++.h>
using namespace std;
#define pow2(n) (1 << (n))
struct avl_node {
int data;
//int size;
struct avl_node *left;
struct avl_node *right;
}*root;
class avlTree {
public:
int height(avl_node *);
int diff(avl_node *);
avl_node *rr_rotation(avl_node *);
avl_node *ll_rotation(avl_node *);
avl_node *lr_rotation(avl_node *);
avl_node *rl_rotation(avl_node *);
avl_node* balance(avl_node *);
avl_node* insert(avl_node *, int);
int getBalance(avl_node*);
int getSize(avl_node*);
avl_node* minValueNode(avl_node*);
avl_node* del(avl_node *, int);
void inorder(avl_node *);
void preorder(avl_node *);
int kthsmallest(avl_node*, int);
avlTree() {
root = NULL;
}
};
int avlTree::height(avl_node *temp) {
int h = 0;
if (temp != NULL) {
int l_height = height(temp->left);
int r_height = height(temp->right);
int max_height = max(l_height, r_height);
h = max_height + 1;
}
return h;
}
int avlTree::diff(avl_node *temp) {
int l_height = height(temp->left);
int r_height = height(temp->right);
int b_factor = l_height - r_height;
return b_factor;
}
avl_node *avlTree::rr_rotation(avl_node *parent) {
avl_node *temp;
cout<<"inside rr rotation"<<endl;
cout<<"parent = "<<parent->data<<endl;
temp = parent->right;
if(temp == NULL)
cout<<"yes null 2"<<endl;
//cout<<"parent->right "<<temp->data<<endl;
parent->right = temp->left;
temp->left = parent;
cout<<"temp->left->data "<<temp->left->data<<endl;
return temp;
}
avl_node *avlTree::ll_rotation(avl_node *parent) {
avl_node *temp;
//cout<<"inside ll rotation"<<endl;
//cout<<"parent = "<<parent->data<<endl;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}
avl_node *avlTree::lr_rotation(avl_node *parent) {
avl_node *temp;
cout<<"inside lr rotation"<<endl;
cout<<"parent = "<<parent->data<<endl;
temp = parent->left;
parent->left = rr_rotation(temp);
return ll_rotation(parent);
}
avl_node *avlTree::rl_rotation(avl_node *parent) {
avl_node *temp;
cout<<"inside rl rotation"<<endl;
cout<<"parent = "<<parent->data<<endl;
temp = parent->right;
parent->right = ll_rotation(temp);
return rr_rotation(parent);
}
avl_node *avlTree::balance(avl_node *temp) {
int bal_factor = diff(temp);
if (bal_factor > 1) {
if (diff(temp->left) > 0)
temp = ll_rotation(temp);
else
temp = lr_rotation(temp);
} else if (bal_factor < -1) {
if (diff(temp->right) > 0)
temp = rl_rotation(temp);
else
temp = rr_rotation(temp);
}
return temp;
}
avl_node *avlTree::insert(avl_node *root, int value) {
//cout<<"Inside insert for val = "<<value<<endl;
if (root == NULL) {
root = new avl_node;
root->data = value;
root->left = NULL;
root->right = NULL;
return root;
} else if (value < root->data) {
root->left = insert(root->left, value);
root = balance(root);
} else if (value >= root->data) {
root->right = insert(root->right, value);
root = balance(root);
}
return root;
}
avl_node* avlTree::minValueNode(avl_node* node) {
avl_node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
int avlTree::getBalance(avl_node* N) {
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
avl_node* avlTree::del(avl_node *root, int value) {
cout<<"del for val = "<<value<<endl;
if (root == NULL){
cout<<"root is null here\n";
return root;
}
// If the key to be deleted is smaller than the
// root's key, then it lies in left subtree
if (value < root->data)
root->left = del(root->left, value);
// If the key to be deleted is greater than the
// root's key, then it lies in right subtree
else if (value > root->data)
root->right = del(root->right, value);
// if key is same as root's key, then This is
// the node to be deleted
else {
// node with only one child or no child
if ((root->left == NULL) || (root->right == NULL)) {
avl_node* temp = root->left ? root->left : root->right;
// No child case
if (temp == NULL) {
temp = root;
root = NULL;
cout<<"Root set to null\n";
}
else{
// One child case
cout<<temp->data<<" copied to root "<<root->data<<"\n";
*root = *temp;
// Copy the contents of
// the non-empty child
}
free(temp);
} else {
// node with two children: Get the inorder
// successor (smallest in the right subtree)
avl_node* temp = minValueNode(root->right);
// Copy the inorder successor's data to this node
root->data = temp->data;
// Delete the inorder successor
root->right = del(root->right, temp->data);
}
} // If the tree had only one node then return
if (root == NULL)
return root;
// STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
//root->height = 1 + max(height(root->left),height(root->right));
// STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to
// check whether this node became unbalanced)
int balance = getBalance(root);
cout<<"balance = "<<balance<<" for root "<<root->data<<endl;
if(root->right == NULL)
cout<<"yes null"<<endl;
// If this node becomes unbalanced, then there are 4 cases// Left Left Case
if (balance > 1 && getBalance(root->left) >= 0){
cout<<"balance1 = "<<getBalance(root->left)<<" for root "<<root->left->data<<endl;
avl_node* t = rr_rotation(root);
//root = rr_rotation(root);
cout<<"Root of the modified sub-tree is "<<t->data<<endl;
return t;
//rr_rotation(root);
}
// Left Right Case
if (balance > 1 && getBalance(root->left) < 0) {
cout<<"balance2 = "<<getBalance(root->left)<<" for root "<<root->left->data<<endl;
cout<<"prev root "<<root->left->data<<endl;
//root->left = ll_rotation(root->left);
root = lr_rotation(root);
cout<<"new root "<<root->data<<endl;
//return rr_rotation(root);
return root;
} // Right Right Case
if (balance < -1 && getBalance(root->right) <= 0){
cout<<"balance3 = "<<getBalance(root->right)<<" for root "<<root->right->data<<endl;
avl_node* t = rr_rotation(root);
cout<<"Root of the modified sub-tree is "<<t->data<<endl;
return t;
//return ll_rotation(root);
}
// Right Left Case
if (balance < -1 && getBalance(root->right) > 0) {
cout<<"balance4 = "<<getBalance(root->right)<<" for root "<<root->right->data<<endl;
//root->right = rr_rotation(root->right);
//return ll_rotation(root);
return rl_rotation(root);
}
return root;
}
void avlTree::inorder(avl_node *tree) {
if (tree == NULL)
return;
inorder(tree->left);
cout << tree->data << " ";
inorder(tree->right);
}
void avlTree::preorder(avl_node *tree) {
if (tree == NULL)
return;
cout << tree->data << " ";
preorder(tree->left);
preorder(tree->right);
}
int avlTree::getSize(avl_node* N){
if(N == NULL)
return 0;
return (getSize(N->left) + 1 + getSize(N->right));
}
int avlTree::kthsmallest(avl_node* N, int k){
int r = getSize(N->left) + 1;
if(k == r)
return N->data;
if(k < r)
return kthsmallest(N->left,k);
if(k > r)
return kthsmallest(N->right,k-r);
return -1;
}
int main(void) {
int n, i, x;
char s;
avlTree tree; for(i=0;i<12;i++){
root = tree.insert(root,i);
tree.preorder(root);
cout<<endl;
}
for(i=4;i<=6;i++){
root = tree.del(root,6);
tree.preorder(root);
cout<<endl;
}
return 0;
}
I'm writing a function that counts the leaf nodes of a height balanced tree using struct and pointers. The function takes 3 arguments: the tree, pointer to an array and the maximum depth of the tree. The length of the array is the maximum depth. When function is called the array is initialized to zero. The function recursively follows the tree structure,
keeping track of the depth, and increments the right counter whenever it reaches a leaf. The function does not follow any pointer deeper than maxdepth. The function returns 0 if there was no leaf at depth greater than maxdepth, and 1 if there was some pointer togreater depth. What is wrong with my code. Thanks.
typedef int object;
typedef int key;
typedef struct tree_struct { key key;
struct tree_struct *left;
struct tree_struct *right;
int height;
} tree_n;
int count_d (tree_n *tr, int *count, int mdepth)
{
tree_n *tmp;
int i;
if (*(count + 0) == NULL){
for (i =0; i<mdepth; i++){
*(count + i) = 0;
}
}
while (medepth != 0)
{
if (tr == NULL) return;
else if ( tree-> left == NULL || tree->right == NULL){
return (0);
}
else {
tmp = tr;
*(count + 0) = 1;
int c = 1;
while(tmp->left != NULL && tmp->right != NULL){
if(tmp-> left){
*(count + c) = 2*c;
tmp = tmp->left;
return count_d(tmp, count , mdepth);
}
else if(tmp->right){
*(count + c + 1) = 2*c + 1;
tmp = tmp->right;
return count_d(tmp,count, mdepth);
}
c++;
mpth--;
}
}
}
What is wrong with my code
One thing I noticed is that you are missing return in the recursive calls.
return count_d(tmp, count , mdepth);
// ^^^ Missing
There are two such calls. Make sure to add return to both of them.
Disclaimer: Fixing this may not fix all your problems.
Correct Function To Insert,Count All Nodes and Count Leaf Nodes
#pragma once
typedef int itemtype;
#include<iostream>
typedef int itemtype;
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class Node
{
public:
Node* left;
Node* right;
itemtype data;
};
class BT
{
private:
int count = 0;
Node* root;
void insert(itemtype d, Node* temp);//Override Function
public:
BT();//Constructor
bool isEmpty();
Node* newNode(itemtype d);
Node* getroot();
void insert(itemtype d);//Function to call in main
int countLeafNodes(Node * temp);
int countAllNodes();//to count all nodes
}
BT::BT()//constructor
{
root = NULL;
}
bool BT::isEmpty()
{
if (root == NULL)
return true;
else
return false;
}
Node* BT::newNode(itemtype d)
{
Node* n = new Node;
n->left = NULL;
n->data = d;
n->right = NULL;
return n;
}
void BT::insert(itemtype d)//Function to call in main
{
if (isEmpty())
{
Node* temp = newNode(d);
root = temp;
}
else
{
Node* temp = root;
insert(d, temp);
}
count++;//to count number of inserted nodes
}
void BT::insert(itemtype d, Node* temp)//Private Function which is overrided
{
if (d <= temp->data)
{
if (temp->left == NULL)
{
Node* n = newNode(d);
temp->left = n;
}
else
{
temp = temp->left;
insert(d, temp);
}
}
else
{
if (temp->right == NULL)
{
temp->right = newNode(d);
}
else
{
temp = temp->right;
insert(d, temp);
}
}
}
int BT::countAllNodes()
{ return count; }
int BT::countLeafNodes(Node* temp)
{
int leaf = 0;
if (temp == NULL)
return leaf;
if (temp->left == NULL && temp->right == NULL)
return ++leaf;
else
{
leaf = countLeafNodes(temp->left) + countLeafNodes(temp->right);
return leaf;
}
}
void main()
{
BT t;
t.insert(7);
t.insert(2);
t.insert(3);
t.insert(15);
t.insert(11);
t.insert(17);
t.insert(18);
cout<<"Total Number Of Nodes:" <<t.countAllNodes() <<endl;
cout << "Leaf Nodes:" << t.countLeafNodes(t.getroot()) << endl;
_getch();
}
Output:
Ouput