带有矢量重复值的二叉树打印出来

binary tree printing out with vector duplicate value

本文关键字:二叉树 打印      更新时间:2023-10-16

我正在尝试持有向量以收集二叉树中的所有重复值。

如果二叉树有 2、2、5,6,7,7,8,那么它将用向量打印出 2 和 7。

我意识到我的递归 1.它最多只会带来2个值,因为它每次调用函数时都会重置向量 2. 在递归函数结束时 - 它不会带来任何价值

我的递归函数中缺少什么?

#include "pch.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
struct node {
int data;
node *left, *right;
};
node* getnew(int val) {
node *curr = new node;
curr->data = val;
curr->left = curr->right = nullptr;
return curr;
}

node* insert(node* root, int val) {
if (root == nullptr)
return getnew(val);
if (val > root->data) 
root->right = insert(root->right, val);
else if (val <= root->data)
root->left = insert(root->left, val);
return root;
}
void print(node*root) {
if (root != nullptr) {
print(root->left);
cout << root->data << " ";
print(root->right);
}
}

int assist(node* root, int value) {
if(root!=nullptr){
if (root->data == value)return value;
return assist(root->left, value) || assist(root->right, value);
}
else return -5;
}
vector<int> findmode(node* root) {
vector<int> thedup;
//create recusion that find dup value
//we need assistant function to find dup value
if (root != nullptr) {
if(assist(root->left, root->data)!=-5){
thedup.push_back(root->data);
return thedup;
}
else if(assist(root->right, root->data)!=-5){
thedup.push_back(root->data);
return thedup;
}
return thedup;
}
else return thedup;
}
int main() {
node *root = nullptr;

root = insert(root, 5);
insert(root, 3);
insert(root, 2);
insert(root, 4);
insert(root, 2);
insert(root, 2);
insert(root, 8);

print(root);
cout << endl;
vector<int> test;
for (int i = 0; i < test.size(); i++)
cout << test[i] << " " << endl;
}

下面是一个修订后的示例,它将遍历树,然后使用树本身作为索引来查找重复项。如注释中所述,您需要通过引用传递收集器以避免覆盖它。我已经更新了您的解决方案,以使用set而不是矢量,以避免添加重复项(矢量会发生这种情况(。 更新的代码如下(带有一些注释掉的调试语句(,您也可以在 https://repl.it/@cptndave/btreedups 尝试一下。

#include <iostream>
#include <vector>
#include <set>
#include <fstream>
#include <string>
using namespace std;
struct node {
int data;
node *left, *right;
};
node* getnew(int val) {
node *curr = new node;
curr->data = val;
curr->left = curr->right = nullptr;
// printf("New %p %dn", curr, val);
return curr;
}

node* insert(node* root, int val) {
if (root == nullptr)
return getnew(val);
if (val > root->data) 
root->right = insert(root->right, val);
else if (val <= root->data)
root->left = insert(root->left, val);
return root;
}
void print(node*root) {
if (root != nullptr) {
print(root->left);
cout << root->data << " ";
print(root->right);
}
}
// Return true if data exists outside of ignore node
bool exists(node *root, node *target)
{
if (root == nullptr)
return false;
// printf("  Looking for %d at %pn", target->data, root);
if (root != target && root->data == target->data)
return true;
if (target->data > root->data)
return exists(root->right, target);
else
return exists(root->left, target);
}
// Traverse tree looking for duplicates
void finddup_helper(node *root, node *current, set<int> *dups)
{
if (current == nullptr)
return;
// printf("Helper: root %p, current: %p, %dn",  root, current, current->data);
if (exists(root, current)) {
// printf("dup %dn", current->data);
dups->insert(current->data);
}
finddup_helper(root, current->left, dups);
finddup_helper(root, current->right, dups);
}
// Base function which kicks off helper from the root
set<int> finddups(node *root)
{
set<int> dups;
finddup_helper(root, root, &dups);
return dups;
}
int main() {
node *root = nullptr;
root = insert(root, 5);
insert(root, 3);
insert(root, 2);
insert(root, 4);
insert(root, 2);
insert(root, 2);
insert(root, 8);
insert(root, 5);
cout << "Tree: ";
print(root);
cout << endl;
set<int> dups = finddups(root);;
cout << "Duplicates: ";
for (set<int>::iterator it = dups.begin(); it != dups.end(); it++)
cout << *it << " ";
cout << endl;
}