Tree container (C ++)

Source: Internet
Author: User
This is a simple container that expresses the structure of a multi-node tree.

//////////////////////////////////////// //////////////////////////////
//
// Class Name:
// Tree
// Description:
// Tree template used to express tree-liked data structure.
// For example, a list of directories name and relations can
// Be stored in tree template.
// Here is a sample to enumerate each member in tree:
// For (tree <int>: iterator E = Tre. Begin (); e! = Tre. End (); ++ E)
//{
// * E = 0;
//}
// The class tree <int>: iterator is a pointer which point
// Nodes in tree. It has getparent (), getfirstsub (), getnextbrother ()
// Fucntions to visit its neighbors.
// Author:
// Sproll
// Date:
// 07-02-09
//
//////////////////////////////////////// //////////////////////////////

# Pragma once

Template <typename T>
Class tree
{
Public:

Class iterator;

PRIVATE:
Class node;

Public:
Class iterator
{
Public:
Iterator (): m_pnode (null ){};
Iterator (node * pnode): m_pnode (pnode ){};
Iterator (const iterator & copy ):
M_pnode (copy. m_pnode ){};
~ Iterator (){};

// Check if point to a valid Tree node
Bool valid () {return m_pnode! = NULL & m_pnode! = 0 xffffffff ;};
// Check if has parent node
Bool hasparent () {return m_pnode-> m_pparent! = NULL ;};
// Get parent node
Iterator getparent () {return m_pnode-> m_pparent ;};
// Check if has sub Node
Bool hassub () {return m_pnode-> m_psub! = NULL ;};
// Get first sub Node
Iterator getfirstsub () {return m_pnode-> m_psub ;};
// Check if has brother Node
Bool hasbrother () {return m_pnode-> m_pnext! = NULL ;};
// Get next brother Node
Iterator getnextbrother () {return m_pnode-> m_pnext ;};

Bool operator = (const iterator & right) {return m_pnode = right. m_pnode ;};
Bool Operator! = (Const iterator & right) {return m_pnode! = Right. m_pnode ;};
Iterator & operator = (const iterator & right) {m_pnode = right. m_pnode; return * This ;};
T & operator * () {return m_pnode-> m_value ;};
T * operator-> () {return & (m_pnode-> m_value );};
Iterator & operator ++ ()
{
If (m_pnode & (INT) m_pnode! = 0 xffffffff) // If pointer to valid node and not to the end
{
If (m_pnode-> m_psub)
{
M_pnode = m_pnode-> m_psub;
}
Else if (m_pnode-> m_pnext)
{
M_pnode = m_pnode-> m_pnext;
}
Else
{
Node * pnode = m_pnode;
While (pnode-> m_pparent)
{
If (pnode-> m_pparent-> m_pnext)
{
M_pnode = pnode-> m_pparent-> m_pnext;
Goto lineout;
}
Else
{
Pnode = pnode-> m_pparent;
}
}
M_pnode = (node *) 0 xffffffff; // point to end
}
}

Lineout:
Return * this;
};
Iterator operator ++ (INT) {iterator TMP = * This; ++ (* This); Return TMP ;};

Friend class tree <t>;

PRIVATE:
Node * m_pnode;
};

Tree (): m_proot (null ){};
Tree (const T value): m_proot (new node) {m_proot-> m_value = value ;};
Tree (iterator PNT): m_proot (null) {m_proot = PNT. m_pnode-> clone (null );};
Tree (const tree <t> & copy) {copy. m_proot? M_proot = copy. m_proot-> clone (null): m_proot = NULL ;};
~ Tree () {clear ();};

Tree <t> & operator = (const tree <t> & copy)
{
If (* This! = Copy)
{
Clear ();
Copy. m_proot? M_proot = copy. m_proot-> clone (null): m_proot = NULL;
}
Return * this;
};
Bool operator = (const tree <t> & copy) {return m_proot = copy. m_proot ;};
Bool Operator! = (Const tree <t> & copy) {return m_proot! = Copy. m_proot ;};

// Get first node of tree (it's the root node)
Iterator begin () {return m_proot? Iterator (m_proot): end ();};
// Get end of tree (it's not the same as the last node, it's invalid node)
Iterator end () {return iterator (node *) 0 xffffffff );};
// Clear the tree and release resource
Void clear ()
{
If (m_proot)
{
M_proot-> Del ();
M_proot = NULL;
}
};
// Check if empty
Bool empty () {return m_proot = NULL ;};
// Get total number of nodes
Int size ()
{
Int _ size = 0;
For (iterator COUNT = begin (); count! = End (); ++ count)
{
++ _ Size;
}
Return _ size;
};
// Swap two trees
Void swap (tree <t> & other)
{
Node * TMP = Other. m_proot;
Other. m_proot = m_proot;
M_proot = TMP;
};

// Insert sub node to a tree of specified parent node, the sub node is added to the last of its parent
Iterator insert (T & Value, iterator where)
{
If (where = end ())
{
M_proot = new node;
M_proot-> m_value = value;
Return m_proot;
}
Else
{
Return where. m_pnode-> insert (value );
}
};
// Erase node and its sub Node
Void erase (iterator where)
{
If (where! = End ())
{
Where. m_pnode-> Del ();
If (where = begin ())
{
M_proot = NULL;
}
}
};

PRIVATE:

Struct Node
{
Public:
Node ():
M_pparent (null ),
M_pnext (null ),
M_psub (null ),
M_value (T ())
{};
Node (const node & copy ):
M_pparent (copy. m_pparent ),
M_pnext (copy. m_pnext ),
M_psub (copy. m_psub ),
M_value (copy. m_value)
{};
~ Node (){};

Node & operator = (const node & right)
{
M_pparent = right. m_pparent;
M_pnext = right. m_pnext;
M_psub = right. m_psub;
M_value = right. m_value;
};

Node * insert (T & value)
{
Node * pnode = new node;
Pnode-> m_pparent = this;
Pnode-> m_value = value;

Node * pbrother = m_psub;
If (pbrother)
{
Do
{
If (pbrother-> m_pnext = NULL)
{
Pbrother-> m_pnext = pnode;
Break;
}
Else
{
Pbrother = pbrother-> m_pnext;
}
} While (true );
}
Else
{
M_psub = pnode;
}

Return pnode;
};

Void del ()
{
If (m_pparent)
{
If (m_pparent-> m_psub = This)
{
M_pparent-> m_psub = m_pnext;
}
Else
{
Node * pnode = m_pparent-> m_psub;
Do
{
If (pnode-> m_pnext = This)
{
Pnode-> m_pnext = m_pnext;
Break;
}
Else
{
Pnode = pnode-> m_pnext;
}
} While (true );
}
}
While (m_psub)
{
M_psub-> Del ();
}
Delete this;
};

Node * clone (node * pparent)
{
Node * Copy = new node;
Copy-> m_pparent = pparent;
If (m_pnext)
{
Copy-> m_pnext = m_pnext-> clone (pparent );
}
If (m_psub)
{
Copy-> m_psub = m_psub-> clone (copy );
}
Return copy;
};

Node * m_pparent; // parent node
Node * m_pnext; // next brother Node
Node * m_psub; // sub Node
T m_value; // node Value
};

Node * m_proot;
};
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.