//二叉查找树的链式表示及算法实现
//by zoe
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
using namespace std;
typedef struct node* position;
typedef int DataType;
struct node{
DataType data;
position lchild;
position rchild;
position parent;
};
typedef struct node* BiTree;
void Print_Tree(BiTree);
void Insert_node(BiTree,DataType);
position Find_Min(BiTree);
position Find_Max(BiTree);
position Find_Value(BiTree,DataType);
DataType Delete_node(position);
static int is_leaf(position);
static int is_root(position);
static DataType delete_leaf(position);
static void Insert_node_to_unempty_tree(BiTree,position);
//遍历算法
void preOrder1(BiTree);
void preOrder2(BiTree);//非递归实现遍历
void inOrder1(BiTree);
void inOrder2(BiTree);
void postOrder1(BiTree);
void postOrder2(BiTree);
int main(){
BiTree tree;
DataType data;
position p;
//tree=NULL;
Insert_node(tree,28);
Insert_node(tree,13);
Insert_node(tree,2);
Insert_node(tree,10);
Insert_node(tree,14);
Insert_node(tree,5);
Insert_node(tree,20);
Print_Tree(tree);
printf("\n");
p=Find_Value(tree,14);
if(p!=NULL){
Delete_node(p);
printf("after delete: ");
Print_Tree(tree);
}
return 0;
}
void Print_Tree(BiTree tree){
if(tree!=NULL){
Print_Tree(tree->lchild);
printf("%d\n",tree->data);
Print_Tree(tree->rchild);
}
else return;
}
void Insert_node(BiTree tree,DataType x){
position np;
np=(position)malloc(sizeof(struct node));//先挖坑,再插入
np->data=x;
np->parent=NULL;
np->lchild=NULL;
np->rchild=NULL;
if(tree==NULL){
tree=np;
}
else{
Insert_node_to_unempty_tree(tree,np);
}
}
position Find_Min(BiTree tree){
position np;
np=tree;
if(np==NULL) return np;
while (np->lchild!=NULL) {
np=np->lchild;
}
return np;
}
position Find_Max(BiTree tree){
position np;
np=tree;
if(np==NULL)return np;
while(np->rchild!=NULL){
np=np->rchild;
}
return np;
}
position Find_Value(BiTree tree,DataType x){
position np;
tree=np;
if(np==NULL) return np;
if(np->data==x) {return np;}
/**
/*迭代非递归
while(np->lchild!=NULL||np->rchild!=NULL){
if(x<np->data)
Find_Value(np->lchild,x);
else
Find_Value(np->rchild,x);
}
*/
else if(x<np->data){
return Find_Value(np->lchild,x);
}
else{
return Find_Value(np->rchild,x);
}
}
DataType Delete_node(position np){
position replace;
DataType temp;
//如果该结点为叶子结点,则直接删除
if(is_leaf(np)){
return delete_leaf(np);
}
//如果该结点有儿子结点,则要进行替换
else{
replace=(np->lchild!=NULL)? Find_Max(np->lchild):Find_Min(np->rchild);
temp=np->data;
np->data=Delete_node(replace);
return temp;
}
}
static int is_leaf(position np){
return (np->lchild==NULL&&np->rchild==NULL);
}
static int is_root(position np){
return (np->parent==NULL);
}
static DataType delete_leaf(position np){
DataType elem;
position parent;
elem=np->data;
parent=np->parent;
if(!is_root(np)){
if(parent->lchild==np){
parent->lchild==NULL;
}
else{
parent->rchild==NULL;
}
}
free(np);
return elem;
}
static void Insert_node_to_nonempty_tree(BiTree tree,position np){
if(np->data<tree->data){
if(tree->lchild==NULL){
tree->lchild=np;
np->parent=tree;
}
else{
Insert_node_to_nonempty_tree(tree->lchild,np);
}
}
else if(np->data>tree->data){
if(tree->rchild==NULL){
tree->rchild=np;
np->parent=tree;
}
else{
Insert_node_to_nonempty_tree(tree->rchild,np);
}
}
}
//各种遍历算法的实现
//递归遍历
void preOrder1(BiTree T){
if(T!=NULL){
cout<<T->data<<" "<<endl;
preOrder1(T->lchild);
preOrder1(T->rchild);
}
}
void inOrder1(BiTree T){
if(T!=NULL){
inOrder1(T->lchild);
cout<<T->data<<" "<<endl;
inOrder1(T->rchild);
}
}
void postOrder1(BiTree T){
if(T!=NULL){
postOrder1(T->lchild);
postOrder1(T->rchild);
cout<<T->data<<" "<<endl;
}
}
//非递归
void preOrder2(BiTree T){
stack<BiTree> S;
position p=T;
while(p!=NULL||!S.empty()){
while(p!=NULL){
cout << p->data << endl;
S.push(p);
p=p->lchild;
}
if(!S.empty()){
p=S.top();
S.pop();
p=p->rchild;
}
}
}
void inOrder2(BiTree T){
stack<BiTree> S;
position p=T;
while(p!=NULL||!S.empty()){
while(p!=NULL){
S.push(p);
p->lchild;
}
if(!S.empty()){
p=S.top();
cout<<p->data<<endl;
S.pop();
p=p->rchild;
}
}
}
void postOrder2(BiTree T){
stack<BiTree> s;
position cur; //当前结点
position pre=NULL; //前一次访问的结点
s.push(T);
while(!s.empty())
{
cur=s.top();
if((cur->lchild==NULL&&cur->rchild==NULL)||
(pre!=NULL&&(pre==cur->lchild||pre==cur->rchild)))
{
cout<<cur->data<<" "; //如果当前结点没有孩子结点或者孩子节点都已被访问过
s.pop();
pre=cur;
}
else
{
if(cur->rchild!=NULL)
s.push(cur->rchild);
if(cur->lchild!=NULL)
s.push(cur->lchild);
}
}
}
二叉树的各种运算及遍历实现
最新推荐文章于 2021-06-13 12:26:54 发布