Access violation. RB tree not creating node correctly - c++

I have an RB tree. It correctly inserts the first node at the root, but when stepping through on the debugger it does not create the next nodes, and when trying to set the parent pointer causes an access violation because it failed to create the node whose parent node pointer it is trying to set.
Source code:
core.h
#include <iostream>
#ifndef CORE_H_
#define CORE_H_
struct node {
public:
node(double);
bool color; //false = red, true = black
double key_value;
node *left_child;
node *right_child;
node *parent;
};
class red_black_tree {
public:
red_black_tree();
~red_black_tree();
void print_tree();
void insert(double key);
// node *search(double key);
// void delete_leaf(double key);
private:
node *root;
node *get_uncle(node *);
node *get_grand(node *);
void insert_case1(node *);
void insert_case2(node *);
void insert_case3(node *);
void insert_case4(node *);
void insert_case5(node *);
void rotate_left(node *);
void rotate_right(node *);
void insert(node *leaf, double key);
//node *search(node *leaf, double key);
// void delete_leaf(double key);
void print_tree(node *leaf);
void destroy_tree(node *leaf);
};
#endif
core.cpp
#include "core.h"
#include <iostream>
node::node(double key) {
left_child = right_child = parent = nullptr;
color = false;
key_value = key;
}
red_black_tree::red_black_tree() {
root = nullptr;
}
red_black_tree::~red_black_tree() {
if (root != nullptr) {
destroy_tree(root);
}
}
void red_black_tree::destroy_tree(node *leaf) {
if (leaf->left_child!= nullptr) {
destroy_tree(leaf->left_child);
}
if (leaf->left_child != nullptr) {
destroy_tree(leaf->right_child);
}
delete leaf;
}
node *red_black_tree::get_grand(node *leaf) {
if (leaf->parent != nullptr && leaf->parent->parent != nullptr)
return leaf->parent->parent;
else return nullptr;
}
node *red_black_tree::get_uncle(node *leaf) {
node *g = get_grand(leaf);
if (g == nullptr)
return nullptr;
if (leaf->parent == g->left_child)
return g->right_child;
else return g->left_child;
}
void red_black_tree::insert(double key) {
if (root == nullptr) {
root = new node(key);
insert_case1(root);
}
else insert(root, key);
}
void red_black_tree::insert(node *leaf, double key) {
//normal recursive binary tree insertion
if (leaf == nullptr) {
leaf = new node(key);
}
else if (key < leaf->key_value) {
insert(leaf->left_child, key);
leaf->left_child->parent = leaf;
insert_case1(leaf);
}
else if (key >= leaf->key_value) {
insert(leaf->right_child, key);
leaf->right_child->parent = leaf;
insert_case1(leaf);
}
}
void red_black_tree::rotate_left(node *leaf) {
node *grand = get_grand(leaf), *s_parent = grand->left_child, *left = leaf->left_child;
grand->left_child = leaf;
leaf->left_child = s_parent;
s_parent->right_child = left;
s_parent->parent = leaf;
leaf->parent = grand;
}
void red_black_tree::rotate_right(node *leaf) {
node *grand = get_grand(leaf), *s_parent = grand->right_child, *right = leaf->right_child;
grand->right_child = leaf;
leaf->right_child = s_parent;
s_parent->left_child = right;
s_parent->parent = leaf;
leaf->parent = grand;
}
void red_black_tree::insert_case1(node * leaf) {
if (leaf->parent == nullptr) {
leaf->color = true;
}
else {
insert_case2(leaf);
}
}
void red_black_tree::insert_case2(node *leaf) {
if (leaf->parent->color == true) {
return;
}
else
insert_case3(leaf);
}
void red_black_tree::insert_case3(node *leaf) {
node *uncle = get_uncle(leaf), *grand;
if ((uncle != nullptr) && (uncle->color == false)) {
leaf->parent->color = true;
uncle->color = true;
grand = get_grand(leaf);
grand->color = false;
insert_case1(grand);
}
else {
insert_case4(leaf);
}
}
void red_black_tree::insert_case4(node *leaf) {
node *grand = get_grand(leaf);
if ((leaf == leaf->parent->right_child) && (leaf->parent == grand->left_child)) {
rotate_left(leaf);
leaf = leaf->left_child;
}
else if ((leaf == leaf->parent->left_child) && (leaf->parent == grand->right_child)) {
rotate_right(leaf);
leaf = leaf->right_child;
}
insert_case5(leaf);
}
void red_black_tree::insert_case5(node *leaf) {
node *grand = get_grand(leaf);
leaf->parent->color = true;
grand->color = false;
if (leaf == leaf->parent->right_child) {
rotate_right(grand);
}
else
rotate_left(grand);
}
void red_black_tree::print_tree() {
print_tree(this->root);
}
void red_black_tree::print_tree(node *leaf) {
if (leaf != nullptr) {
print_tree(leaf->left_child);
std::cout << leaf->key_value;
print_tree(leaf->right_child);
}
}
main.cpp
#include "core.h"
int main() {
red_black_tree tree;
tree.insert(10);
tree.insert(5);
tree.insert(15);
tree.print_tree();
system("pause");
}
Ok, so in the example main(), the first insertion tree.insert(10), works, it calls the non recursive insert, which determines the root is NULL and then sets the root pointer to a new node with the key of 10. The second tree.insert(5), calls the non recursive insert, determines the root exists, and then calls the recursive insert insert(root, 5). This determines that the root is not NULL, then goes to the first of the if/else statements, and recursively calls insert like insert(root->left_child, 5). This determines that root->left_child is NULL and creates a new node at the address for root->left_child. The function then goes back up to the original insert call and tries to set the left child's parent to the root, causing an access violation because somehow the left child was not set. Why isn't the insert function setting the node, my debugger says when I step through that the node is created at the last level of recursion using leaf = new node(5), but when it steps back out a level the node is still NULL at that address?

