GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Jul-2008, 00:42
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

General Allocator for Several Data Structure


Hello to all expect C++ programmer,

I have code link list, double link list and binary search tree in C.

Memory allocation and memory deallocation is take place for every new data structure i program.

CPP / C++ / C Code:
#ifndef _BST_
#define _BST_
 
struct Tree_Node 
{
    int value;
    struct Tree_Node *leftNode;
    struct Tree_Node *rightNode;
};
 
struct BST
{
    struct Tree_Node *root;
};

// -----------------Tree Node Function---------------------------
struct Tree_Node* Tree_Node_Allocate(struct Tree_Node *);
void Tree_Node_Deallocate(struct Tree_Node *);



struct BST* BST_Allocate(struct BST *BST)
{
    BST = malloc(sizeof(struct BST));
    assert(BST != 0);
    BST->root = NULL;
 
    return BST;
}
// -----------------------------------------------------------
int* BST_Deallocate(struct BST *BST, int *isBST_De_allocate)
{
	// Memory Leak if i only delete root and not
    // all tree nodes request from heap memory
	/*
		Use postorder traversal method to free Tree Node
		and finally root
	*/

    if (BST != NULL && BST->root != NULL)
    {
		if (*isBST_De_allocate == 0)
		{
			RecursivePostOrder_Deallocate(BST->root);
			free(BST);
		}
    }
    else
    {
        exit(0);
    }
	
	*isBST_De_allocate = 1;

	return isBST_De_allocate;
}

Link List code :
CPP / C++ / C Code:
#ifndef _BST_
#define _BST_
 
struct Tree_Node 
{
    int value;
    struct Tree_Node *leftNode;
    struct Tree_Node *rightNode;
};
 
struct BST
{
    struct Tree_Node *root;
};

// -----------------Tree Node Function---------------------------
struct Tree_Node* Tree_Node_Allocate(struct Tree_Node *);
void Tree_Node_Deallocate(struct Tree_Node *);
 
struct Tree_Node* Tree_Node_Initialize(struct Tree_Node *, int *);
 
struct Tree_Node* Searching(struct BST *, int *);
struct Tree_Node* RecursiveSearch(struct Tree_Node *, int *, ...);
struct Tree_Node* RecursiveMinSearch(struct Tree_Node *);
struct Tree_Node* RecursiveMaxSearch(struct Tree_Node *);

// --------------------------------------------------------------

// ------------------BST Tree Function---------------------------
struct BST* BST_Allocate(struct BST *);
int* BST_Deallocate(struct BST *, int *);
 
struct BST* BST_Insert(struct BST *, struct Tree_Node *);
struct BST* BST_Remove(struct BST *,int *); 

void FindMin(struct BST *);
void FindMax(struct BST *);
void MinHeight(int *);
void MaxHeight(int *);

void BST_UserInput(int *);
int* QuerySearchValue(int *);
int* numberTreeNodes(int *);
int* removeNode(int *);

// Root->Left->Right
/*
	If left node has children, traversal down until left
	has no children
	Or called Depth-First Traversal(DFS)
*/
void PreorderDisplay(struct BST *);  
void RecursivePreorderDisplay(struct Tree_Node *);

// Left->Root->Right
void InorderDisplay(struct BST *);
void RecursiveInorderDisplay(struct Tree_Node *);

// Left->Right->Root
void PostorderDisplay(struct BST *);
void RecursivePostorderDisplay(struct Tree_Node *);
/*
	Breadth-first traversal(BFS) - traversal from level 0 
	until lowest level
*/
void RecursivePostOrder_Deallocate(struct Tree_Node *);
void SearchDisplay(struct Tree_Node *, int *);

// --------------------------------------------------------------

#endif
/*
	Binary Search Tree can be used for efficient searching, 
	arithmetic expression parsing, and 
	data compression

	Fullness - Previous Level * 2 = Current Level is 
			   consider fullness
	Balance - isBalanced() - Find Maximum height
	          of left node and right node
			  if == balanced else unbalaced
			  All node must same height then 
			  considered balanced
	Leftness - 

    Recursion good for keeping track of paths(Fixed Path or a route)

	AVL Tree - Tree that help balancing when inserted
	Splay Tree - Move data that frequent accessed to
				 the top of tree
    Heap - Another sorted tree structure used maily
	       in priority queue
	Huffman Tree - Used for data compression
    
*/



As you can seen from here, this two program has its own memory allocation and memory deallocation.

Therefore, i thought i going to reqrite BST in C++ but must have a general allocator for any data structure such as AVL tree, adjacency list and much more.

I not intent to replace STL but just want to grasp some important concept and techniques of software reuse in C++.

I hope you understand what i mentioned here.

A billion thanks for your help.
  #2  
Old 02-Jul-2008, 02:43
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: General Allocator for Several Data Structure


Quote:
Originally Posted by Peter_APIIT
As you can seen from here, this two program has its own memory allocation and memory deallocation.
While nothing stops you from using malloc() & free(), the C-style of dynamic memory management is typeless. Nothing is necessarily gained by taking the route proposed unless your real intent is implement binary search trees in C as opposed to C++.

For extra points, you will show greater mastery if you can emulate C++ templates in C. Your proposal above only stores integers within each node. Try to implement a mechanism which will allow any type to be stored within a node without resorting to void*. That's cheating.
  #3  
Old 04-Jul-2008, 21:43
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: General Allocator for Several Data Structure


My intention to create a memory allocation and memory deallocate mechanism for some data structure. After achieved this, i will let the tree suitable for any data type.

I see no point for me to implement since C++ provide such facilities to us.

Thanks for your help.
  #4  
Old 11-Jul-2008, 02:46
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: General Allocator for Several Data Structure


Please help me.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory leak when nothing is happening... How can I even debug this ? Algar MS Visual C++ / MFC Forum 10 19-Nov-2007 08:17
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
Strange C++ code memory leakage problem gaoanyu C++ Forum 7 04-Nov-2005 09:09
[GIM] Data Module - Contact dsmith C Programming Language 2 27-Jan-2005 17:30
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:56.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.