I have the following c++ structure
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
I have a function which sets TreeNode's children to null if their subtrees do not contain the value 1.
bool modify(TreeNode *root)
{
if(root==NULL)
return false;
if(root->val!=1)
{
bool l = modify(root->left);
bool r = modify(root->right);
if(l||r)
{
return true;
}
else
{
root = NULL;
return false;
}
}
else
return true;
}
How do I pass TreeNode *root by reference so that modifications made inside modify() are persisted?
Related
I am making a binary search tree code, in that 'am facing an issue in the deletion function. After executing the function, the code is showing inorder display incorrectly. Basically, it is incorrect, and I'am not able to understand where the problem is.
In the delete function, I have used a search function that will find the node which the user has to delete. And I have used inorder successor method for deletion so the minimum function will find the successor. All other functions like insertion, minimum, and inorder are working fine.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
} node;
struct node *insert(struct node *tnode, int data)
{
if (tnode == NULL)
{
struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = data;
ptr->left = NULL;
ptr->right = NULL;
return ptr;
}
if (data > (tnode->data))
{
tnode->right = insert(tnode->right, data);
return tnode;
}
if (data < (tnode->data))
{
tnode->left = insert(tnode->left, data);
return tnode;
}
}
void in_order(struct node *tnode)
{
if (tnode == NULL)
return;
else
{
in_order(tnode->left);
printf("%d\t", tnode->data);
in_order(tnode->right);
}
}
struct node *search(struct node *tnode, int data)
{
if (tnode == NULL)
{
return NULL;
}
else
{
if (data == tnode->data)
{
return tnode;
}
else
{
if (data > tnode->data)
{
search(tnode->right, data);
}
else
{
search(tnode->left, data);
}
}
}
}
struct node *minimum(struct node *tnode)
{
if (tnode == NULL)
{
printf("No node present\n");
return NULL;
}
else
{
if (tnode->left == NULL)
return tnode;
else
minimum(tnode->left);
}
}
struct node *deletenode(struct node *tnode, int data)
{
struct node *s;
s = search(tnode, data);
if (s == NULL)
{
printf("Value not found\n");
return NULL;
}
else
{
if (s->left == NULL && s->right == NULL) // No child case
{
free(s);
return NULL;
}
else
{
if (s->left == NULL && s->right != NULL) // single child case(right child)
{
struct node *temp = s->right;
free(s);
return temp;
}
else if (s->right == NULL && s->left != NULL) // single child case(left child)
{
struct node *temp = s->left;
free(s);
return temp;
}
else
{
struct node *temp;
temp = minimum(s->right);
s->data = temp->data;
s->right = deletenode(s->right, temp->data);
}
}
}
return s;
}
int main()
{
struct node *root = NULL, *del;
int n, i, data, key;
printf("Enter how many nodes you have to enter in a tree\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter data\n");
scanf("%d", &data);
root = insert(root, data);
}
printf("\nIN-Order display\n");
in_order(root);
int d;
printf("\nEnter data to be delete\n");
scanf("%d", &d);
del=deletenode(root, d);
printf("\nIN-Order display\n");
in_order(root);
return 0;
}
There are several places missing return (in minimum, search and deletenode). You can easily find them if you compile with -Wall -Wextra compiler options.
It is not clear, what do you want to return from deletenode. I assume that it is a tree with deleted element. Then either search should return a parent node (so it is possible to change pointers to freed node), or deletenode should move to parent node.
Also, use in_order(del) instead of in_order(root) because root can be changed while deleting.
This is the part of code where I want the createNode function to return the node that it creates.
struct Node *ptr = createNode(key);
if(key<prev->data)
{
prev->left = ptr;
}
else
{
prev->right = ptr;
}
createNode Function :-
Node * tree :: createNode(int key). //Unknown type name 'Node'
{
Node * n = new Node;
n->data = key;
n->left = NULL;
n->right = NULL;
return n;
}
declaration of function in class:-
Node * createNode(int key);
structure of Node :-
struct Node
{
int data;
Node *left;
Node *right;
};
when I define the function createNode i'am getting the error that -> Unknown type name 'Node'
how can I define the function whose return type is pointer.
#include <iostream>
#include<stdlib.h>
using namespace std;
class tree
{
struct Node
{
int data;
Node *left;
Node *right;
};
public:
Node * toDelete(Node *root, int key);
void insert(Node *root,int key);
Node * createNode(int key);
int isBST(Node* root);
void preorder(Node *root);
void postorder(Node *root);
void inorder(Node *root);
Node * search(Node *root,int key);
Node * searchIter(Node *root,int key);
Node * findmin(Node *root);
};
void tree :: preorder(Node *root)
{
if(root != NULL)
{
cout<<root->data<<"\t";
preorder(root->left);
preorder(root->right);
}
}
void tree :: postorder(Node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
cout<<root->data<<"t";
}
}
void tree :: inorder(Node *root)
{
if(root!=NULL)
{
inorder(root->left);
cout<<root->data<<"\t";
inorder(root->right);
}
}
void tree :: insert(Node *root,int key)
{
struct Node *prev = NULL;
while(root!=NULL)
{
prev = root;
if(key<root->data)
{
root = root->left;
}
else
{
root = root->right;
}
}
struct Node *ptr = createNode(key);
if(key<prev->data)
{
prev->left = ptr;
}
else
{
prev->right = ptr;
}
}
Node * tree :: createNode(int key) //Unknown type name 'Node'
{
Node * n = new Node;
n->data = key;
n->left = NULL;
n->right = NULL;
return n; //Cannot initialize return object of type 'int *' with an lvalue of type 'tree::Node *'
}
int main()
{
}
Keep Node outside tree class.
struct Node
{
int data;
Node* left;
Node* right;
};
class tree
{
....
Node is private to your tree class. Hence, it is not directly accessible outside the class the way you are trying in your code.
I was doing a problem on insertion. This is the right answer.
/*
Node is defined as
typedef struct node
{
int data;
node * left;
node * right;
}node;
*/
node * insert(node * root, int value)
{
if(root == NULL)
{
node *temp = new node();
temp->left = NULL;
temp->right = NULL;
temp->data = value;
root = temp;
return root;
}
if(value > root->data)
{
root->right = insert(root->right, value);
}
else
root->left = insert(root->left, value);
return root;
}
The only difference with the solution I made is that in the if/else statements I did not assign anything. This is my code.
Node is defined as
typedef struct node
{
int data;
node * left;
node * right;
}node;
*/
node * insert(node * root, int value)
{
if(root == NULL)
{
node *temp = new node();
temp->left = NULL;
temp->right = NULL;
temp->data = value;
root = temp;
return root;
}
if(value > root->data)
{
insert(root->right, value);
}
else
insert(root->left, value);
return root;
}
I am not sure I understand why this is necessary because basically calling this function recursively will find a right place for the node with the data value.
Then, I create node with right data and just insert it there.
I will leave a link to the problem. https://www.hackerrank.com/challenges/binary-search-tree-insertion
Thanks for help!
I have just started learning Binary Trees and went ahead and tried to implement my own in C. I am kinda lost as to why only InOrder Traversal is displaying correctly while the other two are wrong. I really can't figure this out. I even directly tried inserting nodes, and the result is the same.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node
{
int val;
struct Node *left;
struct Node *right;
};
//Allocate Memory for New Node
struct Node* getNewNode(int val)
{
struct Node * ptr = (struct Node*)malloc(sizeof(struct Node));
ptr->val = val;
ptr->left = NULL;
ptr->right = NULL;
return ptr;
}
//Insert Node in Binary Search Tree
struct Node* insertNode(struct Node* root,int val)
{
if(root == NULL)
{
root = getNewNode(val);
}
else if(val <= root->val)
{
root->left = insertNode(root->left,val);
}
else
{
root->right = insertNode(root->right,val);
}
return root;
}
void printInorder(struct Node* root)
{
if(root == NULL) return;
printInorder(root->left);
printf("%d ",root->val);
printInorder(root->right);
}
void printPostOrder(struct Node* root)
{
if(root == NULL) return;
printInorder(root->left);
printInorder(root->right);
printf("%d ",root->val);
}
void printPreOrder(struct Node*root)
{
if(root == NULL) return;
printf("%d ",root->val);
printInorder(root->left);
printInorder(root->right);
}
bool search(struct Node* root,int val)
{
if(root == NULL)
{
return false;
}
else if(val == root->val)
{
return true;
}
else if(val < root->val)
{
return search(root->left,val);
}
else
{
return search(root->right,val);
}
}
int main(void)
{
struct Node * root = NULL; //Tree is Empty
root = insertNode(root,15);
root = insertNode(root,10);
root = insertNode(root,8);
root = insertNode(root,12);
root = insertNode(root,20);
root = insertNode(root,17);
root = insertNode(root,25);
printf("Printing In-Order: \n");
printInorder(root);
printf("\nPrinting Post-Order: \n");
printPostOrder(root);
printf("\nPrinting Pre-Order: \n");
printPreOrder(root);
// if(search(root,11))
// {
// printf("\nValue Found\n");
// }
// else
// {
// printf("\nValue Not Found\n");
// }
return 0;
}
Please help me understand if I am doing this wrong or my understanding of traversals is faulty.
The output is as follows:
output terminal
You have copy-paste errors in printPostOrder and printPreOrder - they both call printInorder where they should be calling themselves.
I'm now making a Binary Tree in C++, using friend class.
But, something is wrong, and I cannot know what should I change
template <class Type>
class BinaryTree{
public:
BinaryTree(){
root = new BTNode<Type>();
currentNode = NULL;
}
~BinaryTree(){
delete root, currentNode;
};
void insertItem(Type data){
if(currentNode==NULL){
Item = data;
currentNode = root;
}
if(data<currentNode){
if(currentNode->Left.is_empty()){
currentNode->Left = new BTNode(data);
currentNode = root;
return;
}
else{
currentNode = currentNode->Left;
return insertItem(data);
}
}
if(data>currentNode){
if(currentNode->Right.is_empty()){
currentNode->Right = new BTNode(data);
currentNode = root;
return;
}
else{
currentNode = currentNode->Right;
return insertItem(data);
}
}
currentNode = root;
return;
}
void deleteItem(Type data){}
void is_empty(){
if (this == NULL) return 1;
else return 0;
}
void printInOrder(){
if(!(currentNode->Left).is_empty()){
currentNode = currentNode->Left;
}
}
private:
BTNode<Type>* currentNode;
BTNode<Type>* root;
};
and here the BTNode class that store the item of BinaryTree, and point the next Node:
template <class Type>
class BTNode{
public:
friend class BinaryTree<Type>;
BTNode(){}
BTNode(Type data){
Item = data;
}
~BTNode(){}
private:
Type Item;
BTNode<Type> *Left, *Right;
};
The Binary Tree class's BTNode*root point the first Node, and currentNode will point the 'current node' while doing something like insertion or merging the nodes.
But when I compile, the Compiler Error C2143 occurs in the BinaryTree class,
here:
BTNode<Type>* root;
BTNode<Type>* currentNode;
The error says that there is no toke ; in front of <
but I cannot know what is wrong
It appears as though BTNode isn't correctly defined inside the BinaryTree class. The compiler is not understanding that BTNode is a type. Make sure you're including and linking the files correctly.