I have the following code. It creates a binary tree class. The functions are insert(), pre_order(), post_order(), in_order(). But when I debug it I get zeros values. Also I insert 9 values but only have 7 zeros. Why I did wrong?
#include <iostream>
using namespace std;
//Begin the construction of the BINARY TREE
struct tree_node {
tree_node *left;
tree_node *right;
int data;
};
//Declaring the class
class bst {
tree_node *root; //creating the root of the binary tree
public:
bst() {
root = NULL; //intialize the default construction, set the root to NULL
}
int is_empty() { //check for empty graph
return (root == NULL);
}
//Manipulating the Binary Tree
void insert(int item);
void remove_it(int value); //difficult implementation
//Graph Traversal of Binary Tree
void in_order_trav();
void in_order(tree_node *);
void pre_order_trav();
void pre_order(tree_node *);
void post_order_trav();
void post_order(tree_node *);
};
void bst::insert(int item) {
tree_node *p = new tree_node;
tree_node *parent;
p->left = NULL;
p->right = NULL;
parent = NULL;
if (is_empty()) {
root = p;
}
else {
tree_node *ptr;
ptr = root;
while (ptr != NULL) {
parent = ptr;
if (item > ptr->data)
ptr = ptr->right;
else
ptr = ptr->left;
}
if (item < parent->data)
parent->left = p;
else
parent->right = p;
}
}
/*************In Order Traversal*****************************/
// Begin
void bst::in_order_trav() {
in_order(root);
}
void bst::in_order(tree_node *ptr) {
if (ptr!=NULL) {
in_order(ptr->left);
cout << " " << ptr->data << " ";
in_order(ptr->right);
}
}
// End
/***********************************************************/
/*************Pre Order Traversal*****************************/
// Begin
void bst::pre_order_trav() {
pre_order(root);
}
void bst::pre_order(tree_node *ptr) {
if (ptr!=NULL) {
cout << " " << ptr->data << " ";
pre_order(ptr->left);
pre_order(ptr->right);
}
}
// End
/***********************************************************/
/*************Post Order Traversal*****************************/
// Begin
void bst::post_order_trav() {
post_order(root);
}
void bst::post_order(tree_node *ptr) {
if(ptr!=NULL) {
post_order(ptr->left);
post_order(ptr->right);
cout << " " << ptr->data << " ";
}
}
// End
/***********************************************************/
int main() {
bst bin_tree; //create the Binary Tree
bin_tree.insert(20);
bin_tree.insert(30);
bin_tree.insert(52);
bin_tree.insert(254);
bin_tree.insert(2);
bin_tree.insert(24);
bin_tree.insert(25);
bin_tree.insert(42);
bin_tree.insert(59);
bin_tree.in_order_trav(); //in order traversal
bin_tree.pre_order_trav(); //pre order traversal
bin_tree.post_order_trav(); //post order traversal
}
The node value should be initialized(p->data = item) at function insert() as below
void bst::insert(int item) {
tree_node *p = new tree_node;
tree_node *parent;
p->left = NULL;
p->right = NULL;
p->data = item;
parent = NULL;
... ...
}
Ok the solution is silly! -.-
I forgot to add that line in insert routine!
p->data = item;
Related
Hi I having problem when I try to implement my delete tree function in my Bst class. This is what i have implemented. Currently only have the insert and deletetree functions.
struct nodeType
{
int data;
nodeType * LeftLink;
nodeType * RightLink;
};
class Bst
{
public:
/**
* Default constructor
*/
Bst();
~Bst();
bool isEmpty() const;
void Insert(int insertItem);
void Destroy(nodeType* &p);
void DeleteTree();
bool Search(int searchItem);
void inOrderTraversal() const;
void preOrderTraversal() const;
void postOrderTraversal() const;
void inOrderTraversal(void(*visit) (int& item)) const;
private:
nodeType * root;
void inOrder(nodeType *p, void(*visit) (int &item)) const;
};
Bst::Bst()
{
root = nullptr;
}
Bst::~Bst()
{
Destroy(root);
}
bool Bst::isEmpty() const
{
return (root == nullptr);
}
void Bst::Insert(int insertItem)
{
nodeType* NewNode = new nodeType;
nodeType* trailCurrent = nullptr;
nodeType* current;
NewNode->data = insertItem;
NewNode->LeftLink = NULL;
NewNode->RightLink = NULL;
if(root == NULL)
{
root = NewNode;
}
else
{
current = root;
while(current != nullptr)
{
trailCurrent = current;
if(insertItem == current->data)
{
cout << "item already inserted, No duplicates allowed." << endl;
return;
}
else if(insertItem > current->data)
{
current = current->RightLink;
}
else
{
current = current->LeftLink;
}
}//endwhile
if(insertItem > trailCurrent->data)
{
trailCurrent->RightLink = NewNode;
cout << NewNode->data << " Inserted" << endl;
}
else
{
trailCurrent->LeftLink = NewNode;
cout << NewNode->data << " Inserted" << endl;
}
}
}
//Delete tree function
void Bst::Destroy(nodeType * &p)
{
if(p != NULL)
{
Destroy(p->LeftLink);
Destroy(p->RightLink);
delete p;
p = NULL;
}
}
void Bst::DeleteTree()
{
Destroy(root);
}
main
int main()
{
Bst intTree;
intTree.Insert(5);
intTree.Insert(3);
intTree.Insert(7);
intTree.Insert(10);
intTree.DeleteTree();
cout << "is tree empty: " << intTree.isEmpty() << endl;
}
When i try to call the DeleteTree function my programs ends without printing out the "is tree empty:" line and ends with Process returned -1073741571 (0xC00000FD).
Can anyone figure out what is happening?
EDIT::
Thanks for all the help! I have changed the insert function as it looks like I was doing the inserting wrongly. I have updated my code to the corrected version.
Yet again thanks for the help.
Please edit as follows:
while(current != nullptr)
{
trailCurrent = current;
if(current->data == insertItem)
{
cout << "item already inserted, No duplicates allowed." << endl;
return;
}
else if(current->data > insertItem)
{
current = current->RightLink;
}
else
{
current = current->LeftLink;
}
}
if(trailCurrent->data > insertItem)
{
trailCurrent->LeftLink = NewNode;
}
else
trailCurrent->RightLink = NewNode;
The problem is not with your DeleteTree method. Instead the Insert method is inserting same node at multiple places which is causing issue of dangling pointers because when two or more pointers point to single node and one is deleted then the other is pointing to a deleted node and that causes the crash when we try to access it.
Here is a simplified version of insert. This fixes the issue, although I am not sure if that is your intended implementation requirement.
class Bst
{
public:
Bst();
bool isEmpty() const;
nodeType* Insert(int insertItem, nodeType *tmpRoot=NULL);
void Destroy(nodeType*& p);
void DeleteTree();
bool Search(int searchItem);
void inOrderTraversal() const;
void preOrderTraversal() const;
void postOrderTraversal() const;
private:
nodeType* root;
};
nodeType* Bst::Insert(int insertItem, nodeType* tmpRoot)
{
nodeType* NewNode = new nodeType;
NewNode->data = insertItem;
NewNode->LeftLink = NewNode->RightLink = nullptr;
if (tmpRoot == nullptr)
{
tmpRoot = NewNode;
if (root == nullptr)
root = tmpRoot;
}
else
{
if (tmpRoot->data == insertItem)
{
cout << "item already inserted, No duplicates allowed." << endl;
return tmpRoot;
}
else if (tmpRoot->data > insertItem)
{
tmpRoot->LeftLink = Insert(insertItem, tmpRoot->LeftLink);
}
else
{
tmpRoot->RightLink = Insert(insertItem, tmpRoot->RightLink);
}
}
return tmpRoot;
}
After updating this code it should work just fine.
I'm running some tests on a simple 3 node binary search tree. Root node has a value of 1 and its left and right children have values of 0 and 2, respectively.
Here's the source code (3 files):
File name: bst.cpp
#include <iostream>
#include "bst.h"
template <typename T>
void binary_search_tree<T>::insert(const T val2ins)
{
num_nodes++;
if(!root)
{
root = new tree_node<T>(val2ins, nullptr, nullptr, nullptr);
return;
}
//loop from root until we find where to insert val2ins; seek to find a suitable parent with a nullptr
auto curr_node = root;
tree_node<T> *prev_node = nullptr;
while(curr_node)
{
prev_node = curr_node;
if(val2ins >= curr_node->val) //assign equalities on right children
{
curr_node = curr_node->right;
}
else
{
curr_node = curr_node->left;
}
}
//prev_node is the parent of curr_node
curr_node = new tree_node<T>(val2ins, prev_node, nullptr, nullptr);
//curr_node now points to a tree_node that contains a pointer to to the previous node
//we also need to go to previous_node and set its left/right children to curr_node
if(curr_node->val < prev_node->val)
{
prev_node->left = curr_node;
}
else
{
prev_node->right = curr_node;
}
}
template <typename T>
tree_node<T> *binary_search_tree<T>::get_root()
{
return root;
}
File name: bst.h
#ifndef _BST_H_
#define _BST_H_
template<typename T>
struct tree_node
{
T val;
tree_node *parent;
tree_node *left;
tree_node *right;
tree_node() : val(0), parent(nullptr), left(nullptr), right(nullptr) {}
tree_node(T val2ins, tree_node *p_ptr, tree_node *l_ptr, tree_node *r_ptr)
{
val = val2ins;
parent = p_ptr;
left = l_ptr;
right = r_ptr;
}
};
template<typename T>
class binary_search_tree
{
private:
int num_nodes;
tree_node<T> *root;
//helper function for deletion
void transplant(const tree_node<T> *node2replace, const tree_node<T> *node2insert);
public:
binary_search_tree() : num_nodes(0), root(nullptr) {}
binary_search_tree(int N, tree_node<T> *ptr) : num_nodes(N), root(ptr) {}
void insert(const T val2ins);
void delete_node(const tree_node<T> *node2del);
tree_node<T> *get_root();
// void
};
#endif
File name: main.cpp
#include <iostream>
#include "bst.h"
#include "bst.cpp"
template <typename T>
class Solution {
public:
tree_node<T> *trimBST(tree_node<T> *root, int L, int R) {
search_and_delete(root, L, R);
return root;
}
void search_and_delete(tree_node<T> *&node, const int L, const int R)
{
if(!node)
{
return;
}
if(node && node->val >= L && node->val <= R)
{
trimBST(node->right, L, R);
std::cout << node->left << std::endl;
trimBST(node->left, L, R);
std::cout << node->left << std::endl;
std::cout << node->left << std::endl;
}
else if(node && node->val > R)
{
//delete right sub tree
//then check left sub tree
//Also need to delete current node and link left (if needed)
//this can be done by simply setting current node to its left
if(node->left == nullptr && node->right == nullptr)
{
delete node;
node = nullptr;
return;
}
if(node->right)
{
delete node->right;
node->right = nullptr;
}
if(node->left)
{
node = node->left;
}
}
else if(node && node->val < L)
{
//delete left sub tree
//then check right sub tree
//Also need to delete current node and link right (if needed)
//this can be done by simply setting current node to
//its right
if(node->left == nullptr && node->right == nullptr)
{
std::cout << "deleting node 0" << std::endl;
std::cout << "Address prior to freeing: " << node << std::endl;
delete node;
node = nullptr;
std::cout << "Address after freeing: " << node << std::endl;
return;
}
if(node->left)
{
delete node->left;
node->left = nullptr;
}
if(node->right)
{
node = node->right;
}
std::cout << "done" << std::endl;
}
std::cout << "end" << std::endl;
}
};
int main(int argc, char const *argv[])
{
/* code */
binary_search_tree<int> my_tree;
Solution<int> soln;
my_tree.insert(1);
my_tree.insert(0);
my_tree.insert(2);
soln.trimBST(my_tree.get_root(), 1, 2);
return 0;
}
When I execute this code I get the following output:
0x0
0x0
0x0
end
0x7fdeaec02af0
deleting node 0
Address prior to freeing: 0x7fdeaec02af0
Address after freeing: 0x0
0x7fdeaec02af0
0x7fdeaec02af0
end
The pointer pointing to the node with value 0 is being deleted during the recursive call and set to nullptr. However, when it returns from the recursive call (where the pointer was passed by reference), the pointer is still pointing to the same memory address as it did prior to being deleted and set to nullptr.
I cannot figure out why this is happening. My only guess is that I have a memory leak somewhere that's causing issues with the pointer that I supposedly applied delete to.
At first let me to say you that your node struct just should have one content and two pointer of himself that will show the right and left children.
then for showing the BST you should cout the data NOT this pointers
class node
{
friend class BST;
public:
node(int Content=0,node* R_child = NULL, node*L_child = NULL)
{
this->R_child = R_child;
this->L_child = L_child;
this->Content = Content;
}
private:
int Content;
node*R_child;
node*L_child;
};
check this code for node class you can use template instead of integer.
Good Luck
The following is a new programmer's attempt at a Queue. It seg faults in the Push() function, when I try to print the data in the first node. Looks like front_ptr is not actually getting set in head_insert. What am I doing wrong here, is this a completely wrong approach?
Thanks.
#include <cstdlib>
#include <iostream>
using namespace std;
class node {
public:
typedef double data_type;
node(data_type init_data = 0, node * init_next = NULL) {
data = init_data;
next_node = init_next;
}
void set_data(data_type new_data) { data = new_data; }
void set_next(node * new_next) { next_node = new_next; }
data_type get_data() { return data; }
node * get_next() { return next_node; }
private:
data_type data;
node * next_node;
};
void head_insert(node::data_type val, node* head_ptr) {
node* insert_ptr = new node(val, head_ptr);
head_ptr = insert_ptr;
}
void list_insert(node::data_type val, node* prev_ptr) {
node* insert_ptr = new node(val, prev_ptr->get_next());
prev_ptr->set_next(insert_ptr);
}
void head_remove(node* head_ptr) {
node* remove_ptr = head_ptr;
head_ptr = head_ptr->get_next();
delete remove_ptr;
}
void list_remove(node * prev_ptr) {
node* remove_ptr = prev_ptr->get_next();
prev_ptr->set_next(remove_ptr->get_next());
delete remove_ptr;
}
void list_clear(node* head_ptr) {
while (head_ptr != NULL) {
head_remove(head_ptr);
}
}
class queue {
public:
queue() {
size = 0;
front_ptr = NULL;
rear_ptr = NULL;
}
//~queue() {}
bool empty() { return (size == 0);}
void push(node::data_type val) {
if (empty()) {
head_insert(val, front_ptr);
cout << "Here: " << front_ptr->get_data() << endl;
rear_ptr = front_ptr;
}
else {
list_insert(val, rear_ptr);
}
size++;
}
void pop() {
if (!empty()) {
head_remove(front_ptr);
size--;
}
}
private:
node* front_ptr;
node* rear_ptr;
int size;
};
int main() {
cout << "START" << endl;
double testVal = 1;
queue* qList = new queue();
qList->push(testVal);
cout << "END" << endl;
return 0;
}
Any help is greatly appreciated.
Your front_ptr remains null pointer in push, because head_insert accepts it by value. Dereferencing null pointer then crashes the program. Make parameters that you want to be modified by a function reference parameters like void head_insert(node::data_type val, node*& head_ptr).
Also, you can avoid crash of null pointer dereferencing by checking it before, for example like that:
cout << "Here: " << (front_ptr ? front_ptr->get_data() : 0./0.) << endl;
I am a java programmer teaching myself C++.
While writing a binary tree I found that my program did not "add" values to the tree.
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
using namespace std;
class BinaryTree {
struct Node {
public:
int val;
Node* left;
Node* right;
Node::Node(int v) {
val = v;
left = nullptr;
right = nullptr;
}
};
public:
BinaryTree() {
root = nullptr;
}
int size = 0;
int length();
bool BinaryTree::add(int v);
void printTree();
private:
void printTree(Node* n);
Node* root;
};
bool BinaryTree::add(int v) {
if (root == nullptr) {
root = new Node(v);
++size;
return true;
}
Node* ref = root;
cout << ref->val;
while (ref != nullptr) {
if (v < ref->val) {
ref = ref->left;
}
else if (v > ref->val) {
ref = ref->right;
}
else if (v == ref->val) {
return false;
}
}
Node *newNode = new Node(v);
ref = newNode;
++size;
return true;
}
void BinaryTree::printTree() {
printTree(root);
}
void BinaryTree::printTree(Node* n) {
if (n == nullptr) {
return;
}
printTree(n->left);
cout << n->val << endl;
printTree(n->right);
}
int BinaryTree::length() {
return size;
}
void main(int i) {
BinaryTree tree = BinaryTree();
tree.add(6);
tree.add(3);
tree.add(5);
tree.add(7);
tree.add(1);
tree.add(0);
tree.add(0);
tree.printTree();
cout << "binary tree sz is " << tree.length() << endl;
while (true) {};
}
I have been unable to find the problem in regards to why the tree doesn't commit new Nodes except the root.
I used "new" in the code when writing (ref = new Node) etc in the adds method because this should prevent the new Node from being destroyed once it leaves the scope.
If anyone can enlighten me on this issue I will be greatly thankful.
To add a node to the tree you have to link it to some existing node, as in
existing_node->{left or right} = new_node;
Once ref becomes nullptr, you don't have a valid existing node anymore, and it is too late to do anything. Instead, traverse the tree as long as ref->{left or right} is valid:
if (v < ref->val) {
if (ref->left) {
ref = ref->left;
} else {
ref->left = newNode;
return true;
}
}
// etc for v > ref->val
description
I don't know how to do this task.... but i just created a tree and enter value..can anyone please help me to do this task...the Stack is also of node type and we have to push value of operators like ab+ so we will push a as node then b as node and when + will come we make a tree and a and b will be its leafs node.
.Code
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *left;
Node *right;
Node()
{
data = 0;
left = NULL;
right = NULL;
}
};
class Tree
{
Node *root;
void insert(int d, Node *node)
{
if (d < node->data)
{
if (node->left == NULL)
{
Node *leaf = new Node();
leaf->data = d;
node->left = leaf;
}
else
{
insert(d, node->left);
}
}
else
{
if (node->right == NULL)
{
Node *leaf = new Node();
leaf->data = d;
node->right = leaf;
}
else
{
insert(d, node->right);
}
}
}
void inOrderDisplay(Node *subRoot)
{
if (subRoot != NULL)
{
inOrderDisplay(subRoot->left);
cout << subRoot->data << " ";
inOrderDisplay(subRoot->right);
}
}
void postOrderDisplay(Node *subRoot)
{
if (subRoot != NULL)
{
postOrderDisplay(subRoot->left);
postOrderDisplay(subRoot->right);
cout << subRoot->data << " ";
}
}
void preOrderDisplay(Node *subRoot)
{
if (subRoot != NULL)
{
cout << subRoot->data << " ";
preOrderDisplay(subRoot->left);
preOrderDisplay(subRoot->right);
}
}
void deleteSubtree(Node *subRoot)
{
if (subRoot != NULL)
{
deleteSubtree(subRoot->left);
deleteSubtree(subRoot->right);
cout << "\ndeleting: " << subRoot->data;
delete subRoot;
subRoot = NULL;
}
}
public:
Tree()
{
root = NULL;
}
~Tree()
{
deleteAll();
}
void insert(int d)
{
if (root == NULL)
{
Node *leaf = new Node();
leaf->data = d;
root = leaf;
}
else
{
insert(d, root);
}
}
void inOrderDisplay()
{
inOrderDisplay(root);
}
void postOrderDisplay()
{
postOrderDisplay(root);
}
void preOrderDisplay()
{
preOrderDisplay(root);
}
void deleteAll()
{
deleteSubtree(root);
}
};
.Main Class:
#include<iostream>
#include"task1.h"
using namespace std;
void main()
{
Tree tree;
tree.insert(10);
tree.insert(6);
tree.insert(14);
tree.insert(5);
tree.insert(8);
tree.insert(11);
tree.insert(18);
cout << endl;
system("pause");
//tree.deleteAll();
}
Based on the code you have here, you only have a void insert(int d, Node *node) function, no void insert(operator o, Node *node) function.
I think this shows that you missed an important point here. Every node in the tree can either be an integer (as you did) or an operator. In both cases, I'd call it a string. Every node that is not a leaf must be an operator, and all leafs must be integers (or strings that represents operators/integer in our case).
Then, iterating over your input, the first three item should result in something like:
+
/ \
a b
The next step would be to build more sub trees (not sure of the definition of the input you have), keep them in your stack and then construct more inner nodes of the tree.
So if the tree I showed above is called Tree(+) (for ease of use), and the initial stack was [a,b,+,c,d,e,*,*], then after one iteration you'll have [Tree(+),c,d,e,*,*] and you continue from there.