Sponsored Content
Full Discussion: 2-4 trees in C
Top Forums Programming 2-4 trees in C Post 302366722 by bashuser2 on Friday 30th of October 2009 07:36:10 AM
Old 10-30-2009
2-4 trees in C

i am trying to write a program in order to learn how to work with trees and especially 2-4 trees.
the general idea is that each node is represented by 4 cells and 5 pointers? (maybe 2 arrays then? )
let's suppose that we insert simply int numbers in all cells.
firstly we initialize the root (==null ? )
then we fill up all 4 cells of the first node and we must have its 4 int numbers in numerical order?and what happens when we want to insert a 5th element?

of course i don't expect every question answered(as there is only 1 total answer i suppose) but a brief explanation would really help.maybe a link to a tutorial would also be very helpful.


//important: how can i separate internal nodes from leaf nodes?let's suppose that each pointer in the leaf level must point to another array(one level lower that is)which may contain also 4 cells with another type(int or char * etc)...how can i write and create this separation?maybe two structs or can i do that with just one struct and flag variables?
 

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Best way to diff two huge directory trees

Hi I have a job that will be running nightly incremental backsup of a large directory tree. I did the initial backup, now I want to write a script to verify that all the files were transferred correctly. I did something like this which works in principle on small trees: diff -r -q... (6 Replies)
Discussion started by: same1290
6 Replies

2. Shell Programming and Scripting

CGI , Perl and Trees

I have been trying to get this for weeks now but maybe someone knows or has a snippet of code to display a collapsible tree view. something like this: +Yahoo! -/site.html -/site2.html +Google -/site.php -/site2.php (1 Reply)
Discussion started by: Dabheeruz
1 Replies

3. Shell Programming and Scripting

How to copy very large directory trees

I have constant trouble with XCOPY/s for multi-gigabyte transfers. I need a utility like XCOPY/S that remembers where it left off if I reboot. Is there such a utility? How about a free utility (free as in free beer)? How about an md5sum sanity check too? I posted the above query in another... (3 Replies)
Discussion started by: siegfried
3 Replies

4. Slackware

What is the medium usually used to backup large trees?

Hi: What's asked. (2 Replies)
Discussion started by: stf92
2 Replies

5. What is on Your Mind?

From little Acorns big trees grow...

Hi all... This is mainly for Corona688: Do yo remember your translation of a DFT into AWK code? Well it reached 130 dls inside the first 14 days, but take a look at it now. Aminet - dev/gcc/DFT-FFT.awk.txt (0 Replies)
Discussion started by: wisecracker
0 Replies
tsearch(3C)						   Standard C Library Functions 					       tsearch(3C)

NAME
tsearch, tfind, tdelete, twalk - manage binary search trees SYNOPSIS
#include <search.h> void *tsearch(const void *key, void **rootp, int (*compar)(const void *, const void *)); void *tfind(const void *key, void * const *rootp, int (*compar)(const void *, const void *)); void *tdelete(const void *restrict key, void **restrict rootp, int (*compar)(const void *, const void *)); void twalk(const void *root, void(*action) (void *, VISIT, int)); DESCRIPTION
The tsearch(), tfind(), tdelete(), and twalk() functions are routines for manipulating binary search trees. They are generalized from Knuth (6.2.2) Algorithms T and D. All comparisons are done with a user-supplied routine. This routine is called with two arguments, the pointers to the elements being compared. It returns an integer less than, equal to, or greater than 0, according to whether the first argu- ment is to be considered less than, equal to or greater than the second argument. The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared. The tsearch() function is used to build and access the tree. The key argument is a pointer to a datum to be accessed or stored. If there is a datum in the tree equal to *key (the value pointed to by key), a pointer to this found datum is returned. Otherwise, *key is inserted, and a pointer to it returned. Only pointers are copied, so the calling routine must store the data. The rootp argument points to a variable that points to the root of the tree. A null value for the variable pointed to by rootp denotes an empty tree; in this case, the variable will be set to point to the datum which will be at the root of the new tree. Like tsearch(), tfind() will search for a datum in the tree, returning a pointer to it if found. However, if it is not found, tfind() will return a null pointer. The arguments for tfind() are the same as for tsearch(). The tdelete() function deletes a node from a binary search tree. The arguments are the same as for tsearch(). The variable pointed to by rootp will be changed if the deleted node was the root of the tree. tdelete() returns a pointer to the parent of the deleted node, or a null pointer if the node is not found. The twalk() function traverses a binary search tree. The root argument is the root of the tree to be traversed. (Any node in a tree may be used as the root for a walk below that node.) action is the name of a routine to be invoked at each node. This routine is, in turn, called with three arguments. The first argument is the address of the node being visited. The second argument is a value from an enumeration data type typedef enum { preorder, postorder, endorder, leaf } VISIT; (defined in <search.h>), depending on whether this is the first, second or third time that the node has been visited (during a depth-first, left-to-right traversal of the tree), or whether the node is a leaf. The third argument is the level of the node in the tree, with the root being level zero. The pointers to the key and the root of the tree should be of type pointer-to-element, and cast to type pointer-to-character. Similarly, although declared as type pointer-to-character, the value returned should be cast into type pointer-to-element. RETURN VALUES
If the node is found, both tsearch() and tfind() return a pointer to it. If not, tfind() returns a null pointer, and tsearch() returns a pointer to the inserted item. A null pointer is returned by tsearch() if there is not enough space available to create a new node. A null pointer is returned by tsearch(), tfind() and tdelete() if rootp is a null pointer on entry. The tdelete() function returns a pointer to the parent of the deleted node, or a null pointer if the node is not found. The twalk() function returns no value. ERRORS
No errors are defined. USAGE
The root argument to twalk() is one level of indirection less than the rootp arguments to tsearch() and tdelete(). There are two nomenclatures used to refer to the order in which tree nodes are visited. tsearch() uses preorder, postorder and endorder to refer respectively to visiting a node before any of its children, after its left child and before its right, and after both its children. The alternate nomenclature uses preorder, inorder and postorder to refer to the same visits, which could result in some confusion over the meaning of postorder. If the calling function alters the pointer to the root, the results are unpredictable. These functions safely allows concurrent access by multiple threads to disjoint data, such as overlapping subtrees or tables. EXAMPLES
Example 1: A sample program of using tsearch() function. The following code reads in strings and stores structures containing a pointer to each string and a count of its length. It then walks the tree, printing out the stored strings and their lengths in alphabetical order. #include <string.h> #include <stdio.h> #include <search.h> struct node { char *string; int length; }; char string_space[10000]; struct node nodes[500]; void *root = NULL; int node_compare(const void *node1, const void *node2) { return strcmp(((const struct node *) node1)->string, ((const struct node *) node2)->string); } void print_node(const void *node, VISIT order, int level) { if (order == preorder || order == leaf) { printf("length=%d, string=%20s ", (*(struct node **)node)->length, (*(struct node **)node)->string); } } main() { char *strptr = string_space; struct node *nodeptr = nodes; int i = 0; while (gets(strptr) != NULL && i++ < 500) { nodeptr->string = strptr; nodeptr->length = strlen(strptr); (void) tsearch((void *)nodeptr, &root, node_compare); strptr += nodeptr->length + 1; nodeptr++; } twalk(root, print_node); } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
bsearch(3C), hsearch(3C), lsearch(3C), attributes(5), standards(5) SunOS 5.10 6 Dec 2004 tsearch(3C)
All times are GMT -4. The time now is 12:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy