最近在搞手写二叉树,单单插入元素就搞了3天…
话不多说,上代码:
节点与树:
TT
struct Node
{
T data;
Node<T>* leftNode;
Node<T>* rightNode;
};
TT
class myTree
{
public:
myTree();
// ~myTree();
void PrintNode(Node<T>* inputNode);
void PrintTree();
Node<T>** FindNode(Node<T>* p,T x);
void Insert(T x);
int Size();
T Delete(T x);
//private:
Node<T>* firstNode;
int TreeSize;
};
TT
myTree<T>::myTree()
{
firstNode=nullptr;
TreeSize=0;
}
打印节点、打印树等简单成员函数:
TT
void myTree<T>::PrintNode(Node<T>* inputNode)
{
Node<T>* p=inputNode;
if (p->leftNode!=nullptr)
PrintNode(p->leftNode);
cout<<p->data<<' ';
if (p->rightNode!=nullptr)
PrintNode(p->rightNode);
}
TT
void myTree<T>::PrintTree()
{
PrintNode