c ++程序的输出未达到预期

时间:2016-12-30 07:54:50

标签: c++ binary-tree

我为二叉树制作了一个C ++程序。但终端并未要求声明输入元素放置位置的方向。 当我替换" node * temp = new node" to" node * temp = NULL"程序停止工作。

#include <iostream> 
#include <cstring>

using namespace std;
class node {
    int data;
    node * left;
    node * right;
public:
    node * level_order(node * first);
    node * create_bt(node * first);
    void display(node * first);
};
//node *first=NULL;

node * node::create_bt(node * first) {
    node * temp = new node;
    int ele;
    //char dir;
    cout << "\n Enter data ";
    cin >> ele;
    temp->data = ele;
    temp->left = NULL;
    temp->right = NULL;

    if (first == NULL) {
        temp = first;
        return first;
    } else {
        char dir[20];
        cout << "\n Enter the direction ";
        cin >> dir;
        node * cur = first;
        int j = 0;
        while (dir[j] != '\0') {
            if (dir[j] == 'l') {
                cur = cur->left;
            }
            if (dir[j] == 'r') {
                cur = cur->right;
            }
            j++;
        }
        cur = temp;
        return first;
    }    
}

void node::display(node * first) {
    if (first == NULL)
        return;
    cout << "\n " << first->data;
    display(first->left);
    display(first->right);
}

int main() {
    int n;
    node s;
    node * first = NULL;
    cout << "\n No of elements ";
    cin >> n;
    for (int i = 0; i < n; i++) {
        first = s.create_bt(first);
    }
    s.display(first);
    return 0;
}

2 个答案:

答案 0 :(得分:0)

第一= s.create_bt(第一);不会改变状态,从NULL到&#39; l&#39;或者&#39; r&#39;。你必须改变它。

node*node::create_bt(node *first)
{
    node *temp=new node;
    int ele;
    //char dir;
    cout<<"\n Enter data ";
    cin>>ele;
    temp->data=ele;
    temp->left=NULL;
    temp->right=NULL;
    char dir[20];
    cout<<"\n Enter the direction ";
    cin>>dir;
    if(first==NULL)
    {
        temp=first;
        return first;
    }
    else
    {
        node*cur=first;
        int j=0;
        while(dir[j]!='\0')
        {
            if(dir[j]=='l')
            {
                cur=cur->left;
            }
            if(dir[j]=='r')
            {
                cur=cur->right;
            }
            j++;
        }
        cur=temp;
        return first;
    }

}

答案 1 :(得分:0)

我相信你看起来像这样。这是一个基本的二叉树,我必须做一个基本的,以了解它是如何工作的以及它如何选择左右。我在一个类中创建一个类,以便访问我的数据成员(节点类,int数据,* left,* right)并同时保护它们,一体化。正如您所看到的,“newnode”只是创建一个节点而NULL是指针。而已。 “查找”搜索并查找具有当前键的节点,并在退出时返回该节点。所有其余的,我想,你可以理解它们,因为它们与你的代码大致相同。您唯一需要做的就是定义,当您想要指向您想要的节点时。提醒:你必须找到一种方法来利用它,所以叶子不会左右或极右。(“输入方向”)。我希望我能帮助你理解。

#include <iostream>
#include <conio.h>

using namespace std;
class mybTree {
class node {
public:
    int data;
    node * left;
    node *right;
};
node *root;
node *newnode(int num){
    node *newnode1;
    newnode1 = new (nothrow) node;
    newnode1->data = num;
    newnode1->left = NULL;
    newnode1->right = NULL;
    return newnode1;
}

public:
node *find (int key) {
    node *current;
    current = root;
    while  (current->data !=key){
        if (key<current->data){
            current = current->left;
        } else {
            current = current->right;

        }
        if (current == NULL){
            return NULL;
        }
    }
    return NULL;
}

void display (node *ptr);
void display_tree();
bool insert(int num);

void post_order_delete(node *ptr);
mybTree();
~mybTree();
};
int main(){
char ch = ' ';
int a;
mybTree mybTree1;
while (ch !='0'){
    cout << "0->Exit"<<endl<< "1-> add"<<endl<< "2-> find" <<endl<<"3-> Show me the tree\n";
    ch = getch();
    switch (ch) {
        case '0':
            break;
        case '1':
            cout << "number";
            cin >> a;
            if (!mybTree1.insert(a)){
                cout << "Not enough memory" << endl;
            }
            break;
        case '2' :
            cout << "Number:" ;
            cin >> a;
            if (mybTree1.find(a)!=NULL) {
                cout << "Found" << endl;
            } else {
                cout << "Not existed" << endl;
            }
            break;
        case '3':
            mybTree1.display_tree();
            cout<<endl;
            break;
        default:
            cout << "Wrong Message";
            break;
    }
}
return 0;
}

void mybTree::display(node *ptr) {
if (ptr == NULL){
    return;
}
display(ptr->left);
cout << ptr->data<<endl;
display(ptr->right);
}

void mybTree::display_tree() {
//Displays the Tree
display(root);
}

bool mybTree::insert(int num) {
//It inserts a node. Desides left or right.
node *next,*current,*ptr;
int isleft;
next = current = root;
ptr = newnode(num);
if (ptr == NULL) {
    return false;
}
if (root == NULL) {
    root = ptr;
    return  true;
}
while (1){
    if (num < current->data){
        next = current->left;
        isleft = 1;
    } else {
        next = current->right;
        isleft = 0;
    }
    if (next == NULL){
        if (isleft){
            current->left = ptr;
        } else {
            current->right = ptr;
        }
        return true;
    }
    current=next;
}
return false;
}

void mybTree::post_order_delete(node *ptr) {
//deletes the node. Usefull for destructor
if (ptr == NULL){
    return;
}
post_order_delete(ptr->left);
post_order_delete(ptr->right);
cout << ptr->data;
delete ptr;
}

mybTree::mybTree() {
//Constructor
root = NULL;
}

mybTree::~mybTree() {
//Destructor
post_order_delete(root);
root = NULL;
}