Look at this part of your code:
void red_black_tree::insert(node *leaf, double key) {
//normal recursive binary tree insertion
if (leaf == nullptr) {
leaf = new node(key);
}
else if (key < leaf->key_value) {
insert(leaf->left_child, key);
leaf->left_child->parent = leaf;
insert_case1(leaf);
} ...
What happens at insert(5) in main?
The second if condition succeeds, so you call insert(leaf->left_child, key), but since the leaf here is the root, it does not have any children yet, so we enter again the same method with leaf = nullptr. When leaf == nullptr (the first if condition), you just create a node but you do not attach it anywhere to your tree.
This explains why you are having this pattern. Now the whole process of tree creation needs rethinking. The problem is that almost everywhere any node has a non-null left and right children.

Related

void insert(int ) method for BST tree/C++

I have been trying to get this function working for the longest time now. It is part of an assignment for an online course, but it seems no matter what I submit, the function fails for both the empty child test and the left child test. See code below. The main() function is deliberately commented out. Any info./input is much appreciated.
// C++ binary trees and stuff;
//
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;
class BST
{
public:
int data;
BST *left;
BST *right;
//BST *root;
// BST() constructor
BST (int num)
{
data = num;
left = nullptr;
right = nullptr;
root = nullptr;
}
// constructors for root node(s), initializing as root when no values exist yet;
BST() : root (nullptr){}
BST (BST *rootNode) : root(rootNode){}
void insert (int value)
{
BST *newNode = new BST();
newNode = root;
if (root == nullptr)
{
root = new BST (value);
}
else
{
root->data = value;
}
// check if newNode's value equals the passed-in value:
if (value == root->data)
{
//cout << "\nWarning! Value already exists in tree, so nothing will be done.\n";
return;
}
// check if value is < or > newNode's value:
if (value <= root->data)
{
if (root->left == nullptr)
{
// make a new node as the left child of this node,
root->left = new BST(value);
}
else
{
// recursively call insert() on tree's left side,
root->left->insert(value);
}
}
else
{
if (root->right == nullptr)
{
// make a new node as the right child of this node,
root->right = new BST(value);
}
else
{
// recursively call insert() on tree's right side,
root->right->insert(value);
}
}
}
public:
BST *root;
};
/*
int main (int argc, char *argv[])
{
//...insert code here,
// create nodes,...
BST rootNode(5);
BST leftNode(4);
BST rightNode(6);
// connect the nodes to the tree via rootNode.left and rootNode.right,..
rootNode.left = &leftNode;
rootNode.right = &rightNode;
printf ("\nData (root) value = %i, rootNode.left = %i, and rootNode.right = %i\n",
rootNode.data, rootNode.left->data, rootNode.right->data);
cout << "\n\nHello, Solar System!\n";
return 0;
}
*/
Okay, here's my suggestion. You need to reformat your code. You need two classes. You need a BST, and you need a Node. The various methods to add/remove/traverse are part of the BST class. Nodes are just Nodes.
So:
class BST_Node {
public:
int value;
BST_Node * left = nullptr;
BST_Node * right = nullptr;
// Define constructors, etc.
};
class BST {
public:
BST_Node * root = nullptr;
BST_Node * insert(int value);
void insertNode(BST_Node *node);
void insertNodeBelow(BST_Node *nodeToInsert, BST_Node *startingNode);
};
BST_Node * BST::insert(int value) {
BST_Node * node = new BST_Node(value);
insertNode(node);
return node;
}
void BST::insertNode(BST_Node *node) {
if (node == nullptr) {
return;
}
if (root == nullptr) {
root = node;
}
else {
insertNodeBelow(node, root);
}
}
void BST::insertNodeBelow(BST_Node *node, BST_Node *startingNode) {
if (node == nullptr || startingNode == nullptr) {
return;
}
if (node->value < startingNode->value) {
if (startingNode->left != nullptr) {
insertNodeBelow(node, startingNode->left);
}
else {
startingNode->left = node;
}
}
else {
if (startingNode->right != nullptr) {
insertNodeBelow(node, startingNode->right);
}
else {
startingNode->right = node;
}
}
}
How this works... First, the logic of how to store nodes is in BST. Nodes don't care. Second, I made methods for either inserting a value or a node. Because I think that's handy. That should be fairly easy to understand.
The root node can be null, if so, then your inserted node is now root. Otherwise it calls the recursive insertion function. Now, you could simplify this a little, but I didn't want to get too clever.
So it's simple. We look to see where it belongs relative to the point we're at (initially the root). Either we go into the left branch or the right branch. But that branch could be empty, so you just plop it right in. If it's not empty, then you recurse.
I didn't test it.

How to implement complete binary in c++?

I want to try make insertion of complete binary tree using recursion . I make a piece of code and I cannot catch problem why value not inserted. I make height function and count nodes function with help of these function and recursive call I want to insert new node in Complete binary tree. In main get root by using get root function then send to insert function
#include<iostream>
#include<math.h>
using namespace std;
struct node{
int data;
node *left,*right;
};
class cbt{
node *root;
public:
cbt()
{
root=NULL;
}
node* get_node()
{
return root;
}
node* newNode(int key)
{
node* temp1 = new node;
temp1->data = key;
temp1->left = temp1->right = NULL;
return temp1;
}
void CBT_inseration(node* temp,int data)
{
node *ptr;
ptr=newNode(data);
if(root==NULL)
{
root=ptr;
return;
}
else
{
height = f_height(temp->left);
int excepted_node = pow(2,height)-1;
int left_tree_node_count = countNumNodes(temp->left);
int right_tree_node_count = countNumNodes(temp->right);
if(left_tree_node_count==right_tree_node_count)
{
CBT_inseration(temp->left,data);
}
else if(excepted_node != left_tree_node_count)
{
if(temp->left == NULL)
{
temp->left = ptr;
return;
}else
{
CBT_inseration(temp->left,data);
}
}
else if(temp->right == NULL)
{
temp->right=ptr;
return;
}
else if(excepted_node != left_tree_node_count)
{
if(temp->left == NULL)
{
temp->left=ptr;
return;
}
else
{
CBT_inseration(temp->right,data);
}
}
}
}
void print(node *root) {
if (root == NULL)
return;
print(root->left);
cout << root->data << " ";
print(root->right);
}
};
int main()
{
cbt obj;
node *r=NULL;
obj.CBT_inseration(obj.get_node(),4);
obj.CBT_inseration(obj.get_node(),3);
obj.CBT_inseration(obj.get_node(),5);
obj.CBT_inseration(obj.get_node(),8);
obj.print(obj.get_node());
return 0;
}
You would need to go through a debugger to see what is wrong with your code. I will tell you how I would do this:
First, you need a function to check if the tree is full. We will reuse your functions to do this:
bool isTreeFull(node* head) {
return head != NULL && countNumNodes(head) == (1 << find_height(head)) - 1;
}
Then for inserting I have to check if the number of nodes on each side are the same. This tells me that I am allowed to move one level deeper on the left side. If the number of nodes aren't the same, then I only move on to insert on the right subtree if the left subtree is full:
void CBT_inseration(int data) {
root = insert(root, data);
}
node* insert(node* head, int data) {
if (head == NULL) {
head = newNode(data);
} else {
int leftCount = countNumNodes(head->left);
int rightCount = countNumNodes(head->right);
if (leftCount == rightCount) {
head->left = insert(head->left, data);
} else {
if (isTreeFull(head->left)) {
head->right = insert(head->right, data);
} else {
head->left = insert(head->left, data);
}
}
}
return head;
}
Then you would call it like:
cbt obj;
obj.CBT_inseration(4);
obj.CBT_inseration(3);
obj.CBT_inseration(5);
obj.CBT_inseration(6);
obj.CBT_inseration(8);
obj.print(obj.get_node()); // 6 3 8 4 5

Not able to traverse elements in Binary search tree in C++

I am trying to print all the elements using inorder traversal, but my code is giving incorrect output. I am not able to figure out what went wrong.
#include <iostream>
using namespace std;
struct node
{
int value;
struct node * left;
struct node * right;
};
class Btree
{
node * root;
void insert(int value, node * leaf);
node * search(int value, node * leaf);
void destroy(node * leaf);
public:
Btree() { root = NULL; }
~Btree() { destroy(); }
void insert(int value);
node * search(int value);
void destroy();
void inorder(node * ptr);
};
void Btree::inorder(node *ptr)
{
if (ptr != NULL)
{
inorder(ptr->left);
cout << ptr->value << "";
inorder(ptr->right);
}
}
void Btree::destroy(node *leaf)
{
if(leaf!=NULL)
{
destroy(leaf->left);
destroy(leaf->right);
delete leaf;
}
}
void Btree :: destroy()
{
destroy(root);
}
node *Btree::search(int value, node *leaf)
{
if(leaf!=NULL)
{
if(value==leaf->value)
return leaf;
if(value<leaf->value)
return search(value, leaf->left);
else
return search(value, leaf->right);
}
else return NULL;
}
node *Btree::search(int value)
{
return search(value, root);
}
void Btree::insert(int value, node *leaf)
{
if(value< leaf->value)
{
if(leaf->left!=NULL)
insert(value, leaf->left);
else
{
leaf->left=new node;
leaf->left->value=value;
leaf->left->left=NULL; //Sets the left child of the child node to null
leaf->left->right=NULL; //Sets the right child of the child node to null
}
}
else if(value>=leaf->value)
{
if(leaf->right!=NULL)
insert(value, leaf->right);
else
{
leaf->right=new node;
leaf->right->value=value;
leaf->right->left=NULL; //Sets the left child of the child node to null
leaf->right->right=NULL; //Sets the right child of the child node to null
}
}
}
void Btree::insert(int value)
{
if(root!=NULL)
insert(value, root);
else
{
root=new node;
root->value=value;
root->left=NULL;
root->right=NULL;
}
}
int main()
{
Btree bst;
struct node * root = NULL;
root = new node;
bst.insert(10);
bst.insert(6);
bst.insert(14);
bst.insert(3);
bst.insert(8);
bst.insert(7);
bst.insert(9);
bst.insert(5);
bst.inorder(root);
return 1;
}
I have tried to create a memory pointer that is the root and then passing that root in my inorder function call. My inorder should print me the output
3 5 6 7 8 9 10 14
A pointer to a step-debugger would help me in solving the issue I believe.Or if anyone can provide me the corrected code, would be quite helpful
Edit:- I had previously passed ptr but used root in my inorder function definition (due to which I was getting Bus error), but now it is correected. However, I still do not get the right output. The output I get is 0

Binary Search Tree Using Classes

I have been trying to implement binary search tree using classes. Every time I try to compile and run the program, the program ends. I have tried many things like making the *root public to access it in main so I can update the root, but somehow it becomes null every time.
Help will be appreciated.
This is for my university project.
#include <iostream>
using namespace std;
class tree;
class Node {
friend class tree;
private:
Node *lchild,*rchild;
int data;
public:
Node (int x) {
data = x;
lchild = rchild = NULL;
}
};
class tree {
protected:
Node* root;
void inorder(const Node* root)const;
public:
tree () {
root = NULL;
}
bool insert(int item);
void inorder() const {inorder(root);};
Node* getroot() {
return root;
}
};
bool tree :: insert(int item) {
if (root == NULL) {
Node *temp = new Node(item);
root = temp;
return (bool) root;
}
if (item < root -> data) {
insert(item);
}
if (item > root -> data) {
insert(item);
}
else if (item == root -> data) {
cout<<"Duplicate";
exit (0);
}
return (bool) root;
}
void tree :: inorder(const Node *root)const {
if (root != NULL) {
inorder(root -> lchild);
cout<<root -> data;
inorder(root -> rchild);
}
}
int main()
{
tree obj1;
obj1.insert(3);
//obj1.insert(4);
obj1.insert(1);
//obj1.insert(5);
obj1.inorder();
}
/* Program to implement Binary Search Tree in c++ using classes and objects */
#include<iostream>
#include<stdlib.h>
#include<cstdlib>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
};
class BinaryTree {
private:
struct Node* root;
public:
BinaryTree() {
root = NULL;
}
Node* createNode(int);
Node* insertNode(Node*, int);
Node* deleteNode(Node*, int);
void inOrder(Node*);
void preOrder(Node*);
void postOrder(Node*);
Node* findMinimum(Node*);
/* accessor function helps to
get the root node in main function
because root is private data member direct access is not possible */
Node* getRoot() {
return root;
}
/* mutator method helps to update root ptr after insertion
root is not directly updatable in the main because its private data member */
void setRoot(Node* ptr) {
root = ptr;
}
};
/* Helper function to create a new node in each function call of insertNode */
Node* BinaryTree :: createNode(int n) {
Node* newNode = new struct Node();
newNode->data = n;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
/* Helps to get inorder predessor to delete the node from tree */
Node* BinaryTree :: findMinimum(Node* rootPtr) {
while(rootPtr->left != NULL) {
rootPtr = rootPtr->left;
}
return rootPtr;
}
/* insertion of the Node */
Node* BinaryTree :: insertNode(Node* rootPtr, int n) {
if(rootPtr == NULL) {
return createNode(n);
}
if(n < rootPtr->data) {
rootPtr->left = insertNode(rootPtr->left, n);
}
if(n > rootPtr->data) {
rootPtr->right = insertNode(rootPtr->right, n);
}
return rootPtr;
}
/* function to delete the Node */
Node* BinaryTree :: deleteNode(Node* rootPtr, int n) {
if(rootPtr == NULL) {
cout<<"Node to be deleted is not present.!"<<endl;
return rootPtr;
}
else if(n < rootPtr->data) {
rootPtr->left = deleteNode(rootPtr->left, n);
} else if(n > rootPtr->data) {
rootPtr->right = deleteNode(rootPtr->right, n);
} else {
if(rootPtr->left == NULL && rootPtr->right == NULL) {
delete rootPtr;
rootPtr = NULL;
}
else if(root->left == NULL) {
struct Node* temp = rootPtr;
rootPtr = rootPtr->right;
delete temp;
}
else if(rootPtr->right == NULL) {
struct Node* temp = rootPtr;
rootPtr = rootPtr->left;
delete temp;
} else {
Node* temp = findMinimum(rootPtr->right);
rootPtr->data = temp->data;
rootPtr->left = deleteNode(rootPtr->right, temp->data);
}
}
return rootPtr;
}
/* all traversal technique */
void BinaryTree :: inOrder(Node* root) {
if(root == NULL) {
return;
}
inOrder(root->left);
cout<<root->data<<"\t";
inOrder(root->right);
}
void BinaryTree :: preOrder(Node* root) {
if(root == NULL) return;
cout<<root->data<<"\t";
preOrder(root->left);
preOrder(root->right);
}
void BinaryTree :: postOrder(Node* root) {
if(root == NULL) return;
postOrder(root->left);
postOrder(root->right);
cout<<root->data<<"\t";
}
int main() {
BinaryTree l1;
int ch, ele, res;
Node* ptr;
do {
cout<<"1 - Insert Node\n";
cout<<"2 - IN-ORDER Traversal\n";
cout<<"3 - PRE-ORDER Traversal\n";
cout<<"4 - POST-ORDER Traversal\n";
cout<<"Enter choice\n";
cin>>ch;
switch(ch) {
case 1:
cout<<"Entre element to insert to the List\n";
cin>>ele;
/* calling insertNode function by passing root ptr to the function,
root ptr can be obtained by accessor function getRoot() */
ptr = l1.insertNode(l1.getRoot(), ele);
/* updating the root ptr*/
l1.setRoot(ptr);
break;
case 2:
cout<<"---IN-ORDER TRAVERSAL---"<<endl;
l1.inOrder(l1.getRoot());
cout<<endl;
break;
case 3:
cout<<"---PRE-ORDER TRAVERSAL---"<<endl;
l1.preOrder(l1.getRoot());
cout<<endl;
break;
case 4:
cout<<"---POST-ORDER TRAVERSAL---"<<endl;
l1.postOrder(l1.getRoot());
cout<<endl;
break;
case 5:
cout<<"Enter node to be deleted."<<endl;
cin>>ele;
ptr = l1.deleteNode(l1.getRoot(), ele);
l1.setRoot(ptr);
default: cout<<"Invalid choice"<<endl;
}
} while(ch >=1 && ch <= 5);
return 0;
}
The reason why root gets NULL again and again is that it actually never changes its value to something else than NULL.
Maybe you have introduced this behaviour in your code in the course of fixing some other issues; yet you assign root=NULL in the constructor; afterwards, you assign only obj.root1 = ..., while you return root in getroot() { return root; }. Further, you pass Node *root as parameter in you insert function; Note that this local variable named root hides data member root, such that root->... in these functions will always address the local variable and not the data member.
Before diving around in code that's interface needs a redesign, I'd suggest to adapt the design and then adapt the code; I'm pretty sure the errors will simply go away. I'd suggest to adapt the interface of class tree as follows and write the code around it.
Member function inorder() should be const to indicate that it does not alter the object's state. Note that const-member functions can - in contrast to other non-static member functions - be called on const-objects.
class Node {
friend class tree;
private:
Node *lchild,*rchild;
int data;
public:
Node (int x) {
data = x;
lchild = rchild = NULL;
}
};
class tree {
public:
tree () { root = NULL; }
bool insert(int item) { return insert(item,root); };
void inorder() const { inorder(root);};
protected:
Node* root;
void inorder(const Node* curr) const;
bool insert(int item, Node* curr);
};
bool tree :: insert(int item, Node *currNode) {
if (root == NULL) {
root = new Node(item);
return true;
}
else if (item < currNode->data) {
if (currNode->lchild == NULL) {
currNode->lchild = new Node(item);
return true;
}
else {
return insert(item, currNode->lchild);
}
}
else if (item > currNode->data) {
if (currNode->rchild == NULL) {
currNode->rchild = new Node(item);
return true;
}
else {
return insert(item, currNode->rchild);
}
}
else // item == currNode->data
return false; // duplicate; do not insert
}
The biggest problem with your code are the following lines:
if (item < root -> data) {
insert(item);
}
if (item > root -> data) {
insert(item);
}
Basically you are saying that if the item is larger or smaller than the root data you will call the function again with the same item, you never changed the item and you will basically do this an infinity amount of times.....

How can I create the tree?

I'm trying to make a BST and need to print it inorder, postorder, and preorder.
The thing am not sure about is how to create this tree in my main() function.
struct Tree_Node
{
Tree_Node *right;
Tree_Node *left;
int info;
};
class bTree
{
private:
Tree_Node *root;
public:
bTree();
void bTree::Insert(Tree_Node*& tree, int item);
void bTree::preorderPrint(Tree_Node *root);
};
bTree::bTree()
{
root = NULL;
}
void bTree::Insert(Tree_Node*& tree, int item)
{
if (tree == NULL)
{
tree = new Tree_Node;
tree->right = NULL;
tree->left = NULL;
tree->info = item;
}
else if (item < tree->info)
Insert(tree->left, item);
else
Insert(tree->right, item);
}
void bTree::preorderPrint(Tree_Node *root)
{
if ( root != NULL )
{
cout << root->info << " ";
preorderPrint( root->left );
preorderPrint( root->right );
}
}
void main()
{
// This is where I need help at
// I'm not sure how to insert a new node
bTree Test;
Test.Insert(
}
By the looks of things, you can just write
Test.Insert(Test.root, 3); // Insert 3
Test.Insert(Test.root, 4); // Insert 4
and that should work. Of course, you'll have to make root public.
However, this is a bit awkward, since the first parameter will always be bTree.root - and you don't need to make that public. Remember that the user of your data type (you or anyone else) shouldn't have to care about internals such as nodes - they only care about their data. Instead, I'd recommend making a convenience Insert method which only needs to take an integer (not a tree node) - this is called Overloading.
void bTree::Insert(int item)
{
Insert(root, item);
}
// Keep the other insert method, but make it private.
Then you can just write:
Test.Insert(3);
Test.Insert(4);
void bTree::Insert(int item)
{
Tree_Node * node = new Tree_Node;
node->left = NULL;
node->right = NULL;
node->info = item;
if (root == NULL)
{
root = node;
return;
}
Tree_Node * t = root;
Tree_Node * p = root;
while(1)
{
if (item < t->info)
{
t = t->left;
if(t == NULL)
{
p->left = node;
return;
}
}
else if(item > t->info)
{
t = t->right;
if(t == NULL)
{
p->right = node;
return;
}
}
else //item already exists in the tree
return;
p = t;
}
}
//now you can insert nodes like
Test.Insert(5);
Test.Insert(6);