您的位置:首页 > 编程语言 > C语言/C++

C++实现二叉搜索树:增删改查等几乎所有常用接口

2013-12-22 16:11 615 查看
提问者: Best_幻_

都有什么方法可以唯一确定一颗二叉树。
给定一颗二叉树的按层次遍历序列和后序遍历序列,可以确定唯一的一颗二叉树吗?
求详解,



回答共1条

1秒前carea

给出中序遍历之后再给一个其他的遍历就能够确定了,前序和后续不能确定,因为一个跟在最前,一个根在最后,无法区分左右孩子,不能递归的将孩子分开。


头文件binary_tree_temp.h

v3:加入了析构函数

#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_
#include <iostream>
#include <stack>
using namespace std;

template<typename T>
class binary_tree
{
public:
binary_tree():root_(NULL){ }//OK
binary_tree(const T* , const int);//OK
~binary_tree(void){ clear();}//OK
void insert(const T& data);//OK
bool empty(void) const { return (NULL==root_) ; }//OK
bool exists(const T& data) const;//OK
void clear(void);//OK
void erase(const T& data);
int size(void) const;//OK
T minmum(void) const;//OK
T maxmum(void) const;//OK
template<typename T>
friend ostream& operator<<(ostream& out,const binary_tree<T>& tree);//OK
void print(ostream& out) const ;//OK
void print_in_order(void) const;//OK
void print_successor(void) const;//OK
void print_post_order(void) const;//OK
void print_pre_order(void) const;//OK
private:
struct tree_node//OK
{
tree_node():data(T()),parent(NULL),lchild(NULL),rchild(NULL){}
tree_node(const T& t):data(t),parent(NULL),lchild(NULL),rchild(NULL){}
T data;
tree_node *parent,*lchild,*rchild;
};
binary_tree(const binary_tree&);
binary_tree& operator = (const binary_tree&);
void print_binary_tree(ostream& ,const tree_node* bt, int depth) const;//OK
int count_node(const tree_node* ptree) const;//OK
tree_node* find(const T& data);//OK
tree_node* maxmum(tree_node*) const;//OK
tree_node* root_;//OK
tree_node* minmum(tree_node*) const;//OK
tree_node* successor(tree_node* t) const;
};

template<typename T>
binary_tree<T>::binary_tree(const T* arr,const int length)
{
for (int i=0;i<length;i++)
{
insert(arr[i]);
}
}

template<typename T>
void binary_tree<T>::insert(const T& data)
{
if (0 != root_)
{
tree_node *fast,*slow,*ptemp;
fast=slow=ptemp=root_;

while (fast != 0)
{
slow=fast;
if (data < slow->data)
{
fast=slow->lchild;
}
else if (data > slow->data)
{
fast=slow->rchild;
}
else
{
fast = 0;
}
//esle equal do nothing
}
if (data < slow->data)
{
slow->lchild = new tree_node(data);
slow->lchild->parent = slow;
}
else if (data > slow->data)
{
slow->rchild = new tree_node(data);
slow->rchild->parent = slow;
}
//esle equal do nothing
}
else
{
root_ = new tree_node(data);
}
}

template<typename T>
void binary_tree<T>::print_binary_tree(ostream& out,const tree_node* bt, int depth) const
{
//用右左孩子的方式输出一颗树,先输出右孩子后输出左孩子
if (bt)
{
print_binary_tree(out,bt->rchild,depth+1);

if (depth==0)
{
out<<bt->data<<endl;
}
else if (depth==1)
{
out<<" --"<<bt->data<<endl;
}
else
{
int n=depth;
while(--n)
{
cout<<"    ";
}
out<<" --"<<bt->data<<endl;
}

print_binary_tree(out,bt->lchild,depth+1);
}
}

template<typename T>
void binary_tree<T>::print(ostream& out) const
{
print_binary_tree(out,root_ , 0);
}

