日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

二叉排序树的相关操作

發(fā)布時(shí)間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二叉排序树的相关操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include <IOSTREAM.H>
#include <STDLIB.H>
//二叉樹的生成和釋放
typedef struct Node
{int data;struct Node * pParent;struct Node * pLeftChild;struct Node * pRightChild;
}Node;Node * Create_BTree(int *array,Node* pParent=NULL)//二叉排序樹的創(chuàng)建,按照先序遍歷的方法進(jìn)行構(gòu)造
{static int i=0;if (array[i]==0){return NULL;}Node *temp=(Node *)malloc(sizeof(Node));temp->data=array[i];temp->pParent=pParent;i++;temp->pLeftChild=Create_BTree(array,temp);i++;temp->pRightChild=Create_BTree(array,temp);return temp;
}
void Mid_Order(Node* tree)//二叉排序樹的中序遍歷
{if (tree==NULL){return;}Mid_Order(tree->pLeftChild);cout<<tree->data<<" ";Mid_Order(tree->pRightChild);
}
void Destroy_BTree(Node* tree)//二叉排序樹的是否
{if (tree==NULL){return;}Destroy_BTree(tree->pLeftChild);Destroy_BTree(tree->pRightChild);free(tree);
}
Node* Tree_Search(Node* tree,int x)//二叉排序樹的查找
{Node* temp=tree;while(temp){if(temp->data==x)break;else if(temp->data>x)temp=temp->pLeftChild;elsetemp=temp->pRightChild;}return temp;
}
Node * Tree_Minimum(Node* tree)//二叉排序樹的最小節(jié)點(diǎn)
{while(tree&&tree->pLeftChild){tree=tree->pLeftChild;}return tree;
}
Node * Tree_Maximum(Node* tree)//二叉排序樹的最大節(jié)點(diǎn)
{while(tree&&tree->pRightChild){tree=tree->pRightChild;}return tree;
}
Node * Tree_Successor(Node *p)//返回節(jié)點(diǎn)p的后繼//中序遍歷
{if(p==NULL)return p;else if(p->pRightChild)return Tree_Minimum(p->pRightChild);else{while(p->pParent&&p->pParent->pRightChild==p)p=p->pParent;return p->pParent;}}
Node *Tree_PreDecessor(Node* p)//中序遍歷 求p的前驅(qū)節(jié)點(diǎn)
{if (p==NULL)return p;else if(p->pLeftChild)return Tree_Maximum(p->pLeftChild);else{while(p->pParent&&p->pParent->pLeftChild==p)p=p->pParent;return p->pParent;}
}
void  Tree_Insert(Node* &tree,int x)//給二叉排序樹插入新節(jié)點(diǎn)
{Node * pNodeNew=(Node*)malloc(sizeof(Node));pNodeNew->pLeftChild=NULL;pNodeNew->pRightChild=NULL;pNodeNew->data=x;if(tree==NULL){pNodeNew->pParent=NULL;tree=pNodeNew;return;}Node * pTempParent=NULL,*pTemp=tree;while(pTemp){pTempParent=pTemp;if (pTemp->data<x){pTemp=pTemp->pRightChild;} else{pTemp=pTemp->pLeftChild;}}pNodeNew->pParent=pTempParent;if (pTempParent->data<x){pTempParent->pRightChild=pNodeNew;}elsepTempParent->pLeftChild=pNodeNew;
}
void Tree_Delete(Node* &tree,Node *p)//刪除二叉排序樹中一個(gè)節(jié)點(diǎn)
{if(p->pLeftChild==NULL&&p->pRightChild==NULL)//刪除葉節(jié)點(diǎn)
    {Node *pParent=p->pParent;if (pParent){if (pParent->data<p->data){pParent->pRightChild=NULL;}elsepParent->pLeftChild=NULL;free(p);} else{tree=NULL;free(p);}}else if (p->pLeftChild&&p->pRightChild==NULL||p->pRightChild&&p->pLeftChild==NULL)//只有一個(gè)子樹
    {Node *pParent=p->pParent;if (pParent){if (pParent->data<p->data)//子樹連到父節(jié)點(diǎn)的右孩子
            {if(p->pLeftChild)//哪個(gè)子樹不為空連哪個(gè)
                {pParent->pRightChild=p->pLeftChild;p->pLeftChild->pParent=pParent;}else{pParent->pRightChild=p->pRightChild;p->pRightChild->pParent=pParent;}free(p);}else{if(p->pLeftChild)//哪個(gè)子樹不為空連哪個(gè)
                {pParent->pLeftChild=p->pLeftChild;p->pLeftChild->pParent=pParent;}else{pParent->pLeftChild=p->pRightChild;p->pRightChild->pParent=pParent;}free(p);    }} else{if(p->pLeftChild){p->pLeftChild->pParent=NULL;tree=p->pLeftChild;}else{p->pRightChild->pParent=NULL;tree=p->pRightChild;}free(p);}}else//有兩個(gè)子樹
    {Node *temp=Tree_Maximum(p->pLeftChild);int x=p->data;p->data=temp->data;temp->data=x;Tree_Delete(tree,temp);}
}
void main()
{int array[]={15,6,3,2,0,0,4,0,0,7,0,13,9,0,0,0,18,17,0,0,20,0,0};Node *tree=Create_BTree(array);Tree_Insert(tree,21);Mid_Order(tree);cout<<endl;Node *temp=Tree_Search(tree,7);//查找
    Tree_Delete(tree,temp);Mid_Order(tree);cout<<endl;Destroy_BTree(tree);
}

轉(zhuǎn)載于:https://www.cnblogs.com/GoAhead/archive/2012/10/26/2741508.html

總結(jié)

以上是生活随笔為你收集整理的二叉排序树的相关操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。