MetaTrader 5 herunterladen

CTreeNode

Class CTreeNode is a class of node of the binary tree CTree.

Description

Class CTreeNode provides the ability to work with nodes of the binary tree CTree. Options of navigation through the tree is implemented in the class. Besides that methods of work with a file are implemented.

Declaration

   class CTreeNode : public CObject

Title

   #include <Arrays\TreeNode.mqh>

Vererbungshierarchie

  CObject

      CTreeNode

Direkte Ableitungen

CTree

Class Methods

Attributes

 

Owner

Gets/sets the pointer of the owner node

Left

Gets/sets the pointer of the left node

Right

Gets/sets the pointer of the right node

Balance

Gets the node balance

BalanceL

Gets the balance of the left sub-branch of the node

BalanceR

Gets the balance of the right sub-branch of the node

Creation of a new element

 

CreateSample

Creates a new node instance

Comparison

 

RefreshBalance

Recalculates the node balance

Search

 

GetNext

Gets the pointer of the next node

Input/Output

 

SaveNode

Saves the node data to a file

LoadNode

Downloads the node data from a file

virtual Type

Gets the identifier of the node type

Methoden geerbt von der Klasse CObject

Prev, Prev, Next, Next, Save, Load, Compare

Trees of CTreeNode class descendants get practical application.

A descendant of class CTreeNode must have predefined methods: CreateSample that creates a new instance of the descendant class of CTreeNode, Compare that compares values of key fields of the descendant class of CTreeNode, Type (if it's necessary to identify a node), SaveNode and LoadNode (if it's necessary to work with a file).

Let's consider and example of a CTree descendant class.

//+------------------------------------------------------------------+
//|                                                   MyTreeNode.mq5 |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                       https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "2010, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
//---
#include <Arrays\TreeNode.mqh>
//+------------------------------------------------------------------+
//| Describe classderived from CTreeNode.                    |
//+------------------------------------------------------------------+
//| Class CMyTreeNode.                                             |
//| Purpose: Class of element of a binary tree.                |
//|             Descendant of class CTreeNode.                       |
//+------------------------------------------------------------------+
class CMyTreeNode : public CTreeNode
  {
protected:
   //--- user's data
   long              m_long;            // key field of type long
   double            m_double;          // custom variable of type double
   string            m_string;          // custom variable of type string
   datetime          m_datetime;        // custom variable of type datetime
 
public:
                     CMyTreeNode();
   //--- methods of accessing these user's data
   long              GetLong(void)                { return(m_long); }
   void              SetLong(long value)          { m_long=value;  }
   double            GetDouble(void)              { return(m_double); }
   void              SetDouble(double value)      { m_double=value;  }
   string            GetString(void)              { return(m_string); }
   void              SetString(string value)      { m_string=value;  }
   datetime          GetDateTime(void)            { return(m_datetime); }
   void              SetDateTime(datetime value)  { m_datetime=value;  }
   //--- methods of working with files
   virtual bool      Save(int file_handle);
   virtual bool      Load(int file_handle);
protected:
   virtual int       Compare(const CObject *node,int mode);
   //--- methods of creating class instances
   virtual CTreeNode* CreateSample();
  };
//+------------------------------------------------------------------+
//| CMyTreeNode class constructor.                                   |
//| INPUT:  none.                                                    |
//| OUTPUT: none.                                                    |
//| REMARK: none.                                                    |
//+------------------------------------------------------------------+
void CMyTreeNode::CMyTreeNode()
  {
//--- initialization of user's data
   m_long        =0;
   m_double      =0.0;
   m_string      ="";
   m_datetime    =0;
  }
//+------------------------------------------------------------------+
//| Comparison with another three node by the specified algorithm.   |
//| INPUT:  node - array element to compare,                         |
//|         mode - identifier of comparison algorithm.               |
//| OUTPUT: result of comparison (>0,0,<0).                          |
//| REMARK: none.                                                    |
//+------------------------------------------------------------------+
int CMyTreeNode::Compare(const CObject *node,int mode)
  {
//--- parameter mode is ignored, because tree construction algorithm is the only one
   int res=0;
//--- explicit type casting
   CMyTreeNode *n=node;
   res=(int)(m_long-n.m_long);
//---
   return(res);
  }
//+------------------------------------------------------------------+
//| Creation of a new class instance.                    |
//| INPUT:  none.                                                    |
//| OUTPUT: pointer to a new instance of class CMyTreeNode.          |
//| REMARK: none.                                                    |
//+------------------------------------------------------------------+
CTreeNode* CMyTreeNode::CreateSample()
  {
   CMyTreeNode *result=new CMyTreeNode;
//---
   return(result);
  }
//+------------------------------------------------------------------+
//| Write tree node data to a file.                              |
//| INPUT:  file_handle -handle of a file pre-opened for writing.    |
//| OUTPUT: true if OK, otherwise false.                             |
//| REMARK: none.                                                    |
//+------------------------------------------------------------------+
bool CMyTreeNode::Save(int file_handle)
  {
   uint i=0,len;
//--- checks
   if(file_handle<0) return(false);
//--- writing user data
//--- writing custom variable of type long
   if(FileWriteLong(file_handle,m_long)!=sizeof(long))          return(false);
//--- writing custom variable of type double
   if(FileWriteDouble(file_handle,m_double)!=sizeof(double))    return(false);
//--- writing custom variable of type string
   len=StringLen(m_string);
//--- write string length
   if(FileWriteInteger(file_handle,len,INT_VALUE)!=INT_VALUE)   return(false);
//--- write the string
   if(len!=0 && FileWriteString(file_handle,m_string,len)!=len) return(false);
//--- writing custom variable of type datetime
   if(FileWriteLong(file_handle,m_datetime)!=sizeof(long))      return(false);
//---
   return(true);
  }
//+------------------------------------------------------------------+
//| Read tree node data from a file.                                 |
//| INPUT:  file_handle -handle of a file pre-opened for reading.    |
//| OUTPUT: true if OK, otherwise false.                             |
//| REMARK: none.                                                    |
//+------------------------------------------------------------------+
bool CMyTreeNode::Load(int file_handle)
  {
   uint i=0,len;
//--- checks
   if(file_handle<0) return(false);
//--- reading
   if(FileIsEnding(file_handle)) return(false);
//--- reading custom variable of type char
//--- reading custom variable of type long
   m_long=FileReadLong(file_handle);
//--- reading custom variable of type double
   m_double=FileReadDouble(file_handle);
//--- reading custom variable of type string
//--- read the string length
   len=FileReadInteger(file_handle,INT_VALUE);
//--- read the string
   if(len!=0) m_string=FileReadString(file_handle,len);
   else       m_string="";
//--- reading custom variable of type datetime
   m_datetime=FileReadLong(file_handle);
//---
   return(true);
  }