复习记录,高手无视,关于 二叉搜索树的一些基本操作。
- //Code by Pnig0s1992
- //Date:2012,3,28
- #include <stdio.h>
- #include <stdlib.h>
- typedef int Element_type;
- typedef struct TreeNode * ptrTreeNode;
- typedef struct TreeNode * ADTTree;
- struct TreeNode
- {
- Element_type element;
- ptrTreeNode pLeftChild;
- ptrTreeNode pRightChild;
- };
- void MakeEmpty(ADTTree myTree);//将ADT树置空
- ptrTreeNode Find(Element_type x,ADTTree myTree);//查找指定元素的结点并返回
- ptrTreeNode FindMin(ADTTree myTree);//查找ADT树中的最小值
- ptrTreeNode FindMax(ADTTree myTree);//查找ADT树中的最大值
- ptrTreeNode Insert(Element_type x,ADTTree myTree);//向ADT树中插入结点
- ptrTreeNode Delete(Element_type x,ADTTree myTree);//ADT树中删除指定结点
- ADTTree CreateTree(Element_type x);//构造一个新ADT树并返回根节点指针
- int main(int argc,char ** argv)
- {
- Element_type el = 6;
- ADTTree newTree = CreateTree(el);
- newTree = Insert(2,newTree);
- newTree = Insert(8,newTree);
- newTree = Insert(1,newTree);
- newTree = Insert(4,newTree);
- newTree = Insert(3,newTree);
- newTree = Delete(6,newTree);
- return 0;
- }
- //构建ADT树
- ADTTree CreateTree(Element_type x)
- {
- ADTTree newTree = (ptrTreeNode)malloc(sizeof(TreeNode));
- newTree->element = x;
- newTree->pLeftChild = newTree->pRightChild = NULL;
- return newTree;
- }
- //向ADT树中插入一个结点
- ptrTreeNode Insert(Element_type x,ADTTree myTree)
- {
- if(myTree == NULL)
- {
- myTree =(ptrTreeNode) malloc(sizeof(TreeNode));
- if(myTree == NULL)
- {
- printf("\nOut of space.");
- }
- else
- {
- myTree->element = x;
- myTree->pLeftChild = myTree->pRightChild = NULL;
- }
- }else if (x < myTree->element)
- {
- myTree->pLeftChild = Insert(x,myTree->pLeftChild);
- }else if (x > myTree->element)
- {
- myTree->pRightChild = Insert(x,myTree->pRightChild);
- }
- return myTree;
- }
- //找到ADT树最小结点
- ptrTreeNode FindMin(ADTTree myTree)
- {
- if(myTree == NULL)
- {
- printf("\nThe tree is empty.");
- }else if (myTree->pLeftChild == NULL)
- {
- return myTree;
- }else
- {
- return FindMin(myTree->pLeftChild);
- }
- }
- //找到ADT树最大结点
- ptrTreeNode FindMax(ADTTree myTree)
- {
- if(myTree == NULL)
- {
- printf("\nThe tree is empty.");
- }else if (myTree->pRightChild == NULL)
- {
- return myTree;
- }else
- {
- return FindMax(myTree->pRightChild);
- }
- }
- //删除指定ADT树结点
- ptrTreeNode Delete(Element_type x,ADTTree myTree)
- {
- ptrTreeNode TempCell;
- if(myTree == NULL)
- {
- printf("\nThe tree is empty.");
- }else if (x < myTree->element)
- {
- myTree->pLeftChild = Delete(x,myTree->pLeftChild);
- }else if (x > myTree->element)
- {
- myTree->pRightChild = Delete(x,myTree->pRightChild);
- }else if (myTree->pLeftChild && myTree->pRightChild)
- {
- TempCell = FindMin(myTree->pRightChild);
- myTree->element = TempCell->element;
- myTree->pRightChild = Delete(myTree->element,myTree->pRightChild);
- }else
- {
- TempCell = myTree;
- if(myTree->pLeftChild == NULL)
- {
- myTree = myTree->pRightChild;
- }else if (myTree->pRightChild == NULL)
- {
- myTree = myTree->pLeftChild;
- }
- free(TempCell);
- }
- return myTree;
- }
- ptrTreeNode Find(Element_type x,ADTTree myTree)
- {
- if(myTree == NULL)
- {
- printf("\nelement not be found.");
- }else if (x < myTree->element)
- {
- return Find(x,myTree->pLeftChild);
- }else if(x > myTree->element)
- {
- return Find(x,myTree->pRightChild);
- }else
- return myTree;
- }
- void MakeEmpty(ADTTree myTree)
- {
- if(myTree != NULL)
- {
- MakeEmpty(myTree->pLeftChild);
- MakeEmpty(myTree->pRightChild);
- free(myTree);
- }
- return;
- }
转载于:https://blog.51cto.com/pnig0s1992/819473