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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
#pragma once
#include "StdAfx.h"
struct TreeNode;
template <class ItemType>
class TreeType
{
public:
TreeType(void);
~TreeType(void);
void InsertItem(ItemType item);
void Insert (TreeNode<ItemType>*& tree, ItemType item, bool & taller);
void RightBalance (TreeNode<ItemType> *& tree, bool & taller);
void LeftBalance (TreeNode<ItemType> *& tree, bool & taller);
void RotateLeft (TreeNode<ItemType> * & tree);
void RotateRight (TreeNode<ItemType> * & tree);
//private:
//TreeNode<ItemType>*& tree;
};
template <class ItemType>
void TreeType<ItemType>::InsertItem(ItemType item)
// Calls recursive function Insert to insert item into tree.
{
bool taller;
Insert (root, item, taller);
}
template <class ItemType>
void TreeType<ItemType>::Insert (TreeNode<ItemType>*& tree, ItemType item, bool & taller)
// Inserts item into tree.
// Post: item is in tree; search property is maintained.
{
if (tree == NULL)
{ // Insertion place found.
tree = new TreeNode<ItemType>;
tree->left = NULL;
tree->right = NULL;
tree->info = item;
tree->bf = EH;
taller = true;
}
else if ( item == tree->info)
{
cerr << "Duplicate key is not allowed in AVL tree." << endl;
}
else if (item < tree->info)
{
Insert (tree->left, item, taller);
if (taller)
switch (tree->bf)
{
case LH: LeftBalance (tree, taller);
break;
case EH:
tree->bf = LH;
break;
case RH:
tree->bf = EH;
taller = false;
break;
}
}
else if (item > tree->info)
{
Insert (tree->right, item, taller);
if (taller)
switch (tree->bf)
{
case RH:
RightBalance (tree, taller);
break;
case EH:
tree->bf = RH;
break;
case LH:
tree->bf = EH; taller = false;
break;
}
}
template <class ItemType>
void TreeType<ItemType>::RightBalance (TreeNode<ItemType> *& tree, bool & taller)
{
TreeNode<ItemType> * rs = tree->right;
TreeNode<ItemType> * ls;
switch (rs->bf)
{
case RH: tree->bf = rs->bf = EH;
RotateLeft(tree);
taller = false;
break;
case EH: cerr << "Tree already balanced " << endl;
break;
case LH: ls = rs->left;
switch (ls->bf)
{
case RH:
tree->bf = LH;
rs->bf = EH;
break;
case EH:
tree->bf = rs->bf = EH;
break;
case LH:
tree->bf = EH;
rs->bf = RH;
break;
}
ls->bf = EH;
RotateRight(tree->right);
RotateLeft(tree);
taller = false;
}
}
template <class ItemType>
void TreeType<ItemType>::LeftBalance (TreeNode<ItemType> *& tree, bool & taller)
{
TreeNode<ItemType> * ls = tree->left;
TreeNode<ItemType> * rs;
switch (ls->bf)
{
case LH:
tree->bf = ls->bf = EH;
RotateRight(tree);
taller = false;
break;
case EH:
cerr << "Tree already balanced " << endl;
break;
case RH:
rs = ls->left;
switch (rs->bf)
{
case LH:
tree->bf = RH;
ls->bf = EH;
break;
case EH:
tree->bf = ls->bf = EH;
break;
case RH:
tree->bf = EH;
ls->bf = LH;
break;
}
rs->bf = EH;
RotateLeft(tree->left);
RotateRight(tree);
taller = false;
}
}
template <class ItemType>
void TreeType<ItemType>::RotateLeft (TreeNode<ItemType> * & tree)
{
TreeNode<ItemType> * rs;
if (tree == NULL)
cerr << "Cannot rotate an empty tree in RotateLeft" << endl;
else if (tree->right == NULL)
cerr << "Cannot make an empty subtree the root in RotateLeft" << endl;
else
{
rs = tree->right;
tree->right = rs->left;
rs->left = tree;
tree = rs;
}
}
template <class ItemType>
void TreeType<ItemType> :: RotateRight (TreeNode<ItemType> * & tree)
{
TreeNode<ItemType> * ls;
if (tree == NULL)
cerr << "Cannot rotate an empty tree in RotateRight" << endl;
else if (tree->left == NULL)
cerr << "Cannot make an empty subtree the root in RotateRight" << endl;
else
{
ls = tree->left;
tree->left = ls->right;
ls->right = tree;
tree = ls;
}
}
}
|