Jump to content


george_ya

Member Since 13 Oct 2008
Offline Last Active May 14 2009 06:01 AM
-----

Topics I've Started

binarytree begining, need help

13 May 2009 - 01:37 PM

hi. I am working on binary tree, but jst want to see is everything looking good so far.
code:
#include<iostream>
#include<stdio.h>
#include <string>
using namespace std;
struct node
{
  string key_value;
  node *left;
  node *right;
}*p_akt;
class btree
{
    public:
        btree();
        ~btree();
		void create();
		bool Lchild(node);
		bool Rchild(node);
		void make(node,string question,string animal);
		void data(node *p);
		void root();
		void Parent(node *p,node * root);
		int brojac;
        //void insert(string key);
        //node *search(string key);
        //void destroy_tree();
	private:
		
		
};

btree::btree()
{
	p_akt=0;
	brojac=0;
 // root=NULL;
}

void btree::create()
{
	node * temp=p_akt;
	
}

void btree::make(node,string question,string animal)
{
	node * temp=p_akt;
	temp=p_akt;
	temp->left=new node;
	temp->left->key_value= question;
	temp->left->right=new node;
	temp->left->right->key_value= animal;
}

bool Lchild(node)
{
	node * temp = p_akt;
	if(temp->left!=NULL)
	{
		temp=temp->left;
		return true;
	}
	else
		return false;
}
bool Rchild(node *p)
{
	node * temp = p;
	if(temp->right!=NULL)
	{
		temp=temp->right;
		return true;
	}
	else
		return false;
}
void btree::data(node *p)
{
	cout<< (string)p->key_value<<endl;
}
void btree::Parent(node *p,node *root)
{
	node * temp = root;
	if (temp==NULL)
		return;
	else if  ((temp->left!=NULL) || (temp->right!=NULL))
	{
		if((temp->left->key_value==p->key_value) || (temp->right->key_value==p->key_value))
			temp=p;
	}
	else
	{
		node * temp2=temp;
		temp=temp->left;
		Parent(p,temp);
		temp2=temp->right;
		Parent(p, temp2);
	}

}
i need one more function, which will take me back to the root, but don't know how to write it.

stack problem: print after pop function

06 April 2009 - 05:44 PM

have a problem with my program. It should be program that recognize palindome through the stack. Everything works great, only thing that don't work is printing stacks(original and reversed) after the funcion is done. Here is my entire code, and the problem is at case d and e:
#include <iostream>

using namespace std;


const int MAXSTACK = 21;
class stack {
private:
    int  stop;  
    char stk[MAXSTACK];
public:
    stack();
    ~stack();
    stack(const stack& s);
    void push(const char c);
    char pop();
    char top(void);
    int  emptystack(void);
    int  fullstack(void);
    void stack_print(void);
    int stack::create(void);
};
stack::stack()
{
    stop = 0;
}
stack::~stack() { }  
stack::stack(const stack& s)
{
    stop = s.stop;
    strcpy(stk,s.stk);
}
void stack::push(const char c)
{
    stk[stop++] = c;
}
char stack::pop()
{
    return stop--;
}
char stack::top(void)
{
    return stk[stop - 1];
}
int  stack::emptystack(void)
{
    return !stop; 
}
int  stack::fullstack(void)
{
    return stop == MAXSTACK;
}
void stack::stack_print(void)
{
    for (int i=0; i<stop; i++)
        cout<<stk[i];
    cout<<endl;
}
int  stack::create(void)
{
    return !stop; 
}
char menu()
{

    char volba;

    cout<<"\n";
    cout<<" **********.\n";
    cout<<"\n";
    cout<<" a ... make new containers\n";
    cout<<" b ... delete content\n";
    cout<<" c ... enter string\n";
    cout<<" d ... print on screen first stack\n";
    cout<<" e ...  print on screen first stack\n";
    cout<<" f ... is it palindrom\n";
    cout<<" x ... exit\n";
    cout<<"\n your choice : ";

    cin >>  volba;
    return volba;
}
int main() {
    char  palindrome[MAXSTACK]; 
    char volba;
    stack original,reversed;
    int   stackitems = 0,i;
    //cin.getline(palindrome,MAXSTACK);
    do{
        volba = menu();
        switch (volba)
        {
        case'a':
            {
                original.create();
                reversed.create();
                cout<<"done'";
                break;
            }
        case'b':
            {
            original.emptystack();
            reversed.emptystack();
            cout<<"empty";
            break;
            }
        case'c':
            {
                cout<<"enter your string"<<endl;
            cin.get();
            //cin.get();
            cin.getline(palindrome,MAXSTACK);
    for(int o = 0; o < strlen(palindrome); o++)

        if (isalpha(palindrome[o]))
        {
            original.push(tolower(palindrome[o]));
            stackitems++;                           
        }
            original.stack_print();

        break;
            }
        case'd':
            {
                original.~stack();
                for(int g = 0; g < strlen(palindrome); g++)
                original.push(tolower(palindrome[g]));
                original.stack_print();
            }
            /*//cin.getline(palindrome,MAXSTACK);
    for(int g = 0; g < strlen(palindrome); g++)

        if (isalpha(palindrome[g]))
        {
            original.push(tolower(palindrome[g]));
            stackitems++;                           
        }

            }
            original.stack_print();*/
            break;


        /*{
                cout<<"original: ";
        original.stack_print();
                break;
            }*/
            break;
        case'e':
            {
            cout<<"reversed:"<<endl;
            for( i = 0; i < stackitems; i++) {
            reversed.push(original.top());
            original.pop();
        }
        reversed.stack_print();
            }
            break;

        case'f':
            {
            for( i = 0; i < stackitems / 2; i++) {
            reversed.push(original.top());
            original.pop();
        }


        if (stackitems % 2)
            original.pop();

        while (!original.emptystack()) {
            if (original.top() != reversed.top()) break;
            original.pop(); reversed.pop();
        }
        if (original.emptystack())
            cout << "it is palindrom\n";
        else
            cout << "not palindrom\n";

        break;

            }
        default:cout<<"!??!";


        }
    } while(volba!='x');
}


this destutctor thing was just something i tried, but didn't work

Powered by binpress