Skip to main content

Understanding Tree structures

Tree structures
Tree Structures

Introduction

In many situation, an efficient way to represent data structures is to use trees. A
tree can be defined recursively as an object containing some data and references
to a certain number of subtrees. This definition leads to a hierarchical structure,
in which trees with no sub-trees are called leaves. The other ones are called
internal nodes.

More mathematically, a tree is a graph with no cycles.
Those data structures are very useful to store and organize informations associ-
ated to comparable values. Here we give an example of an associative memory
int -> string.


A simple implementation


class Tree {
// The key
int key;
// The associated string
string str;
// The two subtrees
Tree *left, *right;
public:
Tree(int *k, string *s, int n);
~Tree();
int size();
int depth();
int nbLeaves();
string get(int k);
};
int main(int argc, char **argv) {
string s[] = { "six", "four", "three", "seven",
"two", "one", "nine", "five", "eight", "ten" };
int k[] = { 6, 4, 3, 7, 2, 1, 9, 5, 8, 10 };
Tree t(k, s, 10);
cout << t.get(3) << "\n";
}
Tree::Tree(int *k, string *s, int n) {
for(int i = 0; i<n; i++) cout << k[i] << ":" << s[i]<< " ";
cout << "\n";
key = k[0];
str = s[0];
int *ktmp = new int[n-1];
string *stmp = new string[n-1];
int a = 0, b = n-2;
for(int i = 1; i<n; i++) if(k[i] < key) {
ktmp[a] = k[i]; stmp[a] = s[i]; a++;
} else {
ktmp[b] = k[i]; stmp[b] = s[i]; b--;
}
if(a > 0) left = new Tree(ktmp, stmp, a);
else left = 0;
if(b < n-2) right = new Tree(ktmp+a, stmp+a, n-2-b);
else right = 0;
}
Tree::~Tree() {
if(left) delete left;
if(right) delete right;
}
int Tree::size() {
int n = 1;
if(left) n += left->size();
if(right) n += right->size();
return n;
}
int Tree::depth() {
int dl, dr;
if(left) dl = left->depth(); else dl = 0;
if(right) dr = right->depth(); else dr = 0;
if(dl > dr) return dl+1; else return dr+1;
}
string Tree::get(int k) {
if(key == k) return str;
else if(k < key) {
if(left) return left->get(k);
else abort();
} else {
if(right) return right->get(k);
else abort();
}
}
template<class T>
class Stack {
int size, maxSize;
T *data;
public:
Stack(int m) : size(0), max Size(m), data(new T[m]) {}
~Stack() { delete[] data; }
T pop() { if(size == 0) abort(); else return data[--size]; }
void push(T x) { if(size == maxSize) abort(); else data[size++] = x; }
void print(ostream &o) {
for(int i = size-1; i >= 0; i--) o << data[i] << ’\n’;
}
};
class String Mapping {
public:
virtual string apply(string s) const = 0;
};
void Tree::stacks Elements(Stack<string> &stack) {
stack.push(str);
if(left) left->stacks Elements(stack);
if(right) right->stacks Elements(stack);
}
void Tree::apply Mapping(const String Mapping &map) {
str = map.apply(str);
if(left) left->apply Mapping(map);
if(right) right->apply Mapping(map);
}
class Add Something: public String Mapping {
string stuff;
public:
Add Something(string s) { stuff = s; }
string apply(string s) const { return s + stuff; }
};
int main(int argc, char **argv) {
string s[] = { "six", "four", "three", "seven",
"two", "one", "nine", "five", "eight", "ten" };
int k[] = { 6, 4, 3, 7, 2, 1, 9, 5, 8, 10 };
Tree t(k, s, 10);
cout << t.get(3) << "\n";
Stack<string> stack(100);
t.applyMapping(AddSomething(" excellent!!!"));
t.stacksElements(stack);
stack.print(cout);
}
6:six 4:four 3:three 7:seven 2:two 1:one 9:nine 5:five 8:eight 10:ten
4:four 3:three 2:two 1:one 5:five
3:three 2:two 1:one
2:two 1:one
1:one
5:five
10:ten 8:eight 9:nine 7:seven
8:eight 9:nine 7:seven
7:seven
9:nine
three
nine excellent!!!
seven excellent!!!
eight excellent!!!
ten excellent!!!
five excellent!!!
one excellent!!!
two excellent!!!
three excellent!!!
four excellent!!!
six excellent!!!

Popular posts from this blog

C++ Program to find the sum, difference, product and quotient of two integers

#include <iostream.h> #include <conio.h> void main() {   clrscr();   int x = 10;   int y = 2;   int sum, difference, product, quotient;   sum = x + y;   difference = x - y;   product = x * y;   quotient = x / y;   cout << "The sum of " << x << " & " << y << " is " << sum << "." << endl;   cout << "The difference of " << x << " & " << "y <<  is " << difference << "." << endl;   cout << "The product of " << x << " & " << y << " is " << product << "." << endl;   cout << "The quotient of " << x << " & " << y << " is " << quotient << "." << endl;   getch(); }

Putimage function in c

putimage function outputs a bit image onto the screen. Declaration:- void putimage(int left, int top, void *ptr, int op); putimage puts the bit image previously saved with getimage back onto the screen, with the upper left corner of the image placed at (left, top). ptr points to the area in memory where the source image is stored. The op argument specifies a operator that controls how the color for each destination pixel on screen is computed, based on pixel already on screen and the corresponding source pixel in memory. c smiling face animation This animation using c draws a smiling face which appears at random position on screen. See output below the code, it will help you in understanding the code easily. C programming code #include<graphics.h> #include<conio.h> #include<stdlib.h>   main() { int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75; void *p;   initgraph(&gd,&gm,"C:\\TC\\BGI");   setcolor(YELLOW)...

What is Dynamic Memory Allocation in C++ Program

In the computer world, anything that is processed be it an instruction or any data first needs to be loaded and located in internal memory.  In C++ programs also any data that is processed while executing the program is held in the internal memory.  What is Dynamic Memory Allocation? Dynamic Memory allocation means that the memory that will be used during the program is not known beforehand and is allocated dynamically and on the go. It is allocated during the runtime as and when required by the program. In C++ there are two operators used in dynamic memory allocation  1. New  2. Delete New operator in dynamic memory allocation The new operator in C++ is used to create objects of all types. The new operator will allocate memory of the size of the data type specified in the program.  For Example iptr = new int ;  Storing initial values will allocate needed amount of memory from the free store to hold the value of the specified data-type and store the startin...