void func(Node*&node){
(...) //it just prints the node's properties.
}
Node * root =&Node(1); //e.g. root = 0x500000;
func(root); //before function, root still has value of 0x500000; after function, root becomes invalid(points to a different block of memory), but it shouldn't since there isn't anything in the function that could cause that.
void func(Node*&node)
void func(Node &node){
// Access the node as if it were a local variable.
}
void func(Node *node){
// Access the pointer
}
void func(Node * node){
//this is retrieving the address from the node, not the pointer itself.
}
void func_1(int * a){ //this copies the address of pointer, not the pointer itself.
a = NULL;//and because of that, this wouldn't do anything.
}
void func_2(int &a){
a = NULL; //this one would work, but not for what I want, in this case it would be destroying the pointer.
}
void func_3(int *& a){
a = NULL; //this one would work.
}
(...)
int main(void){
int b = 5;
int * a = &b;
cout << *a << endl; //prints out 5;
void func_1(a);
cout << *a << endl; //prints out 5;
void func_2(*a);
cout << *a << endl; //prints out 0;
void func_3(a);
cout << *a << endl; //crash: fails to read memory because pointer was successfully destroyed.
}
void func_1( int * const & a)
{
*a = 420;
a = 0; // invalid, a is const. will not compile.
}
#include<iostream>
using namespace std;
struct NodeTree{
public:
int c_value;
int c_count;
NodeTree * Parent; //parent node to current node. (Parent node is NULL if current node is root)
NodeTree * Left; //value is lower than current node's card value.
NodeTree * Right; //value is higher than current node's card value.
NodeTree(NodeTree * _Parent, int cardvalue){
Parent = _Parent;
Left = NULL;
Right = NULL;
c_value = cardvalue;
c_count = 1;
}
NodeTree(NodeTree &node){ //copy constructor.
c_value = node.c_value;
c_count = node.c_count;
Parent = node.Parent;
Left = node.Left;
Right = node.Right;
}
NodeTree(NodeTree* node){ //copy constructor (from pointer).
c_value = node->c_value;
c_count = node->c_count;
Parent = node->Parent;
Left = node->Left;
Right = node->Right;
}
};
class SearchTree{
public:
SearchTree(){ root = NULL; }
SearchTree(int cardvalue){ root = &NodeTree(NULL, cardvalue); } //root is parent of all nodes, meaning it doesn't have a parent.
void Insert(int cardvalue);
void Remove(int cardvalue);
int GetCardValueByRank(int rank);
private:
enum Rotation{ //Indicates the type of rotation.
Left,
Right
};
NodeTree * root = NULL; //root node.
void Insert(NodeTree *&node, NodeTree *parent, int cardvalue);
void Remove(NodeTree *&node, NodeTree *parent, int cardvalue);
void DestroyNode(NodeTree *&node);
void BalanceNode(NodeTree *&node);
void RotateNode(NodeTree *&node, Rotation r);
int GetCardValueByRank(NodeTree * node, int rank);
int GetBalanceFactor(NodeTree * node);
int GetHeight(NodeTree * node);
NodeTree *& FindRightMostNode(NodeTree* &node);
};
#include "BinTree.h"
void SearchTree::Insert(int cardvalue){
Insert(root, NULL, cardvalue);
}
void SearchTree::Remove(int cardvalue){
Remove(root, NULL, cardvalue);
}
int SearchTree::GetCardValueByRank(int rank){
return GetCardValueByRank(root, rank);
}
void SearchTree::Insert(NodeTree *&node,NodeTree *parent, int cardvalue){
if (node == NULL){
node = &NodeTree(parent, cardvalue);
BalanceNode(parent);
}
else{
if (node->c_value > cardvalue){ //card value is lower than current node's card.
Insert(node->Left, node, cardvalue);
}
else if (node->c_value < cardvalue){ //card value is higher than current node's card.
Insert(node->Right, node, cardvalue);
}
else{ //card value is equal to current node's card. so this is the node we want to increment.
node->c_count++;
}
}
}
void SearchTree::Remove(NodeTree *&node, NodeTree *parent, int cardvalue){
if (node == NULL){
throw new exception("Node wasn't found.");
}
else{
if (node->c_value > cardvalue){ //card value is lower than current node's card.
Remove(node->Left, node, cardvalue);
}
else if (node->c_value < cardvalue){ //card value is higher than current node's card.
Remove(node->Right, node, cardvalue);
}
else{ //card value is equal to current node's card. so this is the node we want to increment.
if (node->c_count == 1){
DestroyNode(node);
BalanceNode(parent);
}
else{
node->c_count--;
}
}
}
}
void SearchTree::DestroyNode(NodeTree *&node){
if (node != NULL){
if (node->Left != NULL){//parent to at least 1 node.
if (node->Right != NULL){ //is parent to 2 nodes.
NodeTree *& rightmost = FindRightMostNode(node->Left);
node->c_value = rightmost->c_value;
node->c_count = rightmost->c_count;
DestroyNode(rightmost);
}
else{ //is parent to 1 node.
NodeTree * parent = node->Parent; //stores address of parent node.
node = node->Left; //replaces current node with left child.
node->Parent = parent; //changes parent to the real parent.
}
}
else{//parent to, at best, 1 node.
if (node->Right != NULL){ //is parent to 1 node.
NodeTree * parent = node->Parent; //stores address of parent node.
node = node->Left; //replaces current node with left child.
node->Parent = parent; //changes parent to the real parent.
}
else{ //node is a leaf.
node = NULL;
}
}
}
}
void SearchTree::BalanceNode(NodeTree *&node){
if (node != NULL){
if (GetBalanceFactor(node) > 1){
RotateNode(node, Right);
}
else if (GetBalanceFactor(node) < -1){
RotateNode(node, Left);
}
BalanceNode(node->Parent);
}
}
void SearchTree::RotateNode(NodeTree *&node, Rotation r){
NodeTree * _node = &NodeTree(node);
if (r == Left){
node = node->Left;
node->Parent = _node->Parent;
NodeTree * _rnode = &NodeTree(node->Right);
node->Right = _node;
_node->Parent = node;
_node->Left = _rnode;
_rnode->Parent = _node;
}
else{
node = node->Right;
node->Parent = _node->Parent;
NodeTree * _lnode = &NodeTree(node->Left);
node->Left = _node;
_node->Parent = node;
_node->Right = _lnode;
_lnode->Parent = _node;
}
}
int SearchTree::GetHeight(NodeTree*node){
if (node == NULL)
return NULL;
else{
return GetHeight(node->Left) + GetHeight(node->Right) + 1;
}
}
int SearchTree::GetBalanceFactor(NodeTree* node){
if (node == NULL)
return NULL;
else return GetHeight(node->Left) - GetHeight(node->Right);
}
NodeTree* &SearchTree::FindRightMostNode(NodeTree* &node){
if (node == NULL)
return node;
else{
if (node->Right != NULL){
return FindRightMostNode(node->Right);
}
else return node;
}
}
int SearchTree::GetCardValueByRank(NodeTree* node, int rank){
if (node != NULL){
int n_cards = GetCountOfCards(node->Right);
if (rank > n_cards){
return GetCardValueByRank(node->Right, rank);
}
else if (rank > n_cards + node->c_count){
return node->c_value;
}
else
return GetCardValueByRank(node->Left, rank - n_cards - node->c_value);
}
else return NULL;
}
int SearchTree::GetCountOfCards(NodeTree* node){
if (node != NULL){
return node->c_count + GetCountOfCards(node->Left) + GetCountOfCards(node->Right);
}
else return NULL;
}
#include "BinTree.h"
int main(){
SearchTree st;
st.Insert(1); //inserts card with value "1". <- by this point, root is still NULL. A value will be assigned to it at this stage.
st.Insert(2); //insert card with value "2". <- pointer here is still valid, but it gets invalid within the method.
}
void SearchTree::Insert(NodeTree *&node,NodeTree *parent, int cardvalue){
if (node == NULL)
{
node = &NodeTree(parent, cardvalue);
BalanceNode(parent);
}
void SearchTree::Insert(int cardvalue){
Insert(root, NULL, cardvalue); //on card # 2
}
void SearchTree::Insert(NodeTree *&node,NodeTree *parent, int cardvalue){
if (node == NULL) // breakpoint here
void SearchTree::Insert(NodeTree *&node,NodeTree *parent, int cardvalue){
if (node == NULL)
{
node = &NodeTree(parent, cardvalue);
BalanceNode(parent);
}
NodeTree(NodeTree * _Parent, int cardvalue){
Parent = _Parent;
Left = NULL;
Right = NULL;
c_value = cardvalue;
c_count = 1;
}