[ create a new paste ] login | about

Link: http://codepad.org/h0sQp9dX    [ raw code | output | fork ]

C++, pasted on Sep 30:
#include <stdio.h>
#include <iostream>
using namespace std;

struct tree 
{
    tree *left;
    tree *right;
    tree *predok;
    int key;
    char value;
} *phead=0;

void add_tree(tree* head, int key, char value)
{
    if (key < (head -> key))
    {
        //cout << "1" << endl;
        if (head -> left != NULL)
        {
            add_tree(head->left, key, value);
            
        }           
        else
            {
                //cout << "13" << endl;
                //->left;
                head->left=new tree;
                head->left->predok=head;
                head->left->key=key;
                head->left->value=value;
                head->left->left=NULL;
                head->left->right=NULL;
                
            }
    }
    if (key > (head -> key))
    {
        //cout << "1" << endl;
        if (head -> right != NULL)
        {
            add_tree(head->right, key, value);
        }           
        else
        {
                //cout << "13" << endl;
                head->right=new tree;
                head->right->predok=head;
                head->right->key=key;
                head->right->value=value;
                head->right->left=NULL;
                head->right->right=NULL;
                
        }
    }
    //if(value == head->value) {cout<< value<< " is already in tree"<< endl;}
}


int create_tree(int key,char value)
{
    if (phead == 0)
    {
        cout << "!!" << endl;
        phead=new tree;
        phead->key=key;
        phead->value=value;
        phead->left=NULL;
        phead->right=NULL;
        phead->predok=NULL;
    }
    else
    {
        add_tree(phead, key, value);
    }
    return 0;
}

void straight(tree *root)
{
    if(!root)
    return;
    
    cout << root -> key << endl;
    straight(root->left);
    straight(root->right);
}

tree *search(tree *temp,int key)
{
        if (key == (temp->key))
        {
            cout << temp->key << ":" << temp->value << endl;
            return temp;
        }
        if (key < (temp -> key))
        {
            if ((temp-> left) == NULL)
            {
                cout << "Key not found" << endl;
            }
            if ((temp -> left) != NULL)
            {
                search(temp->left, key);
            }
            
        }
        if (key > (temp -> key))
        {
            if ((temp-> right) == NULL)
            {
                cout << "Ket not found" << endl;
            } 
            if ((temp -> right) != NULL)
            {
                search(temp->right, key);
            }
        }
}

tree *max(tree *head)
{
    if (head -> right != NULL)
    {
        max(head->right);
    }
    else {
        cout << head-> key << ":" << head->value << endl;
        return head;
        }
}   

   
    
int main()
{
    int key;
    char value;
    while (key)
    {
        cout << "Vvedite kluch, znachenie:" << endl;
        cin >> key >> value;
        create_tree(key,value);
    }
    straight(phead);
    cout << "Vvedite kluch" << endl;
    cin >> key;
    cout << search(phead, key) -> predok -> key << endl;
    getchar();
    getchar();
    return 0;
}


Output:
1
2
3
4
5
cc1plus: warnings being treated as errors
In function 'tree* max(tree*)':
Line 131: warning: control reaches end of non-void function
In function 'tree* search(tree*, int)':
Line 119: warning: control reaches end of non-void function


Create a new paste based on this one


Comments: