题目要求为实现左旋,题目很简单,不过第一次写左旋,当练手啦~
由题意得需要有一个BinarytNode* 型的数据temp用于进行交换。题目要求将根节点的左节点旋转为根节点,然后将根节点变成左节点。
#include<iostream>
using namespace std;
typedef int Comparable;
struct BinaryNode
{
Comparable data;
BinaryNode *left;
BinaryNode *right;
BinaryNode( const Comparable & theElement, BinaryNode *lt = NULL, BinaryNode *rt=NULL )
: data( theElement ), left( lt ), right( rt ) { }
};
void rotateL(BinaryNode* &root) {
//Left rotation, assuming root and the right subtree of root are not empty.
BinaryNode* temp;
temp = root;
root = root->right;
temp->right = root->left;
root->left = temp;
}