尝试树的实现



试着做树,有一些麻烦,首先它的打印功能-它的打印不是整数,我放,但打印随机数;

另一个问题是它的附加子元素-它只能工作一次;

如果你能帮我做这项任务,我会很高兴的。

也给一些关于链表,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("%dn ", 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("%dn ", current_node->m_pPayload)

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

printf("%dn ", ((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;
}

请在下面找到一段代码,它应该很容易让你开始。它编译并使用递归遍历树。

#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("%dn ", 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);

}

第一个打印节点值

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

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

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

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

  • 左序或序序遍历
  • 前序遍历
  • 后缀次序遍历

这样可以提供更好的遍历信息。

第二步:将节点添加到树
这可能有助于更好地解释。

相关内容

  • 没有找到相关文章

最新更新