二叉树的左旋右旋

 

这个其实比较简单,看图说话最简单:

 

上图是右旋,相信右旋大家都知道怎么回事了,代码如下:

 1 #include <stdio.h>
 2 
 3 typedef struct node
 4 {
 5     int m_nValue;
 6     struct node *m_pLeft;
 7     struct node *m_pRight;
 8 }TreeNode;
 9 
10 void R_Rotate(TreeNode **pRoot)
11 {
12     if((*pRoot) == NULL)
13         return;
14 
15     TreeNode *pNode = (*pRoot)->m_pLeft;
16     (*pRoot)->m_pLeft = pNode->m_pRight;
17     pNode->m_pRight = *pRoot;
18     (*pRoot) = pNode;
19 }
20 
21 void L_Rotate(TreeNode **pRoot)
22 {
23     if(*pRoot == NULL)
24         return;
25 
26     TreeNode *pNode = (*pRoot)->m_pRight;
27     (*pRoot)->m_pRight = pNode->m_pLeft;
28     pNode->m_pLeft = (*pRoot);
29     (*pRoot) = pNode;
30 }

 

posted @ 2013-04-15 17:23  ITeed  阅读(339)  评论(0)    收藏  举报