コード例 #1
0
ファイル: main.cpp プロジェクト: bingone/DataStruct_Algorithm
int main() {
    printf("Hello, World!\n");
    RBTree *tree = RBcreateTree(defaultKeyCompare,defaultValCompare);
    srand(time(NULL));
    for (int i = 0; i < ARR_LEN; i++) {
        arrKey[i] = rand()%INT_RANGE;
        arrVal[i] = rand()%INT_RANGE;
        RBNode *now = RBcreate(arrKey + i, arrVal + i);
        RBinsert(tree, now);
        RBprintSimple(now, 0);
    }
    int level = 0;
    printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
    RBprintTree(tree->root, level);
    printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
    // test insert end
    RBNode *node = RBfind(tree,(void*)(arrKey+rand()%ARR_LEN));
    RBprintSimple(node,0);
    // test find end
    for (int i = 0; i < ARR_LEN; i++) {
        RBdelete(tree, (void *) (arrKey+i));
        printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
        printf("tree size = %d,ready to delete Node{key=%d,value=%d}\n",tree->size,arrKey[i],arrVal[i]);
        RBprintTree(tree->root, level);
        printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
    }
    RBdeleteTree(tree);
    RBprintTree(tree->root, level);
    return 0;
}
コード例 #2
0
ファイル: rbt.c プロジェクト: alior/rbt
int main() {
	int wartosc;
	char znak;
	wezel *root=&NIL;
	NIL.key=-1;
	NIL.left=&NIL;
	NIL.right=&NIL;
	NIL.kolor=BLACK;
	//printf("Co chcesz zrobic? ");
	while(znak!='x') {
		scanf("%c", &znak);
		if(znak=='+'){
			scanf("%d",&wartosc);
			if(TREESEARCH(root, wartosc)==&NIL)  // spr czy klucz juz istnieje
				root = RBinsert(root,wartosc);
			else
				printf("BLAD! taki klucz juz istnieje \n");
		}
		if(znak=='-'){
			scanf("%d",&wartosc);
			root=RBDEL(root, TREESEARCH(root, wartosc));

		}
		if(znak=='p'){
			printf("digraph G {\n");
			wyswietl(root);
			printf("}\n");
		}
	}
	//printf("Narazie..");
	return 0;
}
コード例 #3
0
ファイル: topdownRB.c プロジェクト: snidell/CSE2320
void STinsert(Item item)
{
head = RBinsert(head, item, 0);
if (head->red)
  printf("red to black reset has occurred at root!!!\n");
head->red = 0;
sizeOfTree++;
}
コード例 #4
0
ファイル: rbtree5.c プロジェクト: killinux/hello
int main(){
        int i=0;
        srand(time(NULL));
        BTNode* root=RBinit();
        for(i=0;i<N;i++)
                root=RBinsert(root,rand()%100);
        printf("DispBTree:\n");
        DispBTree(root);
        return 0;
}
コード例 #5
0
ファイル: main.c プロジェクト: EMC2SLN/Skill-Tree
int main(int argc, char *argv[]) {
	rb_tree tree = NULL,
		tmp = NULL;
	int cmd = 0;

	printf("This is a demo for RBTree\n");
	help();
	while (cmd != EOF) {
		int arg;
		printf("$ ");
		fflush(stdout);
		/* Find the first non-whitespace character */
		while (isspace((cmd = getchar()))) {
			if (cmd == '\n') {
				/* We delete up to end of line after the
				 * switch, so put the \n back on. */
				ungetc(cmd, stdin);
				break;
			}
		}
		switch(cmd) {
		case 'C':
		case 'c':
			/* If we already had a tree, we need to free up
			 * the memory. */
			if (tree != NULL) {
				printf("Tree already exists - clearing.\n");
				RBfree(tree);
			}
			tree = RBcreate();
			break;
		case 'R':
		case 'r':
			tmp = RBread(READFILE);
			/* If there was an error, just keep the tree we
			 * have. */
			if (tmp != NULL) {
				if (tree != NULL) {
					printf("Non-empty tree - overwriting.\n");
					RBfree(tree);
				}
				tree = tmp;
			}
			break;
		case 'W':
		case 'w':
			if (tree == NULL) {
				fprintf(stderr, "Error: no tree loaded, cannot write.\n");
			} else {
				RBwrite(tree);
			}
			break;
		case 'I':
		case 'i':
			if (tree == NULL) {
				printf("No tree loaded - creating empty one.\n");
				tree = RBcreate();
			}
			if (scanf("%d", &arg) != 1) {
				fprintf(stderr, "Error: must specify integer key to insert.\n");
			} else {
				RBinsert(tree, arg);
			}
			break;
		case 'D':
		case 'd':
			if (tree == NULL) {
				fprintf(stderr, "Error: no tree loaded, cannot delete.\n");
			} else {
				if (scanf("%d", &arg) != 1) {
					fprintf(stderr, "Error: must specify integer key to delete.\n");
				} else {
					RBdelete(tree, arg);
				}
			} 
			break;
		case 'P':
		case 'p':
			if (tree == NULL) {
				fprintf(stderr, "Error: no tree loaded, cannot draw.\n");
			} else {
				RBdraw(tree, DRAWFILE);
			}
			break;
		case 'H':
		case 'h':
			help();
			break;
		case EOF:
			/* Make the shell not return on the same line */
			putchar('\n');
		case 'S':
		case 's':
			cmd = EOF;
			break;
		/* Corresponds to an empty command */
		case '\n':
			break;
		default:
			fprintf(stderr, "Error: unknown command `%c'.\n", cmd);
			help();
			break;
		}
		if (cmd != EOF) {
			/* Delete the rest of the line. */
			while ((cmd = getchar()) != '\n');
		}
	}

	/* We need to free the tree. */
	if (tree != NULL) {
		RBfree(tree);
	}
	RBcleanup();

	return 0;
}
コード例 #6
0
ファイル: topdownRB.c プロジェクト: snidell/CSE2320
link RBinsert(link h, Item item, int sw)
// Program 13.6 coded to be a bit clearer and make mutually exclusive
// cases obvious.  Also includes tracing.  See 2320 notes.  BPW
// h is present node in search down tree.
// Returns root of modified subtree.
// item is the Item to be inserted.
// sw == 1 <=> h is to the right of its parent.
{
Key v = key(item);
link before;  // Used to trigger printing of an intermediate tree

tracePrint("Down",h);
if (h == z)
  return NEW(item, z, z, 1);  // Attach red leaf

if ((h->l->red) && (h->r->red))      // Flip colors before searching down
{
  tracePrint("Color flip",h);
  h->red = 1;
  h->l->red = 0;
  h->r->red = 0;
  if (trace==2)
    STprintTree();
}

if (less(v, key(h->item))) 
{ 
  tracePrint("Insert left",h);
  before=h->l;
  h->l = RBinsert(h->l, item, 0);    // Insert in left subtree
  if (trace==2 && before!=h->l)      // Has a rotation occurred?
    STprintTree();
  if (h->l->red)
    if (h->red)
      if (sw)
      {
        tracePrint("Case ~1",h);
        h = rotR(h);                 // Set up case ~2 after return
      }
      else
        ;
    else if (h->l->l->red)
    {
      tracePrint("Case 2",h);
      h = rotR(h);
      h->red = 0;
      h->r->red = 1;
    }
}
else
{
  tracePrint("Insert right",h);
  before=h->r;
  h->r = RBinsert(h->r, item, 1);    // Insert in right subtree
  if (trace==2 && before!=h->r)      // Has a rotation occurred?
    STprintTree();
  if (h->r->red)
    if (h->red)
      if (!sw)
      {
        tracePrint("Case 1",h);
        h = rotL(h);                 // Set up case 2 after return
      }
      else
        ;
    else if (h->r->r->red)
    {
      tracePrint("Case ~2",h);
      h = rotL(h);
      h->red = 0;
      h->l->red = 1;
    }
}

fixRank(h); //fix the rank after rotating
tracePrint("Up",h);
return h;
}