//指针的二叉查找树 //插入比较简单所以就没做了 //删除太麻烦等有空再做 #include<iostream> using namespace std; template<typename Elem> class BiNode { public: BiNode(const Elem&, BiNode* pL = NULL, BiNode* pR = NULL); bool IsLeaf(){ return m_pLeft==NULL && m_pRight==NULL; } public: Elem m_Data; BiNode* m_pLeft; BiNode* m_pRight; }; //节点的构造函数 template<typename Elem> BiNode<Elem>::BiNode( const Elem& item, BiNode* pL = NULL, BiNode* pR = NULL)//① { m_Data = item; m_pLeft = pL; m_pRight = pR; } /// /// 二叉树 声明 /// /// template<typename Elem> class BiTree { public: BiTree(); void InitTree(); BiNode<Elem>* AddNode( BiNode<Elem> *root, const Elem&); void PreTraverse( const BiNode<Elem>*); const BiNode<Elem>* GetRoot(){ return m_pRoot; } private: BiNode<Elem>* m_pRoot; int m_NodeCount; int m_Depth; }; //二叉树构造函数 template<typename Elem> BiTree<Elem>::BiTree():m_NodeCount(0), m_Depth(0) { } //二叉树几点插入 template<typename Elem> BiNode<Elem>* BiTree<Elem>::AddNode( BiNode<Elem>* root, const Elem& item)//对比①究竟何时需要在模板参数中添加<> { if( root->m_Data == item )//元素相同即返回NULL return NULL; if( root->m_Data > item ) { if( root->m_pLeft != NULL ) AddNode( root->m_pLeft, item ); else { root->m_pLeft = new BiNode<Elem>( item ); return root->m_pLeft; } } else { if( root->m_pRight != NULL ) AddNode( root->m_pRight, item ); else { root->m_pRight = new BiNode<Elem>(item); return root->m_pRight; } } } template<typename Elem> void BiTree<Elem>::InitTree() { Elem item; cin>>item; m_pRoot = new BiNode<Elem>(item); m_NodeCount++; while(cin>>item&&item != '/n') { if( AddNode(m_pRoot, item)!= NULL) m_NodeCount++; } } //前周游 template<typename Elem> void BiTree<Elem>::PreTraverse( const BiNode<Elem>* root) { if( root == NULL) return; cout<<"当前元素值为"<<root->m_Data<<endl; PreTraverse( root->m_pLeft); PreTraverse( root->m_pRight ); } int main() { BiTree<int> bt; bt.InitTree(); bt.PreTraverse( bt.GetRoot()); return 0; } //这是基于数组的二叉查找树 #include<iostream> using std::cin; using std::cout; using std::endl; template<typename Elem> class BST { public: BST(int n = 30); void BuildTree(); int AddNode(const int subroot, const Elem&); void PreOrder( const int subroot); private: Elem* m_BST; int m_size; }; template<typename Elem> BST<Elem>::BST( int n = 30): m_size(n) { m_BST = new Elem[m_size]; memset( m_BST, -1, m_size*sizeof(Elem)); } template<typename Elem> int BST<Elem>::AddNode(const int subroot, const Elem& item) { if( m_BST[subroot] > item ) { if(( 2*subroot+1)>= m_size) return m_size;//越界处理 if( m_BST[2*subroot+1] != -1 ) AddNode( 2*subroot+1, item ); else { m_BST[ 2*subroot+1] = item; return (2*subroot+1); } } if( m_BST[subroot] < item ) { if( 2*subroot+2 >= m_size ) return m_size;//越界处理 if( m_BST[2*subroot+2] != -1 ) AddNode( 2*subroot+2, item ); else { m_BST[ 2*subroot+2] = item; return 2*subroot+2; } } return -1; } template<typename Elem> void BST<Elem>::BuildTree() { Elem elem; cin>>elem; m_BST[0] = elem; while(cin>>elem&& elem != -1) { this->AddNode( 0, elem ); } } template<typename Elem> void BST<Elem>::PreOrder( const int subroot) { if( m_BST[subroot] != -1 ) cout<<m_BST[subroot]<<" "; else return; if( 2*subroot+1 <= m_size ) PreOrder(2*subroot+1); if( 2*subroot+2 <= m_size ) PreOrder(2*subroot+2 ); } void main() { BST<int> bst(30); bst.BuildTree(); bst.PreOrder(0); }