求助大神,使用vs2017建立二叉树时遇到了不允许使用不完整类型的问题,但是检查后没有发现问题,请问怎么解决?

很菜的菜鸡 2018-10-26 09:12:06

以上是遇到的问题
以下是头文件中类的定义
#pragma once
#ifndef BST
#define BST
#include <iostream>
using namespace std;

//Binary tree node abstract class
template <typename E> class BinNode
{
public:
virtual ~BinNode(){}//Base destructor

//Return the node's value
virtual E& element() = 0;

//Set the node's value
virtual void setElement(const E&) = 0;

//Return the node's left child
virtual void BinNode*left()const = 0;

//Set the node's left child
virtual void setLeft(BinNode*) = 0;

//Return the node's right child
virtual void BinNode*right()const = 0;

//Set the node's right child
virtual void setRight(BinNode*) = 0;

//Return true if the node is a leaf,false otherwise
virtual bool isLeaf() = 0;
};

//Simple binary tree node implementation
template <typename Key,typename E>
class BSTnode :pubilc BinNode<E>
{
private:
Key k;//The node's key
E it;//The node's value
BSTnode* lc;//Pointer to left child
BSTnode* rc;//Pointer to right child
bool find(E e, BSTNode *root);
void insert(BSTNode<int, char> *node, BSTNode<int, char>** list, int& head, int tail);

public:
//Two constructors --with and without inital values
BSTnode() { lc = rc = NULL; }
BSTnode(Key k,E e,BSTnode*l=NULL,BSTnode*r=NULL)
{
k = K;
it = e;
lc = l;
rc = r;
}
~BSTnode(){}//Destructor

//Functions to set and return the vaulue and key
E& element() { return it; }
void setElement(const E& e) { it = e; }
Key& key() { return k; }
void setKey(const Key& K) { k = K; }

//Functions to set and return the children
inline BSTnode*left()const { return lc; }
void setLeft(BinNode<E>*b) { lc = (BSTnode*)b; }
inline BSTnode* right()const { return rc; }
void setRight(BinNode<E>*b) { rc = (BSTnode)*b; }

//Return true if it is a leaf,false otherwise
bool isLeaf() { return(lc == NULL) && (rc == NULL); }

//print
void print()
{
cout << element() << "\t";
}

//前序遍历
void preorder(BinNode<E>* root);

//中序遍历
void inorder(BinNode<E>* root);

//后序遍历
void postorder(BinNode<E>* root);

//利用前序、中序重建二叉树
BSTNode* reconstruction(E* pre, E* in, int i_1, int j_1, int i_2, int j_2);
int findRoot(E& pre_ele, E* in, int i_2, int j_2);
int tell(E* pre, E* in, int i_1, int j_1, int i_2, int j_2);

//建立二叉搜索树
BSTNode* buildBin(E* list, int len);
void locate(BSTNode* root, E e);

//二叉搜索
bool find(E e);

//huffman
BSTNode<int, char>* buildHuffman(BSTNode<int, char>** list, int len);
void print_huffman_code();
void pre(BSTNode<int, char> *root, char *code, int& pos);
};



template<typename Key, typename E>
inline bool BSTnode<Key, E>::find(E e, BSTNode * root)
{
if (root == 0) {
cout << "miss " << e << " in this tree" << endl;
return false;
}
if (e == root->element()) {
cout << "Find " << e << " in this bintree" << endl;
return true;
}
else if (e<root->element())
return find(e, root->left());
else
return find(e, root->right());
}

template<typename Key, typename E>
inline void BSTnode<Key, E>::insert(BSTNode<int, char>* node, BSTNode<int, char>** list, int & head, int tail)
{
int count = 0;
for (int i = head; i <= tail; i++) {

if (node->key() <= list[i]->key()) {
list[i - 1] = node;
if (i == head)
head--;
break;
}
else {
list[i - 1] = list[i];
list[i] = node;
count++;

}
}
if (count)
head--;
cout << "insert over" << endl;
for (int i = head; i <= tail; i++) {
//cout<<"--------"<<endl;
cout << list[i]->key() << "\t";

}
cout << endl;
}

//前序遍历
template<typename Key, typename E>
inline void BSTnode<Key, E>::preorder(BinNode<E>* root)
{
if (root == NULL)return;//Empty subtree, do nothing
//visit(root);//Perform desired action
root->print();
preorder(root->left());
preorder(root->right());
}

//中序遍历
template<typename Key, typename E>
inline void BSTnode<Key, E>::inorder(BinNode<E>* root)
{
if (root == NULL)return;//Empty subtree, do nothing
ineorder(root->left());
//visit(root);//Perform desired action
root->print();
ineorder(root->right());
}

//后序遍历
template<typename Key, typename E>
inline void BSTnode<Key, E>::postorder(BinNode<E>* root)
{
if (root == NULL)return;//Empty subtree, do nothing
postorder(root->left());
postorder(root->right());
//visit(root);//Perform desired action
root->print();
}

//利用前序、中序重建二叉树
template<typename Key, typename E>
inline BSTNode * BSTnode<Key, E>::reconstruction(E * pre, E * in, int i_1, int j_1, int i_2, int j_2)
{
this->setElement(pre[i_1]);
if (j_1 - i_1 != j_2 - i_2)
{
cout << "reconstruct error!!!" << endl;
return 0;
}
if (i_1 == j_1 && i_2 == j_2)
return 0;
int root = findRoot(pre[i_1], in, i_2, j_2);
cout << "root----- " << root << endl;
if (root<0) return 0;

int rc = tell(pre, in, i_1 + 1, j_1, i_2, root - 1);
cout << "rc----- " << rc << endl;
//if(rc<0) return 0;
if (rc>0)
{
this->setLeft(new BSTNode(0, pre[i_1 + 1]));
this->setRight(new BSTNode(1, pre[rc]));
}
else
{
this->setRight(new BSTNode(1, pre[i_1]));
}
if (this->left())
this->left()->reconstruction(pre, in, i_1 + 1, rc - 1, i_2, root - 1);
this->right()->reconstruction(pre, in, rc, j_1, root + 1, j_2);
return this;
}

template<typename Key, typename E>
inline int BSTnode<Key, E>::findRoot(E & pre_ele, E * in, int i_2, int j_2)
{
for (int i = i_2; i <= j_2; i++) {
if (in[i] == pre_ele)
return i;
}

cout << endl << "can not find root " << pre_ele << endl;
return -1;
}

template<typename Key, typename E>
inline int BSTnode<Key, E>::tell(E * pre, E * in, int i_1, int j_1, int i_2, int j_2)
{
for (int v = j_2; v >= i_2; v--) {
for (int t = i_1; t <= j_1; t++) {
if (pre[t] == in[v] && t - i_1 == j_2 - i_2)
return t + 1;

}
}
cout << "print the miss ls" << endl;
for (int i = i_2; i <= j_2; i++)
cout << in[i] << "\t";
cout << "can not tell" << endl;
return -1;
}

//建立二叉搜索树
template<typename Key, typename E>
inline BSTNode * BSTnode<Key, E>::buildBin(E * list, int len)
{
setElement(list[0]);
for (int i = 1; i<len; i++) {
locate(this, list[i]);
}
return this;
}

template<typename Key, typename E>
inline void BSTnode<Key, E>::locate(BSTNode * root, E e)
{
if (e <= root->element()) {
if (root->left() == 0)
root->setLeft(new BSTNode(0, e));
else
locate(root->left(), e);
}
else {
if (root->right() == 0)
root->setRight(new BSTNode(1, e));
else
locate(root->right(), e);
}
}

//二叉搜索
template<typename Key, typename E>
inline bool BSTnode<Key, E>::find(E e)
{
cout << "Find " << e << " in this bintree" << endl;
return true;
}

//huffman
template<typename Key, typename E>
inline BSTNode<int, char>* BSTnode<Key, E>::buildHuffman(BSTNode<int, char>** list, int len)
{
int head = 0;
int tail = len - 1;
BSTNode<int, char> *temp1, *temp2, *temp3;
temp1 = temp2 = temp3 = 0;
while (head<tail) {///////
temp1 = list[head];
temp2 = list[head + 1];
//delete list[head];
//delete list[head+1];
head += 2;
temp3 = new BSTNode<int, char>(temp1->key() + temp2->key(), 0, temp1, temp2);
//cout<<head<<endl;
insert(temp3, list, head, tail);
//delete temp1;
//delete temp2;
}
cout << "build huffman over" << endl;
//temp3->print_Preorder();
return temp3;
}

template<typename Key, typename E>
inline void BSTnode<Key, E>::print_huffman_code()
{
cout << "begin code======" << endl;
char code[50];
//cout<<"begin code"<<endl;
int pos = 0;

pre(this, code, pos);
//delete code;
}

template<typename Key, typename E>
inline void BSTnode<Key, E>::pre(BSTNode<int, char>* root, char * code, int & pos)
{
if (root->element()) {
cout << root->element() << " : " << code << endl;
code[--pos] = '\0';
//pos--;
return;
}

code[pos] = '0';
code[++pos] = '\0';
pre(root->left(), code, pos);

code[pos] = '1';
code[++pos] = '\0';
pre(root->right(), code, pos);

code[--pos] = '\0';
};
#endif


...全文
294 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-10-27
  • 打赏
  • 举报
回复
仅供参考:
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct BiTNode {//二叉树结点
char data; //数据
struct BiTNode *lchild,*rchild; //左右孩子指针
} BiTNode,*BiTree;
int nn=0;
int CreateBiTree(BiTree *T) {//按先序序列创建二叉树
char data;
scanf("%c",&data);//按先序次序输入二叉树中结点的值(一个字符),‘#’表示空树
if (data == '#') {
*T = NULL;
} else {
*T = (BiTree)malloc(sizeof(BiTNode)); nn++;
(*T)->data = data; //生成根结点
CreateBiTree(&(*T)->lchild);//构造左子树
CreateBiTree(&(*T)->rchild);//构造右子树
}
return 0;
}
void Visit(BiTree T) {//输出
if (T->data != '#') {
printf("%c ",T->data);
}
}
void PreOrder(BiTree T) {//先序遍历
if (T != NULL) {
Visit(T); //访问根节点
PreOrder(T->lchild); //访问左子结点
PreOrder(T->rchild); //访问右子结点
}
}
void InOrder(BiTree T) {//中序遍历
if (T != NULL) {
InOrder(T->lchild); //访问左子结点
Visit(T); //访问根节点
InOrder(T->rchild); //访问右子结点
}
}
void PostOrder(BiTree T) {//后序遍历
if (T != NULL) {
PostOrder(T->lchild); //访问左子结点
PostOrder(T->rchild); //访问右子结点
Visit(T); //访问根节点
}
}
void PreOrder2(BiTree T) {//先序遍历(非递归)
//访问T->data后,将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,再先序遍历T的右子树。
BiTree *stack=(BiTree *)malloc(nn*sizeof(BiTree));
int sp=0;
BiTree p = T;//p是遍历指针
while (p || sp) { //栈不空或者p不空时循环
if (p != NULL) {
stack[sp]=p;sp++; //存入栈中
printf("%c ",p->data); //访问根节点
p = p->lchild; //遍历左子树
} else {
sp--;p=stack[sp]; //退栈
p = p->rchild; //访问右子树
}
}
free(stack);
}
void InOrder2(BiTree T) {//中序遍历(非递归)
//T是要遍历树的根指针,中序遍历要求在遍历完左子树后,访问根,再遍历右子树。
//先将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,访问T->data,再中序遍历T的右子树。
BiTree *stack=(BiTree *)malloc(nn*sizeof(BiTree));
int sp=0;
BiTree p = T;//p是遍历指针
while (p || sp) { //栈不空或者p不空时循环
if (p != NULL) {
stack[sp]=p;sp++; //存入栈中
p = p->lchild; //遍历左子树
} else {
sp--;p=stack[sp]; //退栈
printf("%c ",p->data);
p = p->rchild; //访问右子树
}
}
free(stack);
}

typedef struct BiTNodePost{
BiTree biTree;
char tag;
} BiTNodePost,*BiTreePost;
void PostOrder2(BiTree T) {//后序遍历(非递归)
BiTreePost *stack=(BiTreePost *)malloc(nn*sizeof(BiTreePost));
int sp=0;
BiTree p = T;//p是遍历指针
BiTreePost BT;
while (p != NULL || sp) {//栈不空或者p不空时循环
while (p != NULL) {//遍历左子树
BT = (BiTreePost)malloc(sizeof(BiTNodePost));
BT->biTree = p;
BT->tag = 'L';//访问过左子树
stack[sp]=BT;sp++; //存入栈中
p = p->lchild;
}
while (sp && (stack[sp-1])->tag == 'R') {//左右子树访问完毕访问根节点
sp--;BT=stack[sp]; //退栈
printf("%c ",BT->biTree->data);
free(BT);
}
if (sp) {//遍历右子树
BT=stack[sp-1];
BT->tag = 'R';//访问过右子树
p = BT->biTree;
p = p->rchild;
}
}
free(stack);
}
void LevelOrder(BiTree T) {//层次遍历
BiTree p;
BiTree *queue;
int h=0,t=0,n=0;

if (T == NULL) return;
p=T;
queue=(BiTree *)malloc(nn*sizeof(BiTree));
queue[t]=p;t=(t+1)%10;n++;//根节点入队
while (n) { //队列不空循环
p=queue[h]; //对头元素出队
printf("%c ",p->data); //访问p指向的结点
h=(h+1)%10;n--; //退出队列
if (p->lchild != NULL) {//左子树不空,将左子树入队
queue[t]=p->lchild;t=(t+1)%10;n++;
}
if (p->rchild != NULL) {//右子树不空,将右子树入队
queue[t]=p->rchild;t=(t+1)%10;n++;
}
}
free(queue);
}
int main() {
BiTree T;

setlocale(LC_ALL,"chs");
CreateBiTree(&T);

printf("先序遍历 :");PreOrder (T);printf("\n");
printf("先序遍历(非递归):");PreOrder2 (T);printf("\n");
printf("\n");
printf("中序遍历 :");InOrder (T);printf("\n");
printf("中序遍历(非递归):");InOrder2 (T);printf("\n");
printf("\n");
printf("后序遍历 :");PostOrder (T);printf("\n");
printf("后序遍历(非递归):");PostOrder2(T);printf("\n");
printf("\n");
printf("层次遍历 :");LevelOrder(T);printf("\n");

return 0;
}
//ABC##DE#G##F###
//先序遍历 :A B C D E G F
//先序遍历(非递归):A B C D E G F
//
//中序遍历 :C B E G D F A
//中序遍历(非递归):C B E G D F A
//
//后序遍历 :C G E F D B A
//后序遍历(非递归):C G E F D B A
//
//层次遍历 :A B C D E F G
//

/// A
/// /
/// B
/// / \
/// C D
/// / \
/// E F
/// \
/// G

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