template<typename T>
void binary_tree<T>::print_in_order(void) const
{
cout<<"print_in_order : ";
stack<tree_node*> tempstack;
tree_node* t=root_;
if (t!=NULL)
{
do
{
tempstack.push(t);
t=t->lchild;
} while (t!=NULL);
}
while (!tempstack.empty())
{
tree_node* p=tempstack.top();
cout<<p->data<<" ";
tempstack.pop();
if (p->rchild!=NULL)
{
p=p->rchild;
do
{
tempstack.push(p);
p=p->lchild;
} while (p!=NULL);
}
}
cout<<endl;
}

template<typename T>
ostream& operator<<(ostream& out,const binary_tree<T>& tree)
{
tree.print(out);
return out;
}

template<typename T>
void binary_tree<T>::print_post_order(void) const
{
//后续序序遍历输出一颗树的全部结点值2,3,1
//广度优先遍历
cout<<"print_post_order : ";
typedef pair<tree_node*,bool> multinode;
stack<multinode> tempstack;
if (root_)
{
tempstack.push(make_pair(root_,false));
}
while (!tempstack.empty())
{
multinode m=tempstack.top();tempstack.pop();
if (m.first->lchild==NULL && m.first->rchild==NULL)
{//叶子节点直接输出
cout<<m.first->data<<" ";
}
else if (m.second==true)
{//所有孩子都遍历完了才会到这一步
cout<<m.first->data<<" ";
}
else
{//非终结点,并且孩子还没遍历完。
m.second=true;tempstack.push(m);
if (m.first->rchild!=NULL)
{
tempstack.push(make_pair(m.first->rchild,false));
}
if (m.first->lchild!=NULL)
{
tempstack.push(make_pair(m.first->lchild,false));
}
}
}
cout<<endl;
}

template<typename T>
void binary_tree<T>::print_pre_order(void) const
{
//先序遍历输出一颗树的全部结点值1,2,3,先根遍历
cout<<"print_pre_order : ";
stack<tree_node*> node_stack;
if (root_)
{
node_stack.push(root_);
tree_node* t;
while (!node_stack.empty())
{
t=node_stack.top();
node_stack.pop();
cout<<t->data<<" ";
if (t->rchild!=0)
{
node_stack.push(t->rchild);
}
if (t->lchild!=0)
{
node_stack.push(t->lchild);
}
}
cout<<endl;
}
}

template<typename T>
bool binary_tree<T>::exists(const T& data) const
{
bool result = false;
if (root_)
{
tree_node* pfind=root_;
while (pfind)
{
if (pfind->data == data)
{
result = true;
break;
}
else if (data < pfind->data)
{
pfind = pfind->lchild;
}
else
pfind = pfind->rchild;
}
}
return result;
}

template<typename T>
typename binary_tree<T>::tree_node* binary_tree<T>::find(const T& data)
{
if (root_)
{
tree_node* pfind=root_;
while (pfind)
{
if (pfind->data == data)
{
return pfind;
}
else if (data < pfind->data)
{
pfind = pfind->lchild;
}
else
pfind = pfind->rchild;
}
}
return NULL;
}

template<typename T>
void binary_tree<T>::clear(void)
{
//先序遍历删除一颗树的全部结点值1,2,3,先根遍历
if (root_)
{
stack<tree_node*> node_stack;
node_stack.push(root_);
tree_node* t;
while (!node_stack.empty())
{
t=node_stack.top();
node_stack.pop();
if (t->rchild!=0)
{
node_stack.push(t->rchild);
}
if (t->lchild!=0)
{
node_stack.push(t->lchild);
}
delete t;
}
root_ = NULL ;
}
}

template<typename T>
int binary_tree<T>::count_node(const tree_node* ptree) const
{
int num1,num2;
if(ptree == NULL)
return 0;
else
{
num1=count_node(ptree->lchild);
num2=count_node(ptree->rchild);
return (num1+num2+1);
}
}

template<typename T>
int binary_tree<T>::size(void) const
{
return count_node(root_);
}

