我需要编写一个 void 函数来计算每个子树中的节点数。我阅读了很多示例代码,但它们都返回一个整数。而且我不知道如何使用非递归 void 函数来执行与那些 int 函数相同的功能。
这是我到目前为止所拥有的:
void computeWeight(treeNode<treeInfo> *p)
{
//compute the weight of the node pointed at by p
//weight of a node is equal to the number of nodes in the correspodning subtree
if(p == NULL)
p->info.weight = 0;
else
p->info.weight = 1 + p->left->info.weight + p->right->info.weight;
//note that this is not a recursive function
}
这是treeInfo的结构:
struct treeInfo
{
char symb;
int weight;
};
这是 binaryTree.h ,它是一个普通的二叉树头
template<class Type>
struct treeNode
{
Type info;
treeNode<Type> *left;
treeNode<Type> *right;
};
template<class Type>
class treeIterator
{
protected:
treeNode<Type> *current;
stack<treeNode<Type>*> s;
public:
treeIterator(treeNode<Type> *p)
{
current = NULL;
while (p != NULL)
{
s.push(p);
p = p->left;
}
if (!s.empty())
{
current = s.top();
s.pop();
}
}
treeIterator(const treeIterator<Type>& other)
{
current = other.current;
s = other.s;
}
Type& operator*()
{ return current->info; }
treeIterator<Type>& operator++() //pre-increment operator
{
if (current != NULL)
{
current = current->right;
while (current != NULL)
{
s.push(current);
current = current->left;
}
if (!s.empty())
{
current = s.top();
s.pop();
}
}
else
cerr << "Error: treeIterator gets out of bound" << endl;
return *this;
}
bool operator==(const treeIterator<Type>& other)
{ return current == other.current; }
bool operator!=(const treeIterator<Type>& other)
{ return current != other.current; }
};
template<class Type>
class binaryTree
{
protected:
treeNode<Type> *root;
public:
binaryTree()
{ root = NULL; }
binaryTree(const binaryTree<Type>& other);
~binaryTree();
const binaryTree<Type>& operator=(const binaryTree<Type>& other);
bool empty()
{ return root == NULL; }
int height();
int nodeCount();
int leavesCount();
void inorderTraversal(void (*visit)(treeNode<Type> *));
void preorderTraversal(void (*visit)(treeNode<Type> *));
void postorderTraversal(void (*visit)(treeNode<Type> *));
void destroy();
treeIterator<Type> begin();
treeIterator<Type> end();
void print(int inc);
void buildTreeFromArray(Type a[], int n, Type nullSymbol);
private:
treeNode<Type>* copyTree(const treeNode<Type> *other);
void destroy(treeNode<Type> *p);
int height(treeNode<Type> *p);
int nodeCount(treeNode<Type> *p);
int leavesCount(treeNode<Type> *p);
void inorder(treeNode<Type> *p, void (*visit)(treeNode<Type> *));
void postorder(treeNode<Type> *p, void (*visit)(treeNode<Type> *));
void printTree(const treeNode<Type> *p, int indent, int inc);
treeNode<Type>* buildTree(Type a[], int n, int i, Type nullSymbol);
};
template<class Type>
void binaryTree<Type>::preorderTraversal(void (*visit)(treeNode<Type> *p))
{
//implement a non-recrusive preorder traversal of the binary tree
stack<treeNode<Type>*> stack_tree;
stack_tree.push(root);
treeNode<Type> *p = root;
while(!stack_tree.empty())
{
treeNode<Type>* temp = stack_tree.top();
(*visit)(temp);
stack_tree.pop();
if(temp ->right)
stack_tree.push(temp ->right);
if(temp ->left)
stack_tree.push(temp ->left);
}
}
template<class Type>
treeNode<Type>* binaryTree<Type>::buildTree(Type a[], int n, int i, Type nullSymbol)
{
treeNode<Type> *p = NULL;
if (i < n && a[i] != nullSymbol)
{
p = new treeNode<Type>;
p->info = a[i];
p->left = buildTree(a, n, 2*i+1, nullSymbol);
p->right = buildTree(a, n, 2*(i+1), nullSymbol);
}
return p;
}
template<class Type>
void binaryTree<Type>::buildTreeFromArray(Type a[], int n, Type nullSymbol)
{
root = buildTree(a, n, 0, nullSymbol);
}
template<class Type>
void binaryTree<Type>::printTree(const treeNode<Type> *p, int indent, int inc)
{
if (p != NULL)
{
printTree(p->right, indent+inc, inc);
cout << setw(indent) << p->info << endl;
printTree(p->left, indent+inc, inc);
}
}
template<class Type>
void binaryTree<Type>::print(int inc)
{
printTree(root, 4, inc);
}
template<class Type>
int binaryTree<Type>::height(treeNode<Type> *p)
{
if (p == NULL)
return 0;
int HL = height(p->left);
int HR = height(p->right);
if (HL >= HR)
return 1+HL;
else
return 1+HR;
}
template<class Type>
int binaryTree<Type>::height()
{
return height(root);
}
template<class Type>
int binaryTree<Type>::nodeCount(treeNode<Type> *p)
{
if (p == NULL)
return 0;
return 1 + nodeCount(p->left) + nodeCount(p->right);
}
template<class Type>
int binaryTree<Type>::nodeCount()
{
return nodeCount(root);
}
template<class Type>
int binaryTree<Type>::leavesCount(treeNode<Type> *p)
{
if (p == NULL)
return 0;
if (p->left == NULL && p->right == NULL)
return 1;
return leavesCount(p->left) + leavesCount(p->right);
}
template<class Type>
int binaryTree<Type>::leavesCount()
{
return leavesCount(root);
}
template<class Type>
void binaryTree<Type>::inorder(treeNode<Type> *p, void (*visit)(treeNode<Type> *))
{
if (p != NULL)
{
inorder(p->left, visit);
(*visit)(p);
inorder(p->right, visit);
}
}
template<class Type>
void binaryTree<Type>::postorder(treeNode<Type> *p, void (*visit)(treeNode<Type> *))
{
if (p != NULL)
{
postorder(p->left, visit);
postorder(p->right, visit);
(*visit)(p);
}
}
template<class Type>
void binaryTree<Type>::inorderTraversal(void (*visit)(treeNode<Type> *))
{
inorder(root, visit);
}
template<class Type>
void binaryTree<Type>::postorderTraversal(void (*visit)(treeNode<Type> *))
{
postorder(root, visit);
}
template<class Type>
treeNode<Type>* binaryTree<Type>::copyTree(const treeNode<Type> *other)
{
if (other == NULL)
return NULL;
treeNode *p = new treeNode<Type>;
p->info = other->info;
p->left = copyTree(other->left);
p->right = copyTree(other->right);
}
template<class Type>
binaryTree<Type>::binaryTree(const binaryTree<Type>& other)
{
root = copyTree(other.root);
}
template<class Type>
const binaryTree<Type>& binaryTree<Type>::operator=(const binaryTree<Type>& other)
{
if (this != &other)
{
destroy(root);
root = copyTree(other.root);
}
}
template<class Type>
void binaryTree<Type>::destroy(treeNode<Type> *p)
{
if (p != NULL)
{
destroy(p->left);
destroy(p->right);
delete p;
}
}
template<class Type>
void binaryTree<Type>::destroy()
{
destroy(root);
root = NULL;
}
template<class Type>
binaryTree<Type>::~binaryTree()
{
destroy(root);
}
template<class Type>
treeIterator<Type> binaryTree<Type>::begin()
{
return treeIterator<Type>(root);
}
template<class Type>
treeIterator<Type> binaryTree<Type>::end()
{
return treeIterator<Type>(NULL);
}
#endif