二叉連結串列
阿新 • • 發佈:2018-11-21
#include<iostream>
#include<string>
using namespace std;
struct Node
{
string data;
Node *Lchild,*Rchild;
};
class BiTree
{
public:
BiTree(){root=Create(root);} //建構函式,建立一顆二叉樹
~BiTree(){Release(root);} //解構函式,釋放各節點的儲存空間
void Preorder(){Preorder(root);} //前序
void Inorder(){Inorder(root);} //中序
void Postorder(){Postorder(root);} //後序
void Leverorder(); //層序
void print(){print(root);}
void print_family(){print_family(root);}
private:
Node *root; //指向根的頭
Node *Create(Node *bt); //構造呼叫
void Release(Node *bt); //析構呼叫
void Preorder(Node *bt); //前序
void Inorder(Node *bt); //中序
void Postorder(Node *bt); //後序
void PrintParent(Node *bt); //輸出雙親
void print(Node *bt);
void print_family(Node *bt); //輸出親戚
};
Node*BiTree::Create(Node *bt)
{
string s;
cin>>s;
if(s=="none")
bt=NULL;
else{
bt=new Node;
bt->data=s;
bt->Lchild=Create(bt->Lchild);
bt->Rchild=Create(bt->Rchild);
}
return bt;
}
void BiTree::Release(Node *bt)
{
if(bt!=NULL){
Release(bt->Lchild);
Release(bt->Rchild);
delete bt;
}
}
void BiTree::Preorder(Node *bt)
{
if(bt==NULL) return;
else{
cout<<bt->data<<'\t';
Preorder(bt->Lchild);
Preorder(bt->Rchild);
}
}
void BiTree::Inorder(Node *bt)
{
if(bt==NULL)
return;
else{
Inorder(bt->Lchild);
cout<<bt->data<<'\t';
Inorder(bt->Rchild);
}
}
void BiTree::Postorder(Node *bt)
{
if(bt==NULL)
return;
else{
Postorder(bt->Lchild);
Postorder(bt->Rchild);
cout<<bt->data<<'\t';
}
}
void BiTree::Leverorder()
{
Node *Q[10];
int front,rear;
front=rear=-1;
if(root==NULL)
return;
Q[++rear]=root;
while(front!=rear)
{
Node *q;
q=Q[++front];
cout<<q->data<<'\t';
if(q->Lchild!=NULL)
Q[++rear]=q->Lchild;
if(q->Rchild!=NULL)
Q[++rear]=q->Rchild;
}
}
void BiTree::print(Node *bt)
{
if(bt!=NULL)
{
if(!bt->Lchild&&!bt->Rchild)
cout<<bt->data<<'\t';
print(bt->Lchild);
print(bt->Rchild);
}
}
void BiTree::print_family(Node *bt)
{
if(bt==NULL) return;
else{
cout<<'\n'<<endl;
cout<<"我是:"<<bt->data<<",";
if(bt->Lchild!=NULL&&bt->Lchild->data!="none")
cout<<"我的左孩子是:"<<bt->Lchild->data<<",";
else
cout<<"我沒有左孩子"<<",";
if(bt->Rchild!=NULL&&bt->Rchild->data!="none")
cout<<"我的右孩子是:"<<bt->Rchild->data<<";"<<endl;
else
cout<<"我沒有右孩子"<<endl;
print_family(bt->Lchild);
print_family(bt->Rchild);
}
}
int main()
{
cout<<"請輸入結點資料,none表示空"<<'\t';
BiTree one;
cout<<"\n"<<"***************"<<endl;
cout<<"前序遍歷:"<<'\t';
one.Preorder();
cout<<'\n'<<"中序遍歷:"<<'\t';
one.Inorder();
cout<<'\n'<<"後序遍歷:"<<'\t';
one.Postorder();
cout<<'\n'<<"層序遍歷:"<<'\t';
one.Leverorder();
cout<<'\n'<<"葉子結點為:"<<'\t';
one.print();
cout<<'\n'<<endl;
cout<<"輸出家人:"<<endl;
one.print_family();
return 0;
}
#include<string>
using namespace std;
struct Node
{
string data;
Node *Lchild,*Rchild;
};
class BiTree
{
public:
BiTree(){root=Create(root);} //建構函式,建立一顆二叉樹
~BiTree(){Release(root);} //解構函式,釋放各節點的儲存空間
void Preorder(){Preorder(root);} //前序
void Inorder(){Inorder(root);} //中序
void Postorder(){Postorder(root);} //後序
void Leverorder(); //層序
void print(){print(root);}
void print_family(){print_family(root);}
private:
Node *root; //指向根的頭
Node *Create(Node *bt); //構造呼叫
void Release(Node *bt); //析構呼叫
void Preorder(Node *bt); //前序
void Inorder(Node *bt); //中序
void Postorder(Node *bt); //後序
void PrintParent(Node *bt); //輸出雙親
void print(Node *bt);
void print_family(Node *bt); //輸出親戚
};
Node*BiTree::Create(Node *bt)
{
string s;
cin>>s;
if(s=="none")
bt=NULL;
else{
bt=new Node;
bt->data=s;
bt->Lchild=Create(bt->Lchild);
bt->Rchild=Create(bt->Rchild);
}
return bt;
}
void BiTree::Release(Node *bt)
{
if(bt!=NULL){
Release(bt->Lchild);
Release(bt->Rchild);
delete bt;
}
}
void BiTree::Preorder(Node *bt)
{
if(bt==NULL) return;
else{
cout<<bt->data<<'\t';
Preorder(bt->Lchild);
Preorder(bt->Rchild);
}
}
void BiTree::Inorder(Node *bt)
{
if(bt==NULL)
return;
else{
Inorder(bt->Lchild);
cout<<bt->data<<'\t';
Inorder(bt->Rchild);
}
}
void BiTree::Postorder(Node *bt)
{
if(bt==NULL)
return;
else{
Postorder(bt->Lchild);
Postorder(bt->Rchild);
cout<<bt->data<<'\t';
}
}
void BiTree::Leverorder()
{
Node *Q[10];
int front,rear;
front=rear=-1;
if(root==NULL)
return;
Q[++rear]=root;
while(front!=rear)
{
Node *q;
q=Q[++front];
cout<<q->data<<'\t';
if(q->Lchild!=NULL)
Q[++rear]=q->Lchild;
if(q->Rchild!=NULL)
Q[++rear]=q->Rchild;
}
}
void BiTree::print(Node *bt)
{
if(bt!=NULL)
{
if(!bt->Lchild&&!bt->Rchild)
cout<<bt->data<<'\t';
print(bt->Lchild);
print(bt->Rchild);
}
}
void BiTree::print_family(Node *bt)
{
if(bt==NULL) return;
else{
cout<<'\n'<<endl;
cout<<"我是:"<<bt->data<<",";
if(bt->Lchild!=NULL&&bt->Lchild->data!="none")
cout<<"我的左孩子是:"<<bt->Lchild->data<<",";
else
cout<<"我沒有左孩子"<<",";
if(bt->Rchild!=NULL&&bt->Rchild->data!="none")
cout<<"我的右孩子是:"<<bt->Rchild->data<<";"<<endl;
else
cout<<"我沒有右孩子"<<endl;
print_family(bt->Lchild);
print_family(bt->Rchild);
}
}
int main()
{
cout<<"請輸入結點資料,none表示空"<<'\t';
BiTree one;
cout<<"\n"<<"***************"<<endl;
cout<<"前序遍歷:"<<'\t';
one.Preorder();
cout<<'\n'<<"中序遍歷:"<<'\t';
one.Inorder();
cout<<'\n'<<"後序遍歷:"<<'\t';
one.Postorder();
cout<<'\n'<<"層序遍歷:"<<'\t';
one.Leverorder();
cout<<'\n'<<"葉子結點為:"<<'\t';
one.print();
cout<<'\n'<<endl;
cout<<"輸出家人:"<<endl;
one.print_family();
return 0;
}