-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexedAvlBinaryTree.h
107 lines (94 loc) · 2.7 KB
/
IndexedAvlBinaryTree.h
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
// Copyright (C) Kamaledin Ghiasi-Shirazi, Ferdowsi Univerity of Mashhad, 2016 (1395 Hijri Shamsi)
//
// Author: Kamaledin Ghiasi-Shirazi
#pragma once
#include <string>
#include "avlBinaryIndexedTreeNode.h"
using namespace std;
template <class T, class IBTN>
class IndexedAvlBinaryTree : public AvlBinaryTree<T, IBTN>
{
public:
IndexedAvlBinaryTree()
{
//add index to dummy node !!
}
virtual void insertRootNode(T data)
{
BinaryTree::insertRootNode(data);
}
// error if a left child already exists.
virtual void insertLeftChild(const BinaryTreeNode& parentNode, T data)
{
//AvlBinaryTree::insertLeftChild(parentNode, data);
BinaryTree::insertLeftChild(parentNode, data);
Updatemleftsize(getActualNode(parentNode)->mLeftChild, 1);
balanceAll(parentNode.mActualNode);
}
// error if a right child already exists.
virtual void insertRightChild(const BinaryTreeNode& parentNode, T data)
{
//AvlBinaryTree::insertRightChild(parentNode, data);
BinaryTree::insertRightChild(parentNode, data);
Updatemleftsize(getActualNode(parentNode)->mRightChild, 1);
balanceAll(parentNode.mActualNode);
}
// Only leaf nodes and nodes with degree 1 can be deleted.
// If a degree-1 node is deleted, it is replaced by its subtree.
virtual void deleteNode(const BinaryTreeNode& node)
{
Updatemleftsize(getActualNode(node), -1);
AvlBinaryTree::deleteNode(node);
}
virtual void deleteNode2degree(const BinaryTreeNode& node)
{
BinaryTreeNode& delnode = node.getLeftChild();
while (delnode.hasRightChild()) {
delnode = delnode.getRightChild();
}
*(getActualNode(node)->mData) = *(getActualNode(delnode)->mData);
//Updatemleftsize(getActualNode(delnode), -1);
//BinaryTree::deleteNode(delnode);
deleteNode(delnode);
}
virtual int getLeftSize(const BinaryTreeNode& node)
{
return getActualNode(node)->mLeftSize;
}
//int calcLeftSize(IBTN* node) {
// int s = 0;
// IBTN* tmp = node->mLeftChild;
// if (tmp) {
// s += tmp->mLeftSize + 1;
// if (tmp->mRightChild)
// while (tmp = tmp->mRightChild) {
// s += tmp->mLeftSize + 1;
// }
// }
// return s;
//}
virtual IBTN* rr_rotation(IBTN *parent) {
IBTN* tmp = AvlBinaryTree::rr_rotation(parent);
tmp->mLeftSize += tmp->mLeftChild->mLeftSize + 1;
return tmp;
}
virtual IBTN* ll_rotation(IBTN *parent) {
IBTN* tmp = AvlBinaryTree::ll_rotation(parent);
parent->mLeftSize = parent->mLeftSize - tmp->mLeftSize -1;
return tmp;
}
private:
bool isLeftChildOfParent(IBTN* node) {
return (node->mParent->mLeftChild == node);
}
void Updatemleftsize(IBTN* node, int n) {
if (!node)
return;
while (node != mInEnd) {
if (isLeftChildOfParent(node)) {
node->mParent->mLeftSize += n;
}
node = node->mParent;
}
}
};