Home | Develop | Download | Contact
BinaryTreeNode.hpp
1/*
2 * BinaryTreeNode.hpp
3 *
4 * Copyright 2018 Fernando Pujaico Rivera <fernando.pujaico.rivera@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 *
21 */
22
32#ifndef __PDS_BINARYTREENODE_HPP__
33#define __PDS_BINARYTREENODE_HPP__
34
35
46namespace Pds{
47
48
57template <typename Datum>
59{
60
61public:
62
64 Datum *P;
65
68
71
72public:
73
82 BinaryTreeNode(void);
83
88 BinaryTreeNode(const Datum &Value);
89
99 BinaryTreeNode( const Datum &Value,
100 Pds::BinaryTreeNode<Datum> *LeftNodePtr,
101 Pds::BinaryTreeNode<Datum> *RightNodePtr);
102
110 BinaryTreeNode( const Datum &Value,
113
121
122
124
129public:
130
157
168 bool Copy(const Pds::BinaryTreeNode<Datum> &B);
169
175 void MakeEmpty(void);
180public:
181
191 bool IsEmpty(void) const;
192
198 bool HasChild0(void) const;
199
205 bool HasChild1(void) const;
206
211public:
212
222 Datum GetVal(void) const;
223
229 const Pds::BinaryTreeNode<Datum>* GetChild0(void) const;
230
236 const Pds::BinaryTreeNode<Datum>* GetChild1(void) const;
237
242public:
243
254 void Print(const std::string &str="") const;
255
262 void PrintNode(const std::string &str,const std::string &enddata="") const;
263
268public:
269
279 void ExportDotInStreamFile( std::ofstream &streamfile,
280 std::string (*FuncDatumToString)(const Datum &)
281 ) const;
282
283
290 bool ExportDotFile( const std::string &filename,
291 std::string (*FuncDatumToString)(const Datum &)
292 ) const;
293
294
299public:
300
330 bool ExportXmlToStringStream( std::stringstream &sstream,
331 std::string (*FuncDatumToXmlString)(const Datum &)
332 ) const;
333
359 bool ExportXmlToStream( std::ofstream &myfile,
360 std::string (*FuncDatumToXmlString)(const Datum &)
361 ) const;
362
387 std::string ExportXmlToString( std::string (*FuncDatumToXmlString)(const Datum &) ) const;
388
416 bool ImportXmlFromString( const std::string &str,
417 Datum (*FuncXmlStringToDatum)(const std::string &)
418 );
419
420
424}; // Class BinaryTreeNode
425
426} // namespace Pds
427
428
429
434template <typename Datum>
436{
437 this->P=NULL;
438 this->LeftNode=NULL;
439 this->RightNode=NULL;
440
441 return;
442}
443
444template <typename Datum>
446{
447 this->P=new Datum(Value);
448 this->LeftNode=NULL;
449 this->RightNode=NULL;
450
451 return;
452}
453
454
455template <typename Datum>
457 Pds::BinaryTreeNode<Datum> *LeftNodePtr,
458 Pds::BinaryTreeNode<Datum> *RightNodePtr)
459{
460 this->P=new Datum(Value);
461 this->LeftNode=LeftNodePtr;
462 this->RightNode=RightNodePtr;
463 return;
464}
465
466
467
468template <typename Datum>
470 const Pds::BinaryTreeNode<Datum> &LeftNode,
471 const Pds::BinaryTreeNode<Datum> &RightNode)
472{
473 this->P=new Datum(Value);
474
475 if(LeftNode.IsEmpty())
476 this->LeftNode =NULL;
477 else
478 this->LeftNode =new Pds::BinaryTreeNode<Datum>(LeftNode);
479
480 if(RightNode.IsEmpty())
481 this->RightNode =NULL;
482 else
483 this->RightNode=new Pds::BinaryTreeNode<Datum>(RightNode);
484
485 return;
486}
487
488
489template <typename Datum>
491{
492 this->P=NULL;
493 this->LeftNode=NULL;
494 this->RightNode=NULL;
495
496 this->Copy(Node);
497
498 return;
499}
500
501template <typename Datum>
503{
504 this->MakeEmpty();
505}
506
508
509template <typename Datum>
511{
512 if(false==this->Copy(A))
513 this->MakeEmpty();
514
515 //std::cout<<"used copy assignment\n";
516 return *this;
517}
518
519template <typename Datum>
521{
522 if(this!=&Node) //Comprueba que no se esté intentando igualar un objeto a sí mismo
523 {
524 if(Node.IsEmpty())
525 {
526 this->MakeEmpty();
527 return true;
528 }
529
530 this->P=new Datum(*Node.P);
531 if( this->P==NULL )
532 {
533 this->MakeEmpty();
534 return false;
535 }
536
537 if(Node.LeftNode!=NULL) this->LeftNode =new Pds::BinaryTreeNode<Datum>(*Node.LeftNode);
538 if(Node.RightNode!=NULL) this->RightNode=new Pds::BinaryTreeNode<Datum>(*Node.RightNode);
539
540 if( (this->LeftNode==NULL)&&(Node.LeftNode!=NULL) )
541 {
542 this->MakeEmpty();
543 return false;
544 }
545
546 if( (this->RightNode==NULL)&&(Node.RightNode!=NULL) )
547 {
548 this->MakeEmpty();
549 return false;
550 }
551 }
552
553 return true;
554}
555
556
557template <typename Datum>
559{
560 if(this->P!=NULL)
561 {
562 delete this->P;
563 this->P=NULL;
564 }
565
566 if(this->LeftNode!=NULL)
567 {
568 delete this->LeftNode;
569 this->LeftNode=NULL;
570 }
571
572 if(this->RightNode!=NULL)
573 {
574 delete this->RightNode;
575 this->RightNode=NULL;
576 }
577}
578
580template <typename Datum>
581void Pds::BinaryTreeNode<Datum>::Print(const std::string &str) const
582{
583 std::cout<<str;
584
585 if(this->IsEmpty()) return;
586
587 std::cout<<"\n"<<*(this->P);
588
589 if(this->LeftNode!=NULL)
590 {
591 std::cout<<"CALL0\n";
592 this->LeftNode->Print(str+std::to_string(0));
593 }
594 else std::cout<<"NULL0\n";
595
596 if(this->RightNode!=NULL)
597 {
598 std::cout<<"CALL1\n";
599 this->RightNode->Print(str+std::to_string(1));
600 }
601 else std::cout<<"NULL1\n";
602
603 std::cout<<"END\n";
604}
605
606template <typename Datum>
607void Pds::BinaryTreeNode<Datum>::PrintNode(const std::string &str,const std::string &enddata) const
608{
609 std::cout<<str;
610 if(this->P!=NULL)
611 {
612 std::cout<<*(this->P);
613 }
614
615 std::cout<<enddata;
616
617 std::cout<<this->LeftNode<<"\n";
618
619 std::cout<<this->RightNode<<"\n";
620}
621
623template <typename Datum>
625{
626 Datum ERROR;
627 if(this->P==NULL) return ERROR;
628
629 return *(this->P);
630}
631
632template <typename Datum>
634{
635 return this->LeftNode;
636}
637
638template <typename Datum>
640{
641 return this->RightNode;
642}
643
645template <typename Datum>
647{
648 if(this->P==NULL) return true;
649
650 return false;
651}
652
653template <typename Datum>
655{
656 if(this->LeftNode!=NULL) return true;
657
658 return false;
659}
660
661template <typename Datum>
663{
664 if(this->RightNode!=NULL) return true;
665
666 return false;
667}
668
670template <typename Datum>
672 std::string (*FuncDatumToString)(const Datum &)
673 ) const
674{
675 if(this->IsEmpty()) return;
676
677 std::string STRING=FuncDatumToString(*(this->P));
678
679 myfile <<"N"<<(unsigned long)this<<"\t[ label =\""<<STRING<<"\"];\n";
680
681 if(this->LeftNode!=NULL) myfile <<"N"<<(unsigned long)this
682 <<"\t->\tN"<<(unsigned long)this->LeftNode
683 <<"\t[label = \"0\"];\n";
684
685 if(this->RightNode!=NULL) myfile <<"N"<<(unsigned long)this
686 <<"\t->\tN"<<(unsigned long)this->RightNode
687 <<"\t[label = \"1\"];\n";
688
689
690 if(this->LeftNode!=NULL) this->LeftNode->ExportDotInStreamFile(myfile,FuncDatumToString);
691 if(this->RightNode!=NULL) this->RightNode->ExportDotInStreamFile(myfile,FuncDatumToString);
692
693 return;
694}
695
696template <typename Datum>
697bool Pds::BinaryTreeNode<Datum>::ExportDotFile( const std::string &filename,
698 std::string (*FuncDatumToString)(const Datum &)
699 ) const
700{
701 std::ofstream myfile;
702 myfile.open(filename);
703 if (myfile.is_open())
704 {
705 myfile <<"// dot -Tpng -o "<<filename<<".png "<<filename<<"\n";
706 myfile <<"digraph G {\n";
707 this->ExportDotInStreamFile(myfile,FuncDatumToString);
708 myfile <<"}\n";
709
710 myfile.close();
711
712 return true;
713 }
714
715 return false;
716}
717
719template <typename Datum>
721 std::string (*FuncDatumToXmlString)(const Datum &)
722 ) const
723{
724 sstream<<"<"<<Pds::Ra::Tag::BinaryTreeNode<<">\n";
725
726 if(this->IsEmpty()==false)
727 {
728 sstream<<"<Datum>\n";
729 sstream<<FuncDatumToXmlString(*this->P);
730 sstream<<"</Datum>\n";
731
732 sstream<<"<LeftNode>\n";
733 if(this->LeftNode!=NULL)
734 {
735 this->LeftNode->ExportXmlToStringStream(sstream,FuncDatumToXmlString);
736 }
737 sstream<<"</LeftNode>\n";
738
739 sstream<<"<RightNode>\n";
740 if(this->RightNode!=NULL)
741 {
742 this->RightNode->ExportXmlToStringStream(sstream,FuncDatumToXmlString);
743 }
744 sstream<<"</RightNode>\n";
745 }
746
747 sstream<<"</"<<Pds::Ra::Tag::BinaryTreeNode<<">\n";
748
749 return true;
750}
751
752template <typename Datum>
754 std::string (*FuncDatumToXmlString)(const Datum &)
755 ) const
756{
757 if(myfile.is_open()==false) return false;
758
759 std::stringstream ss;
760 this->ExportXmlToStringStream(ss,FuncDatumToXmlString);
761 myfile<<ss.str();
762
763 return true;
764}
765
766template <typename Datum>
767std::string Pds::BinaryTreeNode<Datum>::ExportXmlToString( std::string (*FuncDatumToXmlString)(const Datum &)
768 ) const
769{
770 if(this->IsEmpty()) return "";
771
772 std::stringstream ss;
773 this->ExportXmlToStringStream(ss,FuncDatumToXmlString);
774
775 return ss.str();
776}
777
778
779template <typename Datum>
781 Datum (*FuncXmlStringToDatum)(const std::string &)
782 )
783{
784 unsigned int n;
785 this->MakeEmpty();
786
787 unsigned int L=2*Pds::Ra::Tag::BinaryTreeNode.size()+5;
788
789 if(str.length()<=L) return false;
790
791 std::string StrInNode;
792 StrInNode=Pds::Ra::FirstSubInString(str,
795 );
796 StrInNode=Pds::Ra::Trim(StrInNode);
797
798 if(StrInNode.size()==0) return false;
799
800 std::vector<std::string> dat;
801 std::vector<std::string> tag;
802 dat=Pds::Ra::SubsXmlInString(StrInNode,false,tag);
803
804 std::string StrInDatum="";
805 std::string StrInLeftNode="";
806 std::string StrInRightNode="";
807
808 for(n=0;n<tag.size();n++)
809 {
810 if(tag[n].compare("Datum")==0)
811 {
812 StrInDatum=dat[n];
813 }
814 else if(tag[n].compare("LeftNode")==0)
815 {
816 StrInLeftNode=dat[n];
817 }
818 else if(tag[n].compare("RightNode")==0)
819 {
820 StrInRightNode=dat[n];
821 }
822 }
823
824 if(StrInDatum.size()==0) return false;
825
826 this->P= new Datum();
827 *(this->P)=FuncXmlStringToDatum(StrInDatum);
828
829 if(StrInLeftNode.size()>L)
830 {
831 this->LeftNode = new Pds::BinaryTreeNode<Datum>();
832 this->LeftNode->ImportXmlFromString(StrInLeftNode,FuncXmlStringToDatum);
833 }
834
835 if(StrInRightNode.size()>L)
836 {
837 this->RightNode = new Pds::BinaryTreeNode<Datum>();
838 this->RightNode->ImportXmlFromString(StrInRightNode,FuncXmlStringToDatum);
839 }
840
841
842 return true;
843}
844
845#endif
846
La clase tipo Pds::BinaryTreeNode. Esta clase genera una estructura de datos que contem um valor y do...
Pds::BinaryTreeNode< Datum > * LeftNode
Pds::BinaryTreeNode< Datum > * RightNode
std::string ExportXmlToString(std::string(*FuncDatumToXmlString)(const Datum &)) const
Escribe en un std::string en formato Xml el contenido de todo el arbol desde el Pds::BinaryTreeNode.
Pds::BinaryTreeNode< Datum > & operator=(const Pds::BinaryTreeNode< Datum > &B)
Copia en si mismo (A), un objeto B. Este operador es similar al método Copy().
bool HasChild1(void) const
Verifica si el nodo de la derecha es nulo.
bool Copy(const Pds::BinaryTreeNode< Datum > &B)
Copia en si mismo (A), el contenido de un objeto B. Este método es similar a usar el operador = .
Datum GetVal(void) const
Devuelve el contenido del nodo Pds::BinaryTreeNode.
bool ExportXmlToStringStream(std::stringstream &sstream, std::string(*FuncDatumToXmlString)(const Datum &)) const
Escribe en un std::stringstream en formato Xml el contenido de todo el arbol desde el Pds::BinaryTree...
bool HasChild0(void) const
Verifica si el nodo de la izquierda es nulo.
bool ImportXmlFromString(const std::string &str, Datum(*FuncXmlStringToDatum)(const std::string &))
Lee desde un std::string en formato Xml el contenido de todo el arbol desde el Pds::BinaryTreeNode.
BinaryTreeNode(void)
Crea un objeto de tipo Pds::BinaryTreeNode vacio.
const Pds::BinaryTreeNode< Datum > * GetChild0(void) const
Retorna la direccion del hijo relativo a 0 (izquierda).
void Print(const std::string &str="") const
Muestra en pantalla el Datum del Pds::BinaryTreeNode y de todos sus descendientes....
void MakeEmpty(void)
Limpia los datos internos. Despues de limpiar this->IsEmpty() es igual a true.
void PrintNode(const std::string &str, const std::string &enddata="") const
Muestra en pantalla los datos dentro del actual Pds::BinaryTreeNode. El elemento debe poder ser agreg...
void ExportDotInStreamFile(std::ofstream &streamfile, std::string(*FuncDatumToString)(const Datum &)) const
Salva en formato .dot el objeto de tipo Pds::BinaryTreeNode.
bool ExportDotFile(const std::string &filename, std::string(*FuncDatumToString)(const Datum &)) const
Salva en formato .dot el objeto de tipo Pds::BinaryTreeNode.
bool IsEmpty(void) const
Verifica si el nodo es nulo.
bool ExportXmlToStream(std::ofstream &myfile, std::string(*FuncDatumToXmlString)(const Datum &)) const
Escribe en un std::ofstream en formato Xml el contenido de todo el arbol desde el Pds::BinaryTreeNode...
const Pds::BinaryTreeNode< Datum > * GetChild1(void) const
Retorna la direccion del hijo relativo a 1 (derecha).
const std::string BinaryTreeNode
Tag de un objeto de tipo Pds::Ra::Tag::BinaryTreeNode.
Definition: RaDefines.hpp:397
std::string Trim(const std::string &str)
Esta función retorna una cadena que elimina al inicio y al final algunos caracteres si estos son cara...
std::string FirstSubInString(const std::string &str, const std::string &DelL, const std::string &DelR, bool Full=false)
Retorna la primera cadena de texto dentro de un par de delimitadores.
std::vector< std::string > SubsXmlInString(const std::string &str, bool Full, std::vector< std::string > &Tag)
Busca etiquetas tag entre '<' y '>' de modo de que se busca la forma <tag> dato </tag>....
Nombre de espacio para Pds (Procesamiento Digital de Senales)
Definition: AbstractRV.hpp:42

Enlaces de interés

HomePage Bazaar Download Bug report Ayuda Developer Feed