二叉排序树的相关操作
生活随笔
收集整理的這篇文章主要介紹了
二叉排序树的相关操作
小編覺得挺不錯(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 风停了花谢了是什么歌?
- 下一篇: 星际战甲国服英雄价格大概都是多少人民币