二叉排序树的建立和遍历

  1. #include <iostream>  
  2. using namespace std;  
  3. #include <stdio.h>  
  4. #include <stdlib.h>  
  5.   
  6. typedef struct BiTNode  
  7. {  
  8.     int value;  
  9.     struct BiTNode *lchild,*rchild;  
  10. }*BiTree;  
  11.   
  12. bool LT(int a,int b)  //LessThan小于  
  13. {  
  14.     if(a<b)  
  15.         return true;  
  16.     else  
  17.         return false;  
  18. }  
  19. /* 
  20. 在根指针root所指向的二叉排序树中递归地查找其关键字等于data的数据元素,若查找成功,则指针p指向该数据元素结点,并返回true, 
  21. 否则指针p指向查找路径上访问的最后一个结点并返回false指针,指针f指向root的双亲,其初始调用值NULL 
  22. */  
  23. bool SearchBST(BiTree root,int data,BiTree f,BiTree &p)  
  24. {  
  25.     if(!root)  
  26.     {  
  27.         p=f;  
  28.         return false;  
  29.     }  
  30.     else if(data==root->value)  
  31.     {  
  32.         p=root;  
  33.         return true;  
  34.     }  
  35.     else if(data<root->value)  
  36.         return SearchBST(root->lchild,data,root,p);  
  37.     else if(data>root->value)  
  38.         return SearchBST(root->rchild,data,root,p);  
  39. }  
  40.   
  41. //当二叉排序树root中不存在关键字等于data的数据元素时,插入data  
  42. inline void InsertBST(BiTree &root,int data)     //root为传引用指针  
  43. {    
  44.     BiTree p,s;  
  45.     if(!SearchBST(root,data,NULL,p))    //查找不成功  
  46.     {  
  47.         s=(struct BiTNode *)malloc(sizeof(BiTNode));  
  48.         s->value=data;  
  49.         s->lchild=s->rchild=NULL;  
  50.         if(p==NULL)    //二叉排序树为空的时候,被插入结点*s为新的根结点  
  51.             root=s;  
  52.         else if(LT(data,p->value))           //被插结点*s为左孩子  
  53.             p->lchild=s;  
  54.         else           //被插结点*s为右孩子  
  55.             p->rchild=s;  
  56.     }  
  57.     return ;  
  58. }  
  59. void PreOrderTraverse(BiTree root)    //先序遍历  
  60. {  
  61.     if(root)  
  62.     {  
  63.         printf("%d ",root->value);  
  64.         PreOrderTraverse(root->lchild);  
  65.         PreOrderTraverse(root->rchild);  
  66.     }  
  67. }  
  68. void InOrderTraverse(BiTree root)    //中序遍历  
  69. {  
  70.     if(root)  
  71.     {  
  72.         InOrderTraverse(root->lchild);  
  73.         printf("%d ",root->value);  
  74.         InOrderTraverse(root->rchild);  
  75.     }  
  76. }  
  77. void PostOrderTraverse(BiTree root)    //后序遍历  
  78. {  
  79.     if(root)  
  80.     {  
  81.         PostOrderTraverse(root->lchild);  
  82.         PostOrderTraverse(root->rchild);  
  83.         printf("%d ",root->value);  
  84.     }  
  85. }  
  86. void DeleteBST(BiTree root)  
  87. {  
  88.     if(root)  
  89.     {  
  90.         DeleteBST(root->lchild);    //释放左子树  
  91.         DeleteBST(root->rchild);    //释放右子树  
  92.         free(root);        //释放根结点  
  93.     }  
  94. }  
  95. int main(void)  
  96. {  
  97.     int i,a[101],n;  
  98.     BiTree root;  
  99.     while(scanf("%d",&n)!=EOF)  
  100.     {  
  101.         root=NULL;  
  102.         for(i=1;i<=n;i++)  
  103.         {  
  104.             scanf("%d",&a[i]);  
  105.             InsertBST(root,a[i]);  
  106.         }  
  107.         PreOrderTraverse(root);  
  108.         printf("\n");  
  109.         InOrderTraverse(root);  
  110.         printf("\n");  
  111.         PostOrderTraverse(root);  
  112.         printf("\n");  
  113.         DeleteBST(root);  
  114.     }  
  115.     return 0;  
  116. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言实现二叉排序树的基本操作的示例代码: 1. 二叉排序树建立: ```c #include <stdio.h> #include <stdlib.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; struct TreeNode* insert(struct TreeNode* root, int val) { if (root == NULL) { struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode)); newNode->val = val; newNode->left = NULL; newNode->right = NULL; return newNode; } if (val < root->val) { root->left = insert(root->left, val); } else if (val > root->val) { root->right = insert(root->right, val); } return root; } ``` 2. 二叉排序树的删除: ```c struct TreeNode* delete(struct TreeNode* root, int val) { if (root == NULL) { return NULL; } if (val < root->val) { root->left = delete(root->left, val); } else if (val > root->val) { root->right = delete(root->right, val); } else { if (root->left == NULL) { struct TreeNode* temp = root->right; free(root); return temp; } else if (root->right == NULL) { struct TreeNode* temp = root->left; free(root); return temp; } struct TreeNode* temp = findMin(root->right); root->val = temp->val; root->right = delete(root->right, temp->val); } return root; } struct TreeNode* findMin(struct TreeNode* node) { while (node->left != NULL) { node = node->left; } return node; } ``` 3. 二叉排序树的中序遍历结果: ```c void inorderTraversal(struct TreeNode* root) { if (root == NULL) { return; } inorderTraversal(root->left); printf("%d ", root->val); inorderTraversal(root->right); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值