Click here to Skip to main content
11,505,486 members (57,997 online)
Click here to Skip to main content
Add your own
alternative version

Tree data class for C++

, 12 Jan 2002 165.2K 3K 51
Simple class to represent tree data structures in C++
// treedemo.cpp : Defines the entry point for the console application.
//

#include "stdio.h"
#include "tree.h"
#include "string"
using namespace std;

typedef Tree<string> StringTree;
typedef StringTree::Node StringNode;


// recursive function for prionitng nodes
void PrintNode(StringNode node,int nLevel) {
	// print new line
	fputs("\n",stdout);
	// print spaces for indent (2 for every level)
	string s;
	s.resize(nLevel*2,' ');
	fputs(s.c_str(),stdout);

	// print the value
	fputs(node.get_Data()->c_str(),stdout);

	// iterate through children
	nLevel++;
	for(int n=0;n<node.Count;n++) {
		PrintNode(node.Nodes[n],nLevel);
	}
}

int main(int argc, char* argv[])
{
	// define the tree
	StringTree tree; 
	((string&)tree)="Root";

	//add few nodes
	StringNode node=tree;

	node.AddNode("1").AddNode("11");
	StringNode node2=node.AddNode("2");

	node2.AddNode("21").AddNode("211");
	node2.AddNode("22");
	node2.AddNode("23");
	
	node.AddNode("3");

	// print the tree
	PrintNode(tree,0);

	// wait for enter
	char buf[3];
	fputs("\nPress ENTER :",stdout);
	fgets(buf,3,stdin);
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Alexander Kovachev
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 13 Jan 2002
Article Copyright 2002 by Alexander Kovachev
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid