我的C ++代码有问题。它说所有以isPerfectRec()开头的函数都无法解决......为什么?我尝试了很多东西,但显然它们不起作用。我有很多分配,比如验证二进制搜索树是否完美,找到二叉搜索树中的第二大元素等等。
#include <stdio.h>
#include<iostream>
#include<stack>
template<typename T> class BinarySearchTree {
public:
BinarySearchTree<T> *root, *left_son, *right_son, *parent;
T *pinfo;
BinarySearchTree() {
left_son = right_son = NULL;
root = this;
pinfo = NULL;
}
void setInfo(T info) {
pinfo = new T;
*pinfo = info;
}
void insert(T x) {
if (pinfo == NULL)
setInfo(x);
else
insert_rec(x);
}
bool isPerfectRec(BinarySearchTree *root, int d, int level = 0)
{
// An empty tree is perfect
if (*root == NULL)
return true;
// If leaf node, then its depth must be same as
// depth of all other leaves.
if (*root->left_son == NULL && root->*right_son == NULL)
return (d == level+1);
// If internal node and one child is empty
if (root->*left_son == NULL || root->*right_son == NULL)
return false;
// Left and right subtrees must be perfect.
return isPerfectRec(root->*left_son, d, level+1) &&
isPerfectRec(root->*right_son, d, level+1);
}
// Wrapper over isPerfectRec()
bool isPerfect(BinarySearchTree *root)
{
int d = findADepth(root);
return isPerfectRec(root, d);
}
int findADepth(BinarySearchTree *node)
{
int d = 0;
while (node != NULL)
{
d++;
node = node->left_son;
}
return d;
}
// A function to find 2nd largest element in a given tree.
void secondLargestUtil(BinarySearchTree *root, int &c)
{
// Base cases, the second condition is important to
// avoid unnecessary recursive calls
if (root == NULL || c >= 2)
return;
// Follow reverse inorder traversal so that the
// largest element is visited first
secondLargestUtil(root->right_son, c);
// Increment count of visited nodes
c++;
// If c becomes k now, then this is the 2nd largest
if (c == 2)
{
std::cout << "2nd largest element is "
<< root->pinfo;
printf("\n___\n");
return;
}
// Recur for left subtree
secondLargestUtil(root->left_son, c);
}
void secondLargest(BinarySearchTree *root)
{
// Initialize count of nodes visited as 0
int c = 0;
// Note that c is passed by reference
secondLargestUtil(root, c);
}
bool hasOnlyOneChild(int pre[], int size)
{
int nextDiff, lastDiff;
for (int i=0; i<size-1; i++)
{
nextDiff = pre[i] - pre[i+1];
lastDiff = pre[i] - pre[size-1];
if (nextDiff*lastDiff < 0)
return false;;
}
return true;
}
BinarySearchTree * readListInter(){
BinarySearchTree* root = NULL;//returning object
BinarySearchTree* temp;
BinarySearchTree* input;//new node to add
int x;
std::cout << "enter number (>0 to stop): ";
std::cin >> x;
while(x>=0){
input = BinarySearchTree(x);
if(root == NULL){//if root is empty
root = input;
temp = root;//temp is use to store value for compare
}
else{
temp = root; //for each new addition, must start at root to find correct spot
while(input != NULL){
if( x < temp->pinfo){//if smaller x to add to left
if(temp->left_son == NULL){//left is empty
temp->left_son = input;
input = NULL;//new node added, exit the loop
}
else{//if not empty set temp to subtree
temp = temp->left_son;//need to move left from the current position
}
}
else{//otherwise x add to right
if(temp->right_son == NULL){//right is empty
temp->right_son = input;
input = NULL;//new node added, exit the loop
}
else{
temp = temp->right_son;//need to move right from the current position
}
}
}
}
std::cin >> x;
}
return root;
}
};
int main() {
BinarySearchTree<int> *r = new BinarySearchTree<int>;
BinarySearchTree<int> *r1 = new BinarySearchTree<int>;
BinarySearchTree<int> *p = new BinarySearchTree<int>;
p = readListInter();
r->insert(6);
r->insert(8);
r->insert(1);
r->insert(9);
r->insert(10);
r->insert(4);
r->insert(13);
r->insert(12);
printf("\n___\n");
r1->insert(6);
r1->insert(8);
r1->insert(1);
r1->insert(9);
r1->insert(10);
r1->insert(4);
r1->insert(13);
r1->insert(12);
printf("\n___\n");
r->isPerfect(r);
int pre[] = {8, 3, 5, 7, 6};
int size = sizeof(pre)/sizeof(pre[0]);
if (hasOnlyOneChild(pre, size) == true )
printf("Yes");
else
printf("No");
s
return 0;
}
答案 0 :(得分:1)
我认为您需要在这些函数中编写Python script used is:
import pandas as pd
import numpy as np
import sys
from difflib import SequenceMatcher
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
arg1 = sys.argv[1]
arg2 = sys.argv[2]
arg3 = sys.argv[3]
print (arg1)
print (arg2)
print (arg3)
def get_similar_CRs(arg1, arg2,arg3):
##create dummy data
cr_id=range(1,41)
description=['change in design','More robust system required',
'Grant system adminstrator rights',
'grant access to all products',
'Increase the credit limit',
'EDAP Scenario',
'Volume prpductivity for NA 2015',
'5% productivity saves SOW',
'effort reduction',
'reduction of false claims',
'Volume productivity EMEA',
'Volume productivity for NA 2016',
'10% productivity saves SOW',
]
region=['EMEA','Asia Pacific','UK']
business=['card','secured loan','mortgage']
type=['regulatory','system','audit']
status=['pending','approved']
data=pd.DataFrame()
data['description']=np.random.choice(description, 40)
data['cr_id']=cr_id
data['region']=np.random.choice(region,40)
data['business']=np.random.choice(business, 40)
data['status']=np.random.choice(status,40)
data['type']=np.random.choice(type,40)
subset_data=data.loc[data.region == arg1]
print (subset_data.head())
subset_data=subset_data.loc[subset_data.type ==arg2]
##This has to be captured dynamically
new_cr=arg3
cr_list=data['description'].unique().tolist()
similar_CR=[] ###global variable
# for new_cr in new_cr_lis
for cr in cr_list:
result=similar(new_cr,cr)
if result >=0.8:
similar_CR.append(cr)
temp=subset_data.loc[subset_data.description.isin(similar_CR)]
temp=temp[['description','status','region']]
return temp
temp= get_similar_CRs (arg1, arg2, arg3)
print temp
而不是BinarySearchTree<T>
作为数据类型。