繁体   English   中英

尝试树实现

[英]Try tree inplementation

尝试制作树,有一些麻烦,首先是打印 function - 它不是打印我输入的整数,而是打印随机数;

另一个麻烦它的 append 子——它的工作只有一次;

如果你能帮助我完成这项任务,我会很高兴。

并且还给出了一些关于链表的好文章,c 和 c++ 上的树;


#include <iostream>

#include <stdio.h>

using namespace std;


struct Node
{
    void* m_pPayload;
    Node* m_pParent;
    Node* m_Children;
    
};

struct Person
{
    int m_Id;
};

//typedef bool (*NodeComparator)(void* pValue, void* pPayload);

/*bool Comp(void* pValue, void* pPayload)
{
    Person* pVal = (Person*)pValue;
    Person* pPay = (Person*)pPayload;
    if (pVal->m_Id == pPay->m_Id)
        return true;
    else
        return false;
}
*/

Node* NewNode(void* pPayload)
{
    Node* pNode = new Node;
    pNode->m_pParent = nullptr;
    pNode->m_Children = 0;
    pNode->m_pPayload = pPayload;
    return pNode;
}

Person* NewPerson(int id)
{
    Person* p = new Person;
    p->m_Id = id;
    return p;
}

//Node* FindNode(Node* pParent, Node* m_pPayload, NodeComparator comparator);

void AppendChild(Node* pParent, Node* pNode)
{
    if (pParent->m_Children == NULL)
        pParent->m_Children = pNode;
    
}

void print(Node* head) 
{
    Node* current_node = head;
    while (current_node != NULL) 
    {
        printf("%d\n ", current_node->m_pPayload);
        current_node = current_node->m_Children;
        
    }
}


int main()
{

    Node* T = new Node;
    
    T = NewNode(NewPerson(5));
    
    AppendChild(T, NewNode(NewPerson(11)));
    AppendChild(T, NewNode(NewPerson(15)));
    
    print(T);
    
}


printf("%d\n ", current_node->m_pPayload)

是不正确的。 %d想要一个 integer 并且它被赋予了一个指针。 结果将是不寻常的,并且可能看起来是随机垃圾。

printf("%d\n ", ((Person*)current_node->m_pPayload)->m_Id);
                  ^                                ^
                  |                                Get id from Person
                  treat payload pointer as pointer to Person
                

将解决眼前的问题。

您的代码实际上似乎被很多事情搞砸了,在这里分享我几年前自己评论过的代码,希望对您有所帮助

#include <bits/stdc++.h>
using namespace std;

// Single node representation
struct node {
    int data;
    node *left, *right;
};

// Declaring temp for refference and root to hold  root node
node *root, *temp;

// This function only generates a node and return it to the calling function with data stored in it
node* generateNode(int data){
    temp = new node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}

// This function actually adds node to the tree
node* addNode(int data, node *ptr = root){
    // If the node passed as ptr is NULL
    if(ptr == NULL){
        ptr = generateNode(data);
        return ptr;
    }
    // Condition to check in which side the data will fit in the tree
    else if(ptr->data < data)
        //if its in right, calling this function recursively, with the right part of the tree as the root tree
        ptr->right = addNode(data, ptr->right);
    else
    //In case the data fits in left
        ptr->left = addNode(data, ptr->left);

    //Note: if there is no data in left or roght depending on the data's valid position, this function will get called with NULL as second argument and then the first condition will get triggered

    //returning the tree after appending the child
    return ptr;
}

//Driver function
int main ()
{
    int c, data;
    for (;;){
        cin >> c;
        switch(c){
            case 1:
                cout << "enter data: ";
                cin >> data;
        //Updating root as the tree returned by the addNode function after adding a node
                root = addNode(data);
                break;
            default:
                exit(0);
                break;
        }
    }
        return 0;
}

首先用于打印节点值

在上面的代码中谈论您所犯的当前错误是:

  1. 你还没有提到它指向它的孩子的指针(特别是右或左)。 由于它每次都显示垃圾值。
    例如: print( node->left);

  2. 由于您需要正确键入 caste 以显示数据的数据。
    例如: printf("%d\n ", ((Person*)current_node->m_pPayload)->m_Id);

有一个您想要打印数据的特定方向。 对于树,可以从三个方向打印节点的数据,它们如下:

  • 左序或中序遍历
  • 前序遍历
  • 后序遍历

可以为您提供有关遍历的更好信息。

其次用于将节点添加到树中
可能有助于更好地解释它。

请在下面找到一段可以轻松入门的代码。 它编译并使用递归遍历树。

#include <iostream>
#include <vector>
#include <stdio.h>

using namespace std;


struct Node
{
    int m_Id;
    vector<Node*> m_Children;
    
    Node(const int& id){
       m_Id = id;   
    }
    
    void AppendChild(Node* pNode) {
        m_Children.push_back(pNode);
    }
    
    void Print() {
        printf("%d\n ", m_Id);
    }
    
};

void traverse(Node* head) 
{
    Node* current_node = head;
    current_node->Print();
    for(int i = 0; i<current_node->m_Children.size(); i++) {
        traverse(current_node->m_Children[i]);
    }
}


int main()
{
    Node* T0 = new Node(0);
    
    Node* T10 = new Node(10);
    T10->AppendChild(new Node(20));
    
    Node* T11 = new Node(11);
    
    Node* T12 = new Node(12);
    Node* T22 = new Node(22);
    
    T22->AppendChild(new Node(33));
    T12->AppendChild(T22);
    
    T0->AppendChild(T10);
    T0->AppendChild(T11);
    T0->AppendChild(T12);
    
    traverse(T0);
    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM