1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#include "LinkedBinaryTree.h"
#include <algorithm>
void LinkedBinaryTree::inOrder(Node * node)
{
if (node == NULL)
return;
inOrder(node->left);
cout << node->word << " " << avlNum(node) << " : " ;
inOrder(node->right);
}
void LinkedBinaryTree::rightRotate(Node* node)
{
Node* temp;
temp = node->left;
node->left = temp->right;
temp->right = node;
temp->parent = node->parent;
node = temp;
if (temp->parent == NULL) {
_root = temp;
}
fixHeight(node);
fixHeight(node->right);
fixHeight(node->left);
}
void LinkedBinaryTree::leftRotate(Node * node)
{
Node* temp;
temp = node->right;
node->right = temp->left;
temp->left = node;
temp->parent = node->parent;
node = temp;
if (temp->parent == NULL) {
_root = temp;
}
fixHeight(node);
fixHeight(node->right);
fixHeight(node->left);
}
void LinkedBinaryTree::rightLeftRotate(Node * node)
{
rightRotate(node->left);
leftRotate(node);
}
void LinkedBinaryTree::leftRightRotate(Node * node)
{
leftRotate(node->right);
rightRotate(node);
}
int LinkedBinaryTree::height(Node * node)
{
int h = 0;
if (node != NULL) {
h = node->height;
}
return h;
}
int LinkedBinaryTree::bfactor(Node * node)
{
return height(node->right) - height(node->left);
}
void LinkedBinaryTree::fixHeight(Node * node)
{
int hl = height(node->left);
int hr = height(node->right);
node->height = (hl > hr ? hl : hr) + 1;
}
int LinkedBinaryTree::avlNum(Node * node)
{
int leftH = height(node->left);
int rightH = height(node->right);
int avlNum = rightH - leftH;
return avlNum;
}
LinkedBinaryTree::LinkedBinaryTree()
{
_root = NULL;
}
LinkedBinaryTree::~LinkedBinaryTree()
{
}
void LinkedBinaryTree::insertNode(Node* node, string word)
{
if (word < node->word) {
if (node->left != NULL)
insertNode(node->left, word);
else {
node->left = new Node(word, NULL, NULL, node);
}
}
else if (word > node->word)
{
if (node->right != NULL)
insertNode(node->right, word);
else {
node->right = new Node(word, NULL, NULL, node);
}
}
else if (word == node->word) {
node->wordCount++;
}
balance(node);
}
void LinkedBinaryTree::insert(string word) {
if (_root == NULL) {
_root = new Node(word, NULL, NULL, NULL);
n++;
}
else {
insertNode(_root, word);
n++;
}
}
void LinkedBinaryTree::display(Node* ptr, int level)
{
int i;
if (ptr != NULL)
{
display(ptr->right, level + 1);
printf("\n");
if (ptr == _root)
cout << "Root -> ";
for (i = 0; i < level && ptr != _root; i++)
cout << " ";
cout << ptr->word;
display(ptr->left, level + 1);
}
}
LinkedBinaryTree::Node * LinkedBinaryTree::root()
{
return _root;
}
void LinkedBinaryTree::balance(Node* node)
{
fixHeight(node);
if (bfactor(node) == 2) {
if (bfactor(node->right) < 0)
rightRotate(node->right);
else
leftRotate(node);
}
if (bfactor(node) == -2) {
if (bfactor(node->left) > 0)
leftRotate(node->left);
else
rightRotate(node);
}
}
|