template<typename T>
T binary_tree<T>::minmum(void) const
{
const tree_node* p=root_;
while(p->lchild)
{
p=p->lchild;
}
return p->data;
}

template<typename T>
typename binary_tree<T>::tree_node* binary_tree<T>::minmum(tree_node* p) const
{
while(p->lchild)
{
p=p->lchild;
}
return p;
}

template<typename T>
T binary_tree<T>::maxmum(void) const
{
const tree_node* p=root_;
while(p->rchild)
{
p=p->rchild;
}
return p->data;
}

template<typename T>
typename binary_tree<T>::tree_node* binary_tree<T>::maxmum(tree_node* t) const
{
while(t->rchild)
{
t=t->rchild;
}
return t;
}

template<typename T>
typename binary_tree<T>::tree_node* binary_tree<T>::successor(tree_node* t) const
{
if (t->rchild)
{
return minmum(t->rchild);
}
else
{
tree_node* fast = t->parent , *slow =t;
while (fast && fast->rchild == slow)
{
slow = fast;
fast = fast->parent;
}
return fast;
}
}

template<typename T>
void binary_tree<T>::print_successor(void) const
{
cout<<"print_successor : ";
if (!empty())
{
tree_node* p=minmum(root_);
cout<<p->data<<" ";
while (p=successor(p))
{
cout<<p->data<<" ";
}
cout<<endl;
}
}

template<typename T>
void binary_tree<T>::erase(const T& data)
{
tree_node* iter = find(data) ;
//delete iter;
}

#endif


v2:增加了删除节点操作

#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_
#include <iostream>
#include <stack>
using namespace std;

template<typename T>
class binary_tree
{
public:
binary_tree():root_(NULL){ }//OK
binary_tree(const T* , const int);//OK
void insert(const T& data);//OK
bool empty(void) const { return (NULL==root_) ; }//OK
bool exists(const T& data) const;//OK
void clear(void);//OK
void erase(const T& data);
int size(void) const;//OK
T minmum(void) const;//OK
T maxmum(void) const;//OK
template<typename T>
friend ostream& operator<<(ostream& out,const binary_tree<T>& tree);//OK
void print(ostream& out) const ;//OK
void print_in_order(void) const;//OK
void print_successor(void) const;//OK
void print_post_order(void) const;//OK
void print_pre_order(void) const;//OK
private:
struct tree_node//OK
{
tree_node():data(T()),parent(NULL),lchild(NULL),rchild(NULL){}
tree_node(const T& t):data(t),parent(NULL),lchild(NULL),rchild(NULL){}
T data;
tree_node *parent,*lchild,*rchild;
};
binary_tree(const binary_tree&);
binary_tree& operator = (const binary_tree&);
void print_binary_tree(ostream& ,const tree_node* bt, int depth) const;//OK
int count_node(const tree_node* ptree) const;//OK
tree_node* find(const T& data);//OK
tree_node* maxmum(tree_node*) const;//OK
tree_node* root_;//OK
tree_node* minmum(tree_node*) const;//OK
tree_node* successor(tree_node* t) const;
};

template<typename T>
binary_tree<T>::binary_tree(const T* arr,const int length)
{
for (int i=0;i<length;i++)
{
insert(arr[i]);
}
}

template<typename T>
void binary_tree<T>::insert(const T& data)
{
if (0 != root_)
{
tree_node *fast,*slow,*ptemp;
fast=slow=ptemp=root_;

while (fast != 0)
{
slow=fast;
if (data < slow->data)
{
fast=slow->lchild;
}
else if (data > slow->data)
{
fast=slow->rchild;
}
else
{
fast = 0;
}
//esle equal do nothing
}
if (data < slow->data)
{
slow->lchild = new tree_node(data);
slow->lchild->parent = slow;
}
else if (data > slow->data)
{
slow->rchild = new tree_node(data);
slow->rchild->parent = slow;
}
//esle equal do nothing
}
else
{
root_ = new tree_node(data);
}
}

