一个简单的BTree,还未通过调试,最近太忙了,先存这里,缓几天调试
#ifndef MY_BTREE_H
#define MY_BTREE_H
#include <iostream>
using namespace std;
template <class CType> class MyBtree;
template <class Type> class MyBtreeInordeIterator;
template <class T> class BTreeNode;
/*
*brife/ 节点类
*/
template <class T>
class BTreeNode
{
public:
BTreeNode(T data,BTreeNode<T>* pLeftNode = NULL, BTreeNode<T>* pRightNode = NULL):m_data(data) , m_pLeftChild(pLeftNode) , m_pRightChild(pRightNode) , m_flags(0){}
inline T& Data(){return m_data;}
inline BTreeNode<T> * & LeftChild(){return m_pLeftChild;}
inline BTreeNode<T> * & RightChild(){return m_pRightChild;}
bool LFlag(){return 0x01&m_flags};
bool RFlag(){return 0x02&m_flags};
void SetLFlag(bool flag)
{
m_flags = flag ? 0x01|m_flags : 0x02&~m_flags ;
}
void SetRFlag(bool flag)
{
if (flag)
{
m_flags = 0x02|m_flags;
}
else
{
m_flags = 0x01&~m_flags;
}
}
private:
T m_data;
unsigned char m_flags;
BTreeNode<T>* m_pLeftChild;
BTreeNode<T>* m_pRightChild;
};
/*
*brife/ BTree类(完全2叉树)
*/
template <class CType>
class MyBtree
{
public:
MyBtree():m_end(0),m_pRoot(0),m_pCurrent(0){}
~MyBtree()
{
}
inline CType EndValue(){return m_end;}
/*
*brife/ BTree根节点
*/
BTreeNode<CType>* Root()
{
return m_pRoot ? m_pRoot : NULL;
}
/*
*brife/ pNode节点的父节点
*/
BTreeNode<CType>* Parent(BTreeNode<CType>* pNode)
{
BTreeNode<CType>* pTmp = Root();
if((pTmp = (pTmp->LeftChild() == pNode ? pTmp : Parent(pTmp->LeftChild() ,pNode)))!= NULL)
{
return pTmp;
}
else if (( pTmp = (pTmp->RightChild() == pNode ? pTmp : Parent(pTmp->RightChild() ,pNode)))!= NULL)
{
return pTmp;
}
return NULL;
}
/*
*brife/依次从左至右插入TData
*/
void Insert(CType TData)
{
BTreeNode<CType>* pTemp = new BTreeNode<CType>(TData);
if (!m_pRoot)
{
m_pRoot = pTemp;
m_pCurrent = pTemp;
}
else if (!m_pCurrent->LeftChild())
{
m_pCurrent->LeftChild() = pTemp;
}
else if (!m_pCurrent->RightChild())
{
m_pCurrent->RightChild() = pTemp;
m_pCurrent = RightBro(m_pCurrent);
}
else
{
m_pCurrent = RightBro(m_pCurrent);
if (!m_pCurrent)
m_pCurrent = FirstLeftNode(m_pCurrent);
Insert(TData);
}
}
protected:
/*
*brife/重载了输入输出操作符
*/
friend istream &operator >>(istream& in,MyBtree<CType>* tree)
{
cout<<"Please input data!"<<endl;
CType temp;
in>>temp;
while (temp != tree->EndValue())
{
tree->Insert(temp);
cout<<"Please input data!"<<endl;
in>>temp;
}
return in;
}
friend ostream &operator <<(ostream& out,MyBtree<CType>* tree)
{
BTreeNode<CType>* pTmp = tree->Root();
tree->Travel(pTmp);
return out;
}
void Travel(BTreeNode<CType>* pNode)
{
cout<<pNode->Data()<<" ";
if (pNode->LeftChild())
{
Travel(pNode->LeftChild());
}
if (pNode->RightChild())
{
Travel(pNode->RightChild());
}
}
private:
/*
*brife/从pCurNode开始查找其父节点
*/
BTreeNode<CType>* Parent(BTreeNode<CType>* pCurNode,BTreeNode<CType>* pNode)
{
BTreeNode<CType>* pTmp;
if(pCurNode->LeftChild() == pNode) return pCurNode;
else if ( (pTmp = Parent(pCurNode ->LeftChild() , pNode)) != NULL) return pTmp;
else return pCurNode->RightChild() == pNode ? pCurNode : Parent(pCurNode ->RightChild() ,pNode);
}
/*
*brife/返回pCurNode的右兄弟节点
*/
BTreeNode<CType>* RightBro(BTreeNode<CType>* pCurNode)
{
if (Parent(pCurNode)->LeftChild() == pCurNode)
return Parent(pCurNode)->RightChild();
else
{
BTreeNode<CType>* pTmp = RightBro(Parent(pCurNode));
if(pTmp)
return (pTmp->LeftChild() != pCurNode)? pTmp->LeftChild() : NULL;
}
}
/*
*brife/返回pCurNode的左兄弟节点
*/
BTreeNode<CType>* LeftBro(BTreeNode<CType>* pCurNode)
{
if (Parent(pCurNode)->RightChild() == pCurNode)
return Parent(pCurNode)->LeftChild();
else
{
BTreeNode<CType>* pTmp = LeftBro(Parent(pCurNode));
if(pTmp)
return pTmp->RightChild() != pCurNode ? pTmp->RightChild() : NULL;
}
}
/*
*brife/返回pCurNode的最左边的兄弟节点
*/
BTreeNode<CType>* FirstLeftNode(BTreeNode<CType>* pCurNode)
{
BTreeNode<CType>* pTmp = pCurNode;
BTreeNode<CType>* pRt;
while (pTmp)
{
pRt = pTmp;
pTmp = LeftBro(pTmp);
}
return pRt;
}
private:
BTreeNode<CType>* m_pRoot;//根节点
BTreeNode<CType>* m_pCurrent;//当前节点指针
CType m_end;//输入结束值
};
/*
*brife/ BTree中序线索迭代器
*/
template <class Type>
class MyBtreeInordeIterator
{
public:
MyBtreeInordeIterator(){}
~MyBtreeInordeIterator(){}
BTreeNode<Type>* First()
{
return T->Root();
}
BTreeNode<Type>* End()
{
return T->Root();
}
BTreeNode<Type>* Next()
{
return T->Root();
}
BTreeNode<Type>* Pre()
{
return T->Root();
}
protected:
MyBtree<Type>* T;//btree指针
BTreeNode<Type>* m_pCurrent;//当前指针
private:
};
#endif