[英]C++ Binary Tree With 3 Values For Struct
因此,我試圖創建一個二叉搜索樹,該樹存儲一個ID (T value)
,年齡(int age)
和名稱(string)
,每個ID位於該樹中,每個“氣泡”並按ID排序。
由於某種原因,我無法使我的代碼正確地將所有3個值存儲為每個節點1個結構。 我做錯什么了嗎?
這是我的代碼。
#ifndef _TREE_GUARD
#define _TREE_GUARD 1
#include <iostream>
#include <math.h>
using namespace std;
template <class T>
struct Node {
T value;
int age;
string name;
Node *left;
Node *right;
Node(T val) {
this->value = val;
}
Node(T val, int age, string name, Node<T> left, Node<T> right) {
this->value = val;
this->age = age;
this->name = name;
this->left = left;
this->right = right;
}
};
template <class T>
class BST {
private:
Node<T> *root;
void addHelper(Node<T> *root, T val) {
if (root->value > val) {
if (!root->left) {
root->left = new Node<T>(val);
}
else {
addHelper(root->left, val);
}
}
else {
if (!root->right) {
root->right = new Node<T>(val);
}
else {
addHelper(root->right, val);
}
}
}
void printHelper(Node<T> *root) {
if (!root) return;
printHelper(root->left);
cout << root->value << ' ';
cout << root->age << ' '; // ADDED
cout << root->name << ' '; //ADDED
printHelper(root->right);
}
int nodesCountHelper(Node<T> *root) {
if (!root) return 0;
else return 1 + nodesCountHelper(root->left) + nodesCountHelper(root->right);
}
int heightHelper(Node<T> *root) {
if (!root) return 0;
else return 1 + max(heightHelper(root->left), heightHelper(root->right));
}
void printMaxPathHelper(Node<T> *root) {
if (!root) return;
cout << root->value << ' ';
if (heightHelper(root->left) > heightHelper(root->right)) {
printMaxPathHelper(root->left);
}
else {
printMaxPathHelper(root->right);
}
}
bool deleteValueHelper(Node<T>* parent, Node<T>* current, T value) {
if (!current) return false;
if (current->value == value) {
if (current->left == NULL || current->right == NULL) {
Node<T>* temp = current->left;
if (current->right) temp = current->right;
if (parent) {
if (parent->left == current) {
parent->left = temp;
}
else {
parent->right = temp;
}
}
else {
this->root = temp;
}
}
else {
Node<T>* validSubs = current->right;
while (validSubs->left) {
validSubs = validSubs->left;
}
T temp = current->value;
current->value = validSubs->value;
validSubs->value = temp;
return deleteValueHelper(current, current->right, temp);
}
delete current;
return true;
}
return deleteValueHelper(current, current->left, value) ||
deleteValueHelper(current, current->right, value);
}
public:
void insert(T val) {
if (root) {
this->addHelper(root, val);
}
else {
root = new Node<T>(val);
}
}
void print() {
printHelper(this->root);
}
int nodesCount() {
return nodesCountHelper(root);
}
int height() {
return heightHelper(this->root);
}
void printMaxPath() {
printMaxPathHelper(this->root);
}
bool Delete(T value) {
return this->deleteValueHelper(NULL, this->root, value);
}
};
#endif
我使用這3個值以及指向左右節點的指針創建了結構,但是,我的所有其他功能似乎都不適用於age和name值,而只能使用ID。 這是我add
和print
功能中最常見的問題。
我是C ++的新手,所以可以提供任何幫助。
編輯:我假設我的問題就在這里某個地方。 該代碼可以正常運行,但不會在字符串中添加年齡和名稱,而且我無法正確打印這些值,僅顯示ID
Node<T> *root;
void addHelper(Node<T> *root, T val) {
if (root->value > val) {
if (!root->left) {
root->left = new Node<T>(val);
}
else {
addHelper(root->left, val);**
}
}
else {
if (!root->right) {
root->right = new Node<T>(val);
}
else {
addHelper(root->right, val);
}
}
}
和這里
void printHelper(Node<T> *root) {
if (!root) return;
printHelper(root->left);
cout << root->value << ' ';
cout << root->age << ' '; // ADDED
cout << root->name << ' '; //ADDED
printHelper(root->right);
}
提前致謝!
root-> left = new Node(val);
root-> right = new Node(val);
看來您創建了一個新節點,只給它一個“ val”,沒有年齡,沒有名字等,因此它僅具有值。
編輯:我以前從未使用過“模板”,所以花了我一段時間才弄清楚。 無論如何...
解決方案:插入節點時,您需要復制所有信息,而不僅僅是“值”,否則,您將只獲得“值”。
void addHelper(Node<T> *root, Node<T>* n) {
if (root->value > n->value) {
if (!root->left) {
root->left = new Node<T>(n->value,n->age,n->name,NULL,NULL);
}
else {
addHelper(root->left, n);
}
}
else {
if (!root->right) {
root->right = new Node<T>(n->value,n->age,n->name,NULL,NULL);
}
else {
addHelper(root->right, n);
}
}
}
也在“插入”中:
void insert(Node<T>* n) {
if (root) {
this->addHelper(root, n);
}
else {
root = new Node<T>(n->value,n->age,n->name,NULL,NULL);
}
}
您可以嘗試其他幾件事,看看有什么不同:
我會將* root初始化為NULL,否則可能是一些隨機的東西,有時我會因此而出現段錯誤。
class BST {
private:
Node<T> *root=NULL;
如果要插入的節點與已經存在的節點具有相同的“值”,該怎么辦? 您的程序只會將其添加到右側。 我不知道那是不是您想要的,但這就是它的作用。
我對此進行了測試,並成功了。 希望它也對您有用。
int main(){
BST<int> tree;
Node<int> node1(1,10,"test1",NULL,NULL);
Node<int> node2(5,50,"test2",NULL,NULL);
Node<int> node3(3,30,"test3",NULL,NULL);
Node<int> node4(3,30,"test4",NULL,NULL);
Node<int> node5(3,30,"test5",NULL,NULL);
Node<int> node6(8,80,"test5",NULL,NULL);
tree.insert(&node1);
tree.insert(&node2);
tree.insert(&node3);
tree.insert(&node4);
tree.insert(&node5);
tree.insert(&node6);
tree.print();
cout<<endl;
return 0;
}
輸出:
1 10 test1 3 30 test3 3 30 test4 3 30 test5 5 50 test2 8 80 test5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.