Linux red and Black tree programming example, graphical display of red and black trees

Source: Internet
Author: User

Recently in the Linux kernel to learn the red and black tree, found on the site is not a good example of the visual expression. Referring to the techniques of some great gods on the Internet, finally in

The terminal realizes the visual expression of the red-black tree.

The red-black tree Code We used this time was copied from the Linux kernel: include/linux/rbtree.h and lib/rbtree.c

Since our code is implemented on the application, there are a few changes to these two files:

Rbtree.h:


Note: Comment out two wardrobe files, add macro definitions for offsetof and container_of

RBTREE.C:



Note: Note the turn-around file, add your own changed rbtree.h, comment out all the original Export_symbol


Next we start the official red and black tree programming, this time the red and black Tree example is:


Implementation of code TEST.C:

#include <stdio.h>
#include "rbtree.h"
#include <stdlib.h>
#include <time.h>

struct MyType {
struct Rb_node node;
int key;
};

struct Rb_root mytree = rb_root;

/** same as binary sort tree**/
struct MyType *my_search (struct rb_root *root, int key)
{
struct Rb_node *node = root->rb_node;
struct MyType *data;
while (node) {
data = container_of (node, struct MyType, node);
int result;

if (Key < Data->key) result =-1;
else if (key > Data->key) result = 1;
else result = 0;

if (Result < 0)
node = node->rb_left;
else if (Result > 0)
node = node->rb_right;
Else
return data;
}
return NULL;
}

int My_insert (struct rb_root *root, struct mytype *data)
{
struct Rb_node **new = & (Root->rb_node), *parent = NULL;
struct MyType *this;
/* figure out where to put new node */
while (*new) {
this = container_of (*new, struct MyType, node);


int result;

if (This->key > Data->key) result =-1;
else if (This->key < Data->key) result = 1;
else result = 0;

parent = *new;
if (Result < 0)
New = & ((*new)->rb_left);
else if (Result > 0)
New = & ((*new)->rb_right);
Else
return 0;
}

/* ADD new node and rebalance tree. */
Rb_link_node (& (Data->node), parent, new);
Rb_insert_color (& (Data->node), root);


return 1;
}
int array[]={13,15,11,8,17,1,6,22,25,27};//Red black tree Example shown

#define TAB_SIZE (Array) (sizeof (array)/sizeof (array[0))

void padding (char ch, int n)
{
int i;
if (n>0)
{
for (i = 0; i < (n-1); i++)
printf ("%c", ch);
printf ("--------");
}
}

void Print_node (struct rb_node *node, int level)
{
if (node = = NULL)
{
padding (' \ t ', level);
printf ("\033[42;37m%s\033[0m\n", "null");
}
Else
{
Print_node (Node->rb_right, level + 1);
padding (' \ t ', level);
if (Rb_is_black (node))
{
struct MyType *black = container_of (node, struct MyType, node);
printf ("\033[47;30m%3d\033[0m\n", Black->key);
}
Else
{
struct MyType *red = container_of (node, struct MyType, node);
printf ("\033[41;37m%3d\033[0m\n", Red->key);
}
Print_node (Node->rb_left, level + 1);
}
}

void Print_tree (struct rb_root* tree)
{
printf ("-------------------------------------------\ n");
Print_node (tree->rb_node,0);
printf ("-------------------------------------------\ n");
}
int main (void)
{
struct Rb_node *node;
struct MyType *mynode;
int i;


for (i = 0; I < tab_size (array); i++)
{
Mynode = malloc (sizeof (struct mytype));
Array[i]= (rand ()%100);
Mynode->key= Array[i];
printf ("i =%d, key is%d\n", i,mynode->key);
My_insert (&mytree, Mynode);
}


printf ("*********** up ****************************\n");
i = 0;
for (node = Rb_first (&mytree); node; node = rb_next (node))
{
printf ("i =%d, key =%d\n", I, (rb_entry (node, struct MyType, node))->key);
i++;
}
printf ("------------down---------------------------\ n");
i = 0;
for (node = rb_last (&mytree); node; node = Rb_prev (node))
{
printf ("i =%d, key =%d\n", I, (rb_entry (node, struct MyType, node))->key);
i++;
}
/*
PRINTK ("------------Erase and up---------------------------\ n");
Mynode = My_search (&mytree, array[2]);
if (Mynode)
{
Rb_erase (& (Mynode->node), &mytree);
Kfree (Mynode);
PRINTK ("Erased%d node!\n", array[2]);
}
*/

Print_tree (&mytree);
printf ("-------------------------------------------------------\ n");
return 0;
}
Compilation: Gcc-o rbtree rbtree.c test.c

Running results such as:

Very intuitive to represent the red-black tree, helps to learn and test.

Legacy issues:

In the test, found that in the kernel can not be used, as if the recursive function in the kernel of the use of the problem, the online check seems to be the kernel stack is not enough.

The red and black tree elements are inserted in different order, and the resulting tree is not the same:

int array[]={8,25,1,13,15,11,17,6,22,27}; //shown red black tree instance order changed

Operation Result:



I hope that the great God guidance, thank you!




Linux red and Black tree programming example, graphical display of red and black trees

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.