This code is part of a RedBlack Tree program supposed to receive "ADSOMERT" and sort its characters into Inorder form "A D E M O R S T". It does the job, but with the ASCII numbers of each character "65 68 69 77 79 82 83 84".
Main function:
int main()
{
Tree char_tree = Tree();
cout << "Eingabe: ADSOMERT" << endl;
char_tree.insert('A');
char_tree.insert('D');
char_tree.insert('S');
char_tree.insert('O');
char_tree.insert('M');
char_tree.insert('E');
char_tree.insert('R');
char_tree.insert('T');
cout << "ADSOMERT in In-Order" << endl;
char_tree.print_inorder();
}
insert and rb_insert fucntions:
void Tree::insert(int x)
{
...
}
void Tree::rb_insert(Node *& node, Node *&parent, int x, bool sw)
{
...
}
inorder and print_inorder functions:
void Tree::print_inorder()
{
inorder(head->right);
cout << endl;
}
void Tree::inorder(Knote* node)
{
if (node != nullptr)
{
inorder(node->left);
cout << node->item << " ";
inorder(node->right);
}
}
class dependencies for Node and Tree:
Node.h
class Node {
public:
Node(int data = 0);
bool red;
int item;
Node *left;
Node *right;
Node *parent;
};
Node.cpp
Node::Node(int data)
{
this->item = data;
parent = nullptr;
left = nullptr;
right = nullptr;
red = true;
}
void Tree::inorder(Knote* node)
{
if (node != nullptr)
{
inorder(node->left);
cout << node->item << " ";
inorder(node->right);
}
}
class Node {
public:
Node(int data = 0);
bool red;
int item;
Node *left;
Node *right;
Node *parent;
};
You are outputting node->item and it is an int. Should be a char if you want to output "A D E M O R S T"
template <class T>
class Node{
public:
Node(int data = 0);
bool red;
T item;
....
};
Something like this
Related
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
When trying to add a new Node to the liked list it gives me a Segmentation fault. Can someone tell me what is wrong in the implementation of the addBook() function. I am not sure if it's the implementation of the function that is wrong, or the way that I've declared the classes.
class Reservation {
public:
int getID();
string getResevNum();
void setId(int x);
void setReseNum(string y);
private:
int ID;
string reservedNumber;
};
class ReservationCollection {
public:
ReservationCollection();
~ReservationCollection();
int getUserId(int &id);
string getUserBook(string &bookCall);
void findReservation();
void display();
void addBook(int id, string book);
void RemoveBook();
void ShutDown();
private:
struct Node {
Reservation *data;
Node *next;
};
Node *head;
};
ReservationCollection::ReservationCollection() {
Node *head = new Node;
head->next = NULL;
}
ReservationCollection::~ReservationCollection() {
}
void ReservationCollection::addBook(int id, string book){
Node *tmp = new Node;
tmp->data->setId(id);
tmp->data->setReseNum(book);
tmp->next = head->next;
head->next = tmp;
cout <<"Good\n";
}
int Reservation::getID(){
return ID;
}
string Reservation::getResevNum(){
return reservedNumber;
}
void Reservation::setId(int x){
ID = x;
}
void Reservation::setReseNum(string y){
reservedNumber = y;
}
int ReservationCollection::getUserId(int &id){
cout << "Enter Id number " << endl;
cin >> id;
return id;
}
string ReservationCollection::getUserBook(string &bookCall){
cout << "Enter book reservatin " << endl;
cin >> bookCall;
return bookCall;
}
int main()
{
int ID;
string BookNum;
char cmd;
do {
cout << "Enter command: ";
cin >> cmd;
ReservationCollection list;
if (cmd == 'A' || cmd == 'a'){
list.getUserId(ID);
list.getUserBook(BookNum);
list.addBook(ID, BookNum);
}
else if (cmd == 'S' || cmd == 's'){
cout << " list";
}
} while (cmd != 'Q' || cmd == 'q');
}
Wish I could just comment this:
All instances of the line Node *tmp = new Node; should read Node *tmp = new Node();
Don't use pointers if you don't have to. In this example Node has to be a pointer, because the list may have zero nodes, or 1000 nodes, so we need pointers to dynamically allocate memory on demand.
But (Reservation)data does not have to be a pointer. Each node always has one Reservation member.
If you do declare it as pointer then you must allocate it, and free it when it is no longer needed.
head should be initialized to NULL, because the single linked-list has no nodes when it is initialized. The first node inserted becomes the head.
#include <iostream>
#include <string>
using namespace std;
class Reservation
{
private:
int ID;
string reservedNumber;
public:
int getID() { return ID; }
string getResevNum() { return reservedNumber; }
void setId(int x) { ID = x; }
void setReseNum(string y) { reservedNumber = y; }
};
class ReservationCollection
{
public:
struct Node
{
Reservation data;
Node *next;
};
ReservationCollection();
~ReservationCollection();
void addBook(int id, string book);
Node* getHead() { return head; }
private:
Node *head;
};
ReservationCollection::ReservationCollection()
{
head = NULL;
}
ReservationCollection::~ReservationCollection()
{
Node *p = head;
while (p)
{
Node *next = p->next;
cout << "delete: " << p->data.getID() << ", " << p->data.getResevNum() << endl;
delete p;
p = next;
}
}
void ReservationCollection::addBook(int id, string book)
{
Node *node = new Node;
node->data.setId(id);
node->data.setReseNum(book);
//this element is the last element
node->next = NULL;
if (!head)
{
//first element inserted
head = node;
head->next = NULL;
}
else
{
//find the previous node in the list
Node *prev = head;
while (prev->next)
prev = prev->next;
//this node is after previous node
prev->next = node;
}
cout << "Good\n";
}
int main()
{
ReservationCollection list;
list.addBook(0, "Book0");
list.addBook(1, "Book1");
list.addBook(2, "Book2");
list.addBook(3, "Book3");
ReservationCollection::Node *p = list.getHead();
while (p)
{
ReservationCollection::Node *next = p->next;
cout << p->data.getID() << ", " << p->data.getResevNum() << endl;
p = next;
}
}
Creating a Node wont create the structure that Reservation *data; tries to point to, and so you try to access non initialized memory.
You need to initialize data:
tmp->data = new Reservation();
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.
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;
I am having difficulty printing out an Inorder binary tree. When I run my program and enter my input, it prints the Inorder after each character.
For example, if I enter ABCD, it will print:
Inorder: A
Inorder: AB
Inorder: ABC
Inorder: ABCD
However I only want to print out that last line.
This is the code that I have:
#include <iostream>
using namespace std;
template <class T>
class BinaryTree
{
private:
struct TreeNode
{
TreeNode *left;
TreeNode *right;
T data;
};
TreeNode *root;
public:
BinaryTree()
{
root = NULL;
}
void Inorder(TreeNode *n)
{
if(n != NULL)
{
Inorder(n -> left);
cout<< n -> data;
Inorder(n -> right);
}
}
void PrintInorder()
{
Inorder(root);
}
void InsertData(T data)
{
TreeNode *t = new TreeNode;
TreeNode *parent;
t -> data = data;
t -> left = NULL;
t -> right = NULL;
parent = NULL;
//is this a new tree?
if (isEmpty())
root = t;
else
{
TreeNode *curr;
curr = root;
while(curr)
{
parent = curr;
if (t -> data > curr -> data)
curr = curr -> right;
else
curr = curr -> left;
}
if(t -> data < parent -> data)
parent -> left = t;
else
parent -> right =t;
}
}
bool isEmpty()
{
return (root == NULL);
}
};
int main()
{
BinaryTree <char> BT;
char num;
while (cin >> num)
{
BT.InsertData(num);
cout << "Inorder: ";
BT.PrintInorder();
cout << endl;
}
return 0;
}
Don't print anything until you've read all the numbers.
while (cin >> num)
{
BT.InsertData(num);
}
cout << "Inorder: ";
BT.PrintInorder();
cout << endl;