template<typename T>
void binary_tree<T>::print_binary_tree(ostream& out,const tree_node* bt, int depth) const
{
//用右左孩子的方式输出一颗树,先输出右孩子后输出左孩子
if (bt)
{
print_binary_tree(out,bt->rchild,depth+1);

if (depth==0)
{
out<<bt->data<<endl;
}
else if (depth==1)
{
out<<" --"<<bt->data<<endl;
}
else
{
int n=depth;
while(--n)
{
cout<<"    ";
}
out<<" --"<<bt->data<<endl;
}

print_binary_tree(out,bt->lchild,depth+1);
}
}

template<typename T>
void binary_tree<T>::print(ostream& out) const
{
print_binary_tree(out,root_ , 0);
}

template<typename T>
void binary_tree<T>::print_in_order(void) const
{
cout<<"print_in_order : ";
stack<tree_node*> tempstack;
tree_node* t=root_;
if (t!=NULL)
{
do
{
tempstack.push(t);
t=t->lchild;
} while (t!=NULL);
}
while (!tempstack.empty())
{
tree_node* p=tempstack.top();
cout<<p->data<<" ";
tempstack.pop();
if (p->rchild!=NULL)
{
p=p->rchild;
do
{
tempstack.push(p);
p=p->lchild;
} while (p!=NULL);
}
}
cout<<endl;
}

template<typename T>
ostream& operator<<(ostream& out,const binary_tree<T>& tree)
{
tree.print(out);
return out;
}

template<typename T>
void binary_tree<T>::print_post_order(void) const
{
//后续序序遍历输出一颗树的全部结点值2,3,1
//广度优先遍历
cout<<"print_post_order : ";
typedef pair<tree_node*,bool> multinode;
stack<multinode> tempstack;
if (root_)
{
tempstack.push(make_pair(root_,false));
}
while (!tempstack.empty())
{
multinode m=tempstack.top();tempstack.pop();
if (m.first->lchild==NULL && m.first->rchild==NULL)
{//叶子节点直接输出
cout<<m.first->data<<" ";
}
else if (m.second==true)
{//所有孩子都遍历完了才会到这一步
cout<<m.first->data<<" ";
}
else
{//非终结点,并且孩子还没遍历完。
m.second=true;tempstack.push(m);
if (m.first->rchild!=NULL)
{
tempstack.push(make_pair(m.first->rchild,false));
}
if (m.first->lchild!=NULL)
{
tempstack.push(make_pair(m.first->lchild,false));
}
}
}
cout<<endl;
}

template<typename T>
void binary_tree<T>::print_pre_order(void) const
{
//先序遍历输出一颗树的全部结点值1,2,3,先根遍历
cout<<"print_pre_order : ";
stack<tree_node*> node_stack;
if (root_)
{
node_stack.push(root_);
tree_node* t;
while (!node_stack.empty())
{
t=node_stack.top();
node_stack.pop();
cout<<t->data<<" ";
if (t->rchild!=0)
{
node_stack.push(t->rchild);
}
if (t->lchild!=0)
{
node_stack.push(t->lchild);
}
}
cout<<endl;
}
}

template<typename T>
bool binary_tree<T>::exists(const T& data) const
{
bool result = false;
if (root_)
{
tree_node* pfind=root_;
while (pfind)
{
if (pfind->data == data)
{
result = true;
break;
}
else if (data < pfind->data)
{
pfind = pfind->lchild;
}
else
pfind = pfind->rchild;
}
}
return result;
}

template<typename T>
typename binary_tree<T>::tree_node* binary_tree<T>::find(const T& data)
{
if (root_)
{
tree_node* pfind=root_;
while (pfind)
{
if (pfind->data == data)
{
return pfind;
}
else if (data < pfind->data)
{
pfind = pfind->lchild;
}
else
pfind = pfind->rchild;
}
}
return NULL;
}

template<typename T>
void binary_tree<T>::clear(void)
{
//先序遍历删除一颗树的全部结点值1,2,3,先根遍历
if (root_)
{
stack<tree_node*> node_stack;
node_stack.push(root_);
tree_node* t;
while (!node_stack.empty())
{
t=node_stack.top();
node_stack.pop();
if (t->rchild!=0)
{
node_stack.push(t->rchild);
}
if (t->lchild!=0)
{
node_stack.push(t->lchild);
}
delete t;
}
root_ = NULL ;
}
}

template<typename T>
int binary_tree<T>::count_node(const tree_node* ptree) const
{
int num1,num2;
if(ptree == NULL)
return 0;
else
{
num1=count_node(ptree->lchild);
num2=count_node(ptree->rchild);
return (num1+num2+1);
}
}

template<typename T>
int binary_tree<T>::size(void) const
{
return count_node(root_);
}

template<typename T>
T binary_tree<T>::minmum(void) const
{
const tree_node* p=root_;
while(p->lchild)
{
p=p->lchild;
}
return p->data;
}

template<typename T>
typename binary_tree<T>::tree_node* binary_tree<T>::minmum(tree_node* p) const
{
while(p->lchild)
{
p=p->lchild;
}
return p;
}

template<typename T>
T binary_tree<T>::maxmum(void) const
{
const tree_node* p=root_;
while(p->rchild)
{
p=p->rchild;
}
return p->data;
}

template<typename T>
typename binary_tree<T>::tree_node* binary_tree<T>::maxmum(tree_node* t) const
{
while(t->rchild)
{
t=t->rchild;
}
return t;
}

template<typename T>
typename binary_tree<T>::tree_node* binary_tree<T>::successor(tree_node* t) const
{
if (t->rchild)
{
return minmum(t->rchild);
}
else
{
tree_node* fast = t->parent , *slow =t;
while (fast && fast->rchild == slow)
{
slow = fast;
fast = fast->parent;
}
return fast;
}
}

template<typename T>
void binary_tree<T>::print_successor(void) const
{
cout<<"print_successor : ";
if (!empty())
{
tree_node* p=minmum(root_);
cout<<p->data<<" ";
while (p=successor(p))
{
cout<<p->data<<" ";
}
cout<<endl;
}
}

template<typename T>
void binary_tree<T>::erase(const T& data)
{
tree_node* iter = find(data) ;
//delete iter;
}

#endif


目前还不能支持删除元素

源文件main.cpp

v2:

#include "binary_tree_temp.h"

#include <cstdlib>
#include <ctime>
#include <string>
#include <map>
using namespace std;

int main()
{
binary_tree<int> tree;
time_t t;
time(&t);
srand(t);
const int size=20;
for (int i=0;i<size;i++)
{
tree.insert(rand()%size);
}
if(!tree.empty())
{
cout<<tree;
cout<<"tree size : "<<tree.size()<<endl;
tree.print_in_order();
tree.print_successor();
tree.print_post_order();
tree.print_pre_order();
}
cout<<"exists "<<3<<" ? "<<tree.exists(3)<<endl;
//tree.erase(2);
cout<<"min element : "<<tree.minmum()<<endl;
cout<<"max element : "<<tree.maxmum()<<endl;
tree.clear();
cout<<tree<<endl;

return 0;
}
v2:

输出

             --19

         --18

     --17

             --16

         --15

             --14

 --12

             --11

         --10

     --9

7

 --6

             --5

         --4

             --3

     --1

tree size : 16

print_in_order : 1 3 4 5 6 7 9 10 11 12 14 15 16 17 18 19

print_successor : 1 3 4 5 6 7 9 10 11 12 14 15 16 17 18 19

print_post_order : 3 5 4 1 6 11 10 9 14 16 15 19 18 17 12 7

print_pre_order : 7 6 1 4 3 5 12 9 10 11 17 15 14 16 18 19

exists 3 ? 1

min element : 1

max element : 19

请按任意键继续. . .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: