以前收集的代码,贴给你,希望对你能有所帮助.二叉树的代码实现如下:
// FileName : btree.h#ifndef __BINARY_TREE_H__
#define __BINARY_TREE_H__#include
#include
#define DEBUG_NEW new (_NORMAL_BLOCK, THIS_FILE, __LINE__)
#endif#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif#ifdef _DEBUG
#ifndef ASSERT
#define ASSERT assert
#endif
#else // not _DEBUG
#ifndef ASSERT
#define ASSERT
#endif
#endif // _DEBUGtemplate
class CBTNode
{
public:
T data;
CBTNode
CBTNode
CBTNode
CBTNode(
T data = T(),
CBTNode
CBTNode
CBTNode
) : data(data), parent(parent), left(left), right(right) {}
};template
class CBTree
{
protected:
CBTNode
CBTree(CBTNode
~CBTree();
void AssignTo(CBTNode
void Copy(CBTree
CBTNode
const CBTNode
void (*Visit)(const T &data)
) const; void InOrderTraverse(
const CBTNode
void (*Visit)(const T &data)
) const; void PostOrderTraverse(
const CBTNode
void (*Visit)(const T &data)
) const; void GetNodesCount(const CBTNode
T& GetNodeData(CBTNode
T GetNodeData(const CBTNode
void SetNodeData(CBTNode
CBTNode
CBTNode
CBTNode
CBTNode
CBTNode
CBTNode
CBTNode
CBTNode
CBTNode
CBTNode
CBTNode
CBTNode
int IsEmpty() const;
void Destroy();
void PreOrderTraverse(void (*Visit)(const T &data)) const;
void InOrderTraverse(void (*Visit)(const T &data)) const;
void PostOrderTraverse(void (*Visit)(const T &data)) const;
unsigned int GetNodesCount() const; // Get how many nodes
unsigned int GetLeafCount() const;
unsigned int GetDepth() const;
unsigned int GetDepth(const CBTNode
};template
inline CBTree
{
}template
inline CBTree
{
Destroy();
}template
inline void CBTree
{
ASSERT(p);
m_pNodeRoot = p;
}template
inline void CBTree
{
if (NULL != p.m_pNodeRoot)
m_pNodeRoot = Copy(p.m_pNodeRoot);
else
m_pNodeRoot = NULL;
}template
inline CBTNode
{
if (p)
{
CBTNode
if (NULL == pNewNode)
return NULL;
pNewNode->data = p->data;
pNewNode->parent = p->parent;
pNewNode->left = Copy(p->left);
pNewNode->right = Copy(p->right);
return pNewNode;
}
else
return NULL;
}template
inline CBTNode
{
ASSERT(p);
return *(&(p->left));
}template
inline CBTNode
{
ASSERT(p);
return p->left;
}template
inline CBTNode
{
ASSERT(p);
return *(&(p->right));
}template
inline CBTNode
{
ASSERT(p);
return p->right;
}template
inline CBTNode
{
ASSERT(p); if (p->parent)
return *(&(p->parent->left));
else
return *(&(p->parent)); // return NULL;
}template
inline CBTNode
{
ASSERT(p); if (p->parent)
return p->parent->left;
else
return p->parent; // return NULL;
}template
inline CBTNode
{
ASSERT(p); if (p->parent)
return *(&(p->parent->right));
else
return *(&(p->parent)); // return NULL;
}template
inline CBTNode
{
ASSERT(p); if (p->parent)
return p->parent->right;
else
return p->parent; // return NULL;
}template
inline CBTNode
{
ASSERT(p);
return *(&(p->parent));
}template
inline CBTNode
{
ASSERT(p);
return p->parent;
}template
inline T& CBTree
{
ASSERT(p);
return p->data;
}template
inline T CBTree
{
ASSERT(p);
return p->data;
}template
inline void CBTree
{
ASSERT(p);
p->data = data;
}template
inline int CBTree
{
return NULL == m_pNodeRoot;
}template
inline CBTNode
{
return *(&(m_pNodeRoot));
}template
inline CBTNode
{
return m_pNodeRoot;
}template
inline void CBTree
{
if (p)
{
DestroyNode(p->left);
DestroyNode(p->right);
delete p;
}
}template
inline void CBTree
{
DestroyNode(m_pNodeRoot);
m_pNodeRoot = NULL;
}template
inline void CBTree
{
PreOrderTraverse(m_pNodeRoot, Visit);
}template
inline void CBTree
const CBTNode
void (*Visit)(const T &data)
) const
{
if (p)
{
Visit(p->data);
PreOrderTraverse(p->left, Visit);
PreOrderTraverse(p->right, Visit);
}
}template
inline void CBTree
{
InOrderTraverse(m_pNodeRoot, Visit);
}template
inline void CBTree
const CBTNode
void (*Visit)(const T &data)
) const
{
if (p)
{
InOrderTraverse(p->left, Visit);
Visit(p->data);
InOrderTraverse(p->right, Visit);
}
}template
inline void CBTree
{
PostOrderTraverse(m_pNodeRoot, Visit);
}template
inline void CBTree
const CBTNode
void (*Visit)(const T &data)
) const
{
if (p)
{
PostOrderTraverse(p->left, Visit);
PostOrderTraverse(p->right, Visit);
Visit(p->data);
}
}template
inline unsigned int CBTree
{
unsigned int unCount;
GetNodesCount(m_pNodeRoot, &unCount);
return unCount;
}template
inline void CBTree
const CBTNode
unsigned int *unCount
) const
{
ASSERT(unCount); unsigned int unLeftCount;
unsigned int unRightCount; if (NULL == p)
*unCount = 0;
else if ((NULL == p->left) && (NULL == p->right))
*unCount = 1;
else
{
GetNodesCount(p->left, &unLeftCount);
GetNodesCount(p->right, &unRightCount);
*unCount = 1 + unLeftCount + unRightCount;
}
}template
inline unsigned int CBTree
{
unsigned int unCount = 0;
GetLeafCount(m_pNodeRoot, &unCount);
return unCount;
}template
inline void CBTree
const CBTNode
unsigned int *unCount
) const
{
ASSERT(unCount); if (p)
{
// if the node's left & right children are both NULL, it must be a leaf
if ((NULL == p->left) && (NULL == p->right))
++(*unCount);
GetLeafCount(p->left, unCount);
GetLeafCount(p->right, unCount);
}
}template
inline unsigned int CBTree
{
// Minus 1 here because I think the root node's depth should be 0.
// So, don't do it if u think the root node's depth should be 1.
return GetDepth(m_pNodeRoot) - 1;
}template
inline unsigned int CBTree
{
unsigned int unDepthLeft;
unsigned int unDepthRight; if (p)
{
unDepthLeft = GetDepth(p->left);
unDepthRight = GetDepth(p->right);
return 1 + // if don't plus 1 here, the tree's depth will be always 0
(unDepthLeft > unDepthRight ? unDepthLeft : unDepthRight);
}
else
return 0;
}#endif // __BINARY_TREE_H__
设计一个计算器,当当前节点的左右孩子结点都没得的时候,计算器增加!
#include
#include
using namespace std;
int geshu=0;
template
struct Node
{
T data;
Node
};
template
class shu
{
public:
zhongshu();
~shu();
void qianli(Node
void zhongli(Node
void houli(Node
void menu();
void yezi(Node
private:
Node
Node
void xiaochu(Node
};
template
shu
{
root=chuangzao();
}
template
Node
{
Node
T ch;
cout<<"请输入结点数据,输入#为空"<
if(ch=="#")root=NULL;
else
{
root=new Node
root->data=ch;
root->zuohaizi=chuangzao();
root->youhaizi=chuangzao();
}
return root;
}
template
shu
{
if(root!=NULL)
xiaochu(root);
}
template
void shu
{
if(root==NULL)return;
else
{
cout<
qianli(root->youhaizi);
}
}
template
void shu
{
if(root==NULL)return;
else
{
zhongli(root->zuohaizi);
cout<
}
}
template
void shu
{
if(root==NULL)return;
else
{
houli(root->zuohaizi);
houli(root->youhaizi);
cout<
}
template
void shu
{
if(root!=NULL)
{
xiaochu(root->zuohaizi);
xiaochu(root->youhaizi);
delete root;
}
}
template
void shu
{
int t=1;
while(t!=6)
{
cout<<"1.种树"<
system("cls"); switch(t)
{
case 1:zhongshu();break;
case 2:qianli(root);break;
case 3:zhongli(root);break;
case 4:houli(root);break;
case 5:yezi(root);cout<<"叶子个数为"<
}
};}
template
void shu
{
if(root!=NULL)
{
if(root->zuohaizi==NULL&&root->youhaizi==NULL){cout<
{
yezi(root->zuohaizi);
yezi(root->youhaizi);
}
}
else return;
}
int main()
{
shu
shu1.menu(); return 0;
}