[ create a new paste ] login | about

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

C++, pasted on Sep 29:
#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;
                
                head->left=new tree;
                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->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;
    }
    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;
        }
}   

/*void delete_u(tree *head, int key)
{
    tree *temp1=search(head, key);
    tree *temp2=temp1-> left;
    temp2=max(temp2);
    temp1 -> key=temp2 -> key;
    temp1 -> value=temp2 -> value;
    if (temp2->left != NULL)
    {
        temp2->predok->right=temp2->left;
        temp2->left->predok=temp2->predok;
    }
}*/
        
    
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) -> value << endl;
    cout << max(phead)->predok->key << endl;
    /*cout << "Vvedite kluch dlya udal" << endl;
    cin >> key;
    delete_u(phead, key);
    straight(phead);*/
    getchar();
    getchar();
    return 0;
}


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


Create a new paste based on this one


Comments: