一些常用的数据结构必须了解:比如基本的三种数据结构:链表,栈,队列,其中链表是最基础,实现了链表就可以利用继承或者合成的关系实现栈或者队列,他们简单在于都是线性结构;高级一点的就是二叉树,堆和优先队列,而二叉树是关键!
1、创建:
类似于链表的的创建,如创建: root ----> 60
left:55 right:100
这样简单二叉树:有结构体创建节点法和类构造法:
template<typename T>
struct treenode{
T ele;
treenode<T>*left;
treenode<T>*right;};
int main(){
treenode<int> left={55,NULL,NULL};
treenode<int> right={100,NULL,NULL};
treenode<int> root={60,&left,&right};
treenode<int>*p=&root;
cout<<"the inf:"<<endl;
cout<<" "<<p->ele<<endl;
cout<<left.ele<<" "<<right.ele<<endl;
system("pause");
return 0;}//结构体创建
template<typename T>
class treenode{
public:
T ele;
treenode<T>*left;
treenode<T>*right;
treenode(){left=NULL;
right=NULL;}
treenode(T ele){
this->ele=ele;
left=NULL;
right=NULL;}
};
int main(){
treenode<int> *root=new treenode<int>(60);
root->left=new treenode<int>(55);
root->right=new treenode<int>(100);
cout<<"the inf of tree:"<<endl;
cout<<" "<<root->ele<<endl;
cout<<root->left->ele<<" "<<root->right->ele<<endl;
system("pause");
return 0;}//类构造
2、插入数据元素
template<typename T>
bool bitree<T>::insert(T ele){
if(root==NULL) root=new treenode<T>(ele);
else{
treenode<T>*parent=NULL;
treenode<T>*p=root;
while(p!=NULL){
if(ele<p->ele){parent=p;
p=p->left;}//新元素小于父节点,则做父节点左孩纸
else if(ele>p->ele){parent=p;p=p->right;}//新元素大于父节点,则做父节点右边孩纸
else return false;
if(ele<parent->ele)parent->left=new treenode<T>(ele);
else parent->right=new treenode<T>(ele);}
}
3、遍历元素
元素的遍历一般常见的有先序(中-左-右)、中序(左-中-右)以及后序(左-右-中)遍历。其实现的思想都是一样:都是利用递归思想:例如先序遍历:先输出中间当前元素,然后以此元素为父节点继续先序遍历此节点的左子树和此节点的右子树。。。以此遍历下去:
template<typename T>
void bitree<T>::preorder(){
preorder(root);}
template<typename T>
void bitree<T>::preorder(treenode<T>*root){
if(root==NULL)return ;
else{
cout<<root->ele<<" ";
preorder(root->left);
preorder(root->right;);}//前序遍历
整体定义二叉树类如下:
template<typename T>
class treenode{
public:
T ele;
treenode<T>*left;
treenode<T>*right;
treenode(){left=NULL;
right=NULL;}
treenode(T ele){
this->ele=ele;
left=NULL;
right=NULL;}
};
template<typename T>
class bitree{
private:treenode<T>*root;
int size;
void inorder(treenode<T>*root);
void preorder(treenode<T>*root);
void postorder(treenode<T>*root);
public:
bitree(){root=NULL;size=0;}
bitree(T ele[],int size);
bool insert(T ele);
void inorder();
void preorder();
void postorder();
};
template<typename T>
bitree<T>::bitree(T ele[],int size){
root=new treenode<T>(ele[0]);
this->size=size;;
for(int i=0;i<size;i++)
insert(ele[i]);}
template<typename T>
bool bitree<T>::insert(T ele){
if(root==NULL) root=new treenode<T>(ele);
else{
treenode<T>*parent=NULL;
treenode<T>*p=root;
while(p!=NULL){
if(ele<p->ele){parent=p;
p=p->left;}//新元素小于父节点,则做父节点左孩纸
else if(ele>p->ele){parent=p;p=p->right;}//新元素大于父节点,则做父节点右边孩纸
else return false;}
if(ele<parent->ele)parent->left=new treenode<T>(ele);
else parent->right=new treenode<T>(ele);
}
size++;
return true;}
template<typename T>
void bitree<T>::inorder(){
inorder(root);}
template<typename T>
void bitree<T>::inorder(treenode<T>*root){
if(root==NULL) return;
else{
inorder(root->left);
cout<<root->ele<<" ";
inorder(root->right);
}
}//中序遍历,递归
template<typename T>
void bitree<T>::preorder(){
preorder(root);}
template<typename T>
void bitree<T>::preorder(treenode<T>*root){
if(root==NULL)return;
else{
cout<<root->ele<<" ";
preorder(root->left);
preorder(root->right);}}//前序遍历
template<typename T>
void bitree<T>::postorder(){
postorder(root);}
template<typename T>
void bitree<T>::postorder(treenode<T>*root){
if(root==NULL) return;
else{postorder(root->left);
postorder(root->right);
cout<<root->ele<<" ";
}}//后序
调用结果: