「已注销」 2016-11-08 12:47 采纳率: 0%
浏览 2468

“binaryTreeNode”: 使用 类 模板 需要 模板 参数列表 该怎么解决

#ifndef linkedBinaryTree_
#define linkedBinaryTree_

using namespace std;

#include
#include "binaryTree.h"
#include "arrayQueue.h"
#include "binaryTreeNode.h"
#include "myExceptions.h"
#include "booster.h"

template
class linkedBinaryTree : public binaryTree >
{
public:
linkedBinaryTree() {root = NULL; treeSize = 0;}
~linkedBinaryTree(){erase();};
bool empty() const {return treeSize == 0;}
int size() const {return treeSize;}
E* rootElement() const;
void makeTree(const E& element,
linkedBinaryTree&, linkedBinaryTree&);
linkedBinaryTree& removeLeftSubtree();
linkedBinaryTree& removeRightSubtree();
void preOrder(void(*theVisit)(binaryTreeNode))
{visit = theVisit; preOrder(root);}
void inOrder(void(*theVisit)(binaryTreeNode
))
{visit = theVisit; inOrder(root);}
void postOrder(void(*theVisit)(binaryTreeNode))
{visit = theVisit; postOrder(root);}
void levelOrder(void(
)(binaryTreeNode *));
void preOrderOutput() {preOrder(output); cout << endl;}
void inOrderOutput() {inOrder(output); cout << endl;}
void postOrderOutput() {postOrder(output); cout << endl;}
void levelOrderOutput() {levelOrder(output); cout << endl;}
void erase()
{
postOrder(dispose);
root = NULL;
treeSize = 0;
}
int height() const {return height(root);}

protected:
binaryTreeNoderoot;
static void stack(binaryTreeNode);
int treeSize; // number of nodes in tree
static void (*visit)(binaryTreeNode
); // visit function
static int count; // used to count nodes in a subtree
static void preOrder(binaryTreeNodet);
static void inOrder(binaryTreeNode *t);
static void postOrder(binaryTreeNode *t);
static void countNodes(binaryTreeNode *t)
{
visit = addToCount;
count = 0;
preOrder(t);
}
static void dispose(binaryTreeNode *t) {delete t;}
static void output(binaryTreeNode *t)
{cout << t->element << ' ';}
static void addToCount(binaryTreeNode *t)
{count++;}
static int height(binaryTreeNode *t);
};
// the following should work but gives an internal compiler error
// template void (*linkedBinaryTree::visit)(binaryTreeNode
);
// so the explicit declarations that follow are used for our purpose instead
void (*linkedBinaryTree::visit)(binaryTreeNode);
void (*linkedBinaryTree::visit)(binaryTreeNode
);
void (*linkedBinaryTree >::visit)(binaryTreeNode >);
void (*linkedBinaryTree >::visit)(binaryTreeNode >
);
void (*linkedBinaryTree >::visit)(binaryTreeNode >*);

template
E* linkedBinaryTree::rootElement() const
{// Return NULL if no root. Otherwise, return pointer to root element.
if (treeSize == 0)
return NULL; // no root
else
return &root->element;
}

template
void linkedBinaryTree::makeTree(const E& element,
linkedBinaryTree& left, linkedBinaryTree& right)
{
root = new binaryTreeNode (element, left.root, right.root);
treeSize = left.treeSize + right.treeSize + 1;

left.root = right.root = NULL;
left.treeSize = right.treeSize = 0;
}

template
linkedBinaryTree& linkedBinaryTree::removeLeftSubtree()
{
if (treeSize == 0)
throw emptyTree();

linkedBinaryTree leftSubtree;
leftSubtree.root = root->leftChild;
count = 0;
leftSubtree.treeSize = countNodes(leftSubtree.root);
root->leftChild = NULL;
treeSize -= leftSubtree.treeSize;

return leftSubTree;
}

template
linkedBinaryTree& linkedBinaryTree::removeRightSubtree()
{
if (treeSize == 0)
throw emptyTree();

linkedBinaryTree rightSubtree;
rightSubtree.root = root->rightChild;
count = 0;
rightSubtree.treeSize = countNodes(rightSubtree.root);
root->rightChild = NULL;
treeSize -= rightSubtree.treeSize;

return rightSubTree;
}

template
void linkedBinaryTree::preOrder(binaryTreeNoderoot)
{
if (root == NULL)
return;
binaryTreeNode *p = root;
stack<binaryTreeNode
> s;
while (!s.empty() || p)
{

    while (p)
    {
        cout << setw(4) << p->element;
        s.push(p);
        p = p->leftChild;
    }

    if (!s.empty())
    {
        p = s.top();
        s.pop();
        p = p->rightChild;
    }
}
cout << endl;

}

template
void linkedBinaryTree::inOrder(binaryTreeNode *t)
{
if (t != NULL)
{
inOrder(t->leftChild);
linkedBinaryTree::visit(t);
inOrder(t->rightChild);
}
}

template
void linkedBinaryTree::postOrder(binaryTreeNode *t)
{
if (t != NULL)
{
postOrder(t->leftChild);
postOrder(t->rightChild);
linkedBinaryTree::visit(t);
}
}

template
void linkedBinaryTree::levelOrder(void(*theVisit)(binaryTreeNode))
{
arrayQueue
> q;
binaryTreeNode *t = root;
while (t != NULL)
{
theVisit(t);

  if (t->leftChild != NULL)
     q.push(t->leftChild);
  if (t->rightChild != NULL)
     q.push(t->rightChild);


  try {t = q.front();}
  catch (queueEmpty) {return;}
  q.pop();

}
}

template
int linkedBinaryTree::height(binaryTreeNode *t)
{
if (t == NULL)
return 0;

int hl = height(t->leftChild);

int hr = height(t->rightChild);
if (hl > hr)
return ++hl;
else
return ++hr;
}

#endif

  • 写回答

1条回答

  • threenewbee 2016-11-08 16:11
    关注
     你的调用在哪里。main函数呢?检查下,使用binaryTreeNode的时候要模板参数,比如 binaryTreeNode<int>,这个int就是模板参数
    
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)