Chinaunix首页 | 论坛 | 博客
  • 博客访问: 41851
  • 博文数量: 9
  • 博客积分: 1557
  • 博客等级: 上尉
  • 技术积分: 124
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-06 17:06
文章分类
文章存档

2011年(2)

2010年(7)

我的朋友

分类: C/C++

2011-08-21 20:08:12

题目:
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
要求不能创建任何新的结点,只调整指针的指向。
10
/ \
6 14
/ \ / \
4 8 12 16
转换成双向链表
4=6=8=10=12=14=16。
首先我们定义的二元查找树 节点的数据结构如下:
struct BSTreeNode
{
int m_nValue; // value of node
BSTreeNode *m_pLeft; // left child of node
BSTreeNode *m_pRight; // right child of node
};
 
思路: 第一种方法,利用中序遍历思想,由于中序遍历出来的结果正好有序,因此按照中序遍历输出的结果顺序串成有序链表。
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct BSTreeNode
  4. {
  5.     int m_nValue;
  6.     BSTreeNode *m_pleft;
  7.     BSTreeNode *m_pright;
  8. }BSTreeNode;
  9. BSTreeNode *current_node, *first_node;

  10. void insertNode(BSTreeNode **proot, BSTreeNode **pNode)
  11. {
  12.     BSTreeNode *node, *pnode, *pre_node;

  13.     if(*proot == NULL)
  14.     {
  15.         *proot = *pNode;
  16.         return;
  17.     }

  18.     pnode = *pNode;
  19.     node = *proot;
  20.     pre_node = NULL;

  21.     while(node)
  22.     {
  23.         pre_node = node;
  24.         if(node->m_nValue == pnode->m_nValue)
  25.         {
  26.             free(pnode);
  27.             return;
  28.         }
  29.         if(node->m_nValue > pnode->m_nValue)
  30.             node = node->m_pleft;
  31.         else
  32.             node = node->m_pright;
  33.     }
  34.     if(pre_node->m_nValue > pnode->m_nValue)
  35.         pre_node->m_pleft = pnode;
  36.     else
  37.         pre_node->m_pright = pnode;

  38. }

  39. void print(BSTreeNode *node)
  40. {
  41.     if(node)
  42.     {
  43.         print(node->m_pleft);
  44.         if(current_node == NULL)
  45.         {
  46.             current_node = node;
  47.             first_node = node;
  48.         }
  49.         else
  50.         {
  51.             current_node->m_pright = node;
  52.             node->m_pleft = current_node;
  53.         }
  54.         current_node = node;
  55.         print(node->m_pright);
  56.     }
  57. }


  58. int main()
  59. {
  60.     int i, j;
  61.     int n, m;
  62.     BSTreeNode *root, *newNode;

  63.     while(scanf("%d", &n) != EOF)
  64.     {
  65.         root = NULL;
  66.         current_node = NULL;
  67.         first_node = NULL;
  68.         for(i = 0; i < n; i )
  69.         {
  70.             scanf("%d", &m);

  71.             newNode = (BSTreeNode *)malloc(sizeof(BSTreeNode));
  72.             newNode->m_nValue = m;
  73.             newNode->m_pleft = NULL;
  74.             newNode->m_pright = NULL;
  75.             insertNode(&root, &newNode);
  76.         }
  77.         print(root);
  78.         newNode = first_node;
  79.         while(newNode)
  80.         {
  81.             printf("%d ", newNode->m_nValue);
  82.             newNode = newNode->m_pright;
  83.         }
  84.     }
  85. }
    第二种方法: 利用分治思想,先处理左子树,后处理右子树,最后利用根结点将处理完的有序左子树与有序右子树连接起来,实现将二元查找树转换成有序双链表的目的。
  1. #define LEFT -1
  2. #define RIGHT 1

  3. BSTreeNode* change(BSTreeNode *node, int dir)
  4. {
  5.     BSTreeNode *ptmp = NULL;
  6.     BSTreeNode *pleft = NULL;
  7.     BSTreeNode *pright = NULL;
  8.     if(node->m_pleft)
  9.         pleft = change(node->m_pleft, LEFT);
  10.     if(node->m_pright)
  11.         pright = change(node->m_pright, RIGHT);
  12.     if(pleft)
  13.     {
  14.         node->m_pleft = pleft;
  15.         pleft->m_pright = node;
  16.     }
  17.     if(pright)
  18.     {
  19.         node->m_pright = pright;
  20.         pleft->m_pleft = node;
  21.     }
  22.     ptmp = node;
  23.     if(dir == LEFT)
  24.     {
  25.         while(ptmp->m_pright)
  26.             ptmp = ptmp->m_pright;
  27.     }
  28.     if(dir == RIGHT)
  29.     {
  30.         while(ptmp->m_pleft)
  31.             ptmp = ptmp->m_pleft;
  32.     }
  33.     return ptmp;
  34. }
阅读(1461) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:设计包含min函数的栈

给主人留下些什么吧!~~