20树结构的类实现

树结构的类实现

1.二叉树类
(1)结点类的定义
template <class Type>
class binary_node
{
  public:
    Type data;
       binary_node<Type> * Lson, *Rson;
      binary_node(){ };    /     /构造函数
      binary_node(const Type &x) //重载构造函数,为新结点赋值
        {  data=x; Lson=Rson=NULL;}
};
(2)二叉树类的定义
template <class Type>
class binary_tree
{
protected:
    binary_node<Type> * root;
public:
    binary_tree();        //构造函数
    ~binary_tree();    //析构函数
    bool empty();        //树空否
     void preorder(void(*visit)( binary_node<Type> &));    
                   //三种遍历函数
    void inorder(void(*visit)( binary_node<Type> &));
    void postorder(void(*visit)( binary_node<Type> &));
    void creat();        //构造二叉树
    //可以在此处加其他成员函数
}
emplate <class Type>        //构造函数
binary_tree<type> :: binary_tree()
{  root=NULL; }

template <class Type>    //测试空树函数
bool binary_tree<type> :: empty()
{  return root==NULL; }
(3)中序遍历函数的实现之一:
template <class Type>
void binary_tree<type> ::inorder( void(*visit)( binary_node<Type> &))
{  re_inorder(root,visit); }
template <class Type>
void binary_tree<type>::
re_inorder( binary_node<Type> *p, void(*visit)( binary_node<Type> &))
{  if(!p) return;    //若p等于NULL,返回
  re_inorder(p->Lson, visit);
  (*visit)(p);        //抽象的访问函数
  re_inorder(p->Rson, visit);
}
(4)中序遍历函数的实现之二:
void binary_tree<Type> ::inorder()
{  re_inorder(root);
cout<<endl;
}
template <class Type>
void binary_tree<Type> ::re_inorder( binary_node<Type> *p)
{  if(!p) return;    //若p等于NULL,返回
  re_inorder(p->Lson);
  cout<<p->data<<"   “;    //输出结点值
  re_inorder(p->Rson);
}

2.检索树类

下面定义的检索树类bin_search_tree

继承二叉树类binary_tree的成员数据(根指针root)和成员函数,并追加检索树特需的成员函数:插入、删除、构造、查找,结点类同binary_node类

1)由普通二叉树类派生出检索树类
template <class Type>
class bin_search_tree: public binary_tree<Type>
{
private:
  void find_and_insert( Type x , binary_node<Type> * &p);      //插入x
  void delete_2(binary_node<Type> * q, binary_node<Type> * &r);                                 
//x有两个儿子的删除操作
  int  delete_1(Type x, binary_node<Type> * &p);    //删除x
  int search_node( Type x , binary_node<Type> * p);  //查找x
public:
  void insert(Type x);        //x是插入结点值
  int  remove(Type x);    //x是待删除结点值
  int  search(Type x);              //x是待查找结点值
  void creat();
};
2)插入函数 template <class Type> void bin_search_tree<Type>::insert(Type x) //x是插入结点值 { find_and_insert(x, root); return; } template <class Type> void bin_search_tree<Type>::find_and_insert(Type x, binary_node<Type> * &p) //x是要插入的结点值 { if(p==NULL) //当遇到空树时 { p=new binary_node<Type>(x); //申请一个新结点 return ; //插入完毕,返回 } //p不等于NULL,递归地向下层为x查找有序位置 if(x<=p->data)
    find_and_insert(x,p->Lson); //递归地向左 else
    find_and_insert(x,p->Rson); //递归地向右 }
3)构造函数 template <class Type> void bin_search_tree<Type>::creat() { Type x; cin>>x; //读入结点序列 while(x!=ZERO) //ZERO是输入结束标记 { insert(x); //插入结点x cin>>x; //读入下一个要插入的结点 } }
4)删除函数 template <class Type> void bin_search_tree<Type>::delete_2(binary_node<Type> * q, binary_node<Type> * &r) //x有两个儿子的删除操作 { if(r->Rson!=NULL)
    delete_2(q,r->Rson); //递归的向右找x的中序前驱 else { q->data=r->data; q=r; r=r->Lson; //将r之父的右链改为指向r的左儿子 delete q; } } template <class Type> int bin_search_tree<Type>::delete_1(Type x, binary_node<Type> * &p) //x是要删除的结点值 { binary_node<Type> *q; if(p==NULL) return NOTFOUND; //遇到空树时,删除不成功,返回0 if(x<p->data)
     return delete_1(x,p->Lson); //递归地向左 else if(x>p->data)
     return delete_1(x,p->Rson); //递归地向右 //找到x结点 q=p; //q将被释放的结点 if(p->Rson==NULL) //x是叶,或x只有左儿子 { p=p->Lson; //注意,此句用于修改x之父的链域 delete q; //释放x所占结点 } else if(p->Lson==NULL) //x只有右儿子 { p=p->Rson; //修改x之父的链域 delete q;   } else
  delete_2(q,q->Lson); //x有两个儿子 return OK; //删除成功 } template <class Type> int bin_search_tree<Type>::remove(Type x) //x是要删除的结点值 { return delete_1(x, root); }
5)查找函数 template <class Type> int bin_search_tree<Type>::search(Type x) //x是要查找的结点值 { return search_node(x, root); } template <class Type> int bin_search_tree<Type>::search_node(Type x,binary_node<Type> *p) //x是查找结点值 { if(p==NULL) //当遇到空树时 return NOTFOUND; //查找失败之处 if(x<p->data)
   return search_node(x,p->Lson);//递归地向左 elseif(x>p->data)
   return search_node(x,p->Rson);//递归地向右 return OK; }
原文地址:https://www.cnblogs.com/gd-luojialin/p/8509369.html