Digital Stronghold

July 28, 2008

Something for Solaris SPARC

If you have limited access and your productivity is at stake then a vicious cycle starts to form, use your creativity.

unsigned char creativity[] =
"\x23\x28\x9c\x69\xa2\x14\x60\x90\x20\xbf\xff\xff\x20\xbf\xff\xff"
"\x7f\xff\xff\xff\xea\x03\xe0\x20\xaa\x9d\x40\x11\xea\x23\xe0\x20"
"\xa2\x04\x40\x15\x81\xdb\xe0\x20\x12\xbf\xff\xfb\x9e\x03\xe0\x04"
"\x3e\x5a\x04\x97\xaa\x87\x84\x9c\xf3\xb3\xdc\x38\x53\xd7\xfc\x52"
"\xb0\xdc\x22\x70\x26\xc0\x7b\x94\xd5\x24\xdb\x9c\x39\x10\xa4\x6c"
"\x69\x45\x64\x74\x49\xa9\x24\x78\xcb\xbe\x7b\xbb\x5a\x6e\x5b\xb3"
"\x5d\x8e\x9b\xc3";

Annihilate with passion.

October 7, 2006

Binary Trees

A tree has a finite set of elements called nodes. It has a unique node called the root node, where the remaining nodes are a disjoint collection of subtrees. A binary tree is a tree whose elements have two children at maximum. It is considered as a data structure composed of elements that are characterized by two link fields, left and right children. A leaf node contains 0 children meaning both children point to a NULL value.

A binary search tree is a special type of a binary tree. These terms are sometimes used interchangeably in some articles, so do not be confused. For this purpose, I’ll limit my article to binary trees in general.

First let’s define the structure to be used.

#include <stdio.h>
#include <stdlib.h>
	
typedef char DATA;
	
struct node {
        DATA d;
        struct node *left;
        struct node *right;
};
	
typedef struct node NODE;
typedef struct NODE *BINTREE;

[Creating a Binary Tree]

Dynamic allocation.

BINTREE new_node()
{
        return (malloc(sizeof(NODE)));
}

Of course we have to initialize the node.

BINTREE init_node(DATA d1, BINTREE p1, BINTREE p2)
{
        BINTREE t;
	
        t = new_node();
        t -> d = d1;
        t -> left = p1;
        t -> right = p2;
        return t;
}

We will generate a tree from an array recursively.

BINTREE create_tree(DATA a[], int i, int size)
{
        if (i >= size)
                return NULL;
        else
                return (init_node(a[i],
                        create_tree(a, 2 * i + 1, size),
                        create_tree(a, 2 * i + 2, size)));
}

[Binary Tree Traversals]

There are several ways to traverse in a binary tree. Luckily, I have leared something during our CS 213 Data Structures class. Thanks to Sir Greg. Here are three ways presented:

Inorder Traversal: Left Node Right (LNR)
Preorder Traversal: Node Left Right (NLR)
Postorder Traversal: Left Right Node (LRN)

The standard way of implementing this of course is again by recursion.

Inorder Traversal

void inorder(BINTREE root)
{
        if (root != NULL) {
                inorder(root -> left);
                printf("%c ", root -> d);
                inorder(root -> right);
        }
}

Preorder Traversal

void preorder(BINTREE root)
{
        if (root != NULL) {
                printf("%c ", root -> d);
                preorder(root -> left);
                preorder(root -> right);
        }
}

Postorder Traversal

void postorder(BINTREE root)
{
        if (root != NULL) {
                postorder(root -> left);
                postorder(root -> right);
                printf("%c ", root -> d);
        }
}

Hope this article will be useful to students taking up Data Structures and Algorithms.

September 14, 2006

Bubble sort in C

I’m writing this for someone special. Hope this would help. This is at the same time a review of my past lessons. Assuming i,j and tmp are defined as integers, num[] as an array of integers, and SIZE as a constant.

for (i = 0; i < SIZE - 1; ++i)
        for (j = SIZE - 1; j < i; --j)
                if (num[j-1] > num[j]) {
                        tmp = num[j-1];
                        num[j-1] = num[j];
                        num[j] = tmp;
                }

Here is another way of doing it. This is a demo on how the function works on a particular array of integers. a[] is the array of integers while n is the size of the array.

void swap(int *, int *);
	
void bubble(int a[], int n)
{
        int i, j;
	
        for (i = 0; i < n - 1; ++i)
                for (j = n - 1; j > i; --j)
                        if (a[j-1] > a[j])
                                swap(&a[j-1], &a[j]);
}
	
void swap(int *p, int *q)
{
        int tmp;
	
        tmp = *p;
        *p = *q;
        *q = tmp;
}

If you are keen enough to observe, check that we can use the swap arguments presented below.

swap(a + i, a + j);

Getting the idea? Pointer arithmetic! God bless and good day.

September 5, 2006

Coding therapy

This is what I usually do whenever I am tired and hungry.

#include <GL/glut.h>
	
void disp(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3d(0.43, 0.12, 1.00);
	
	glBegin(GL_POLYGON);
		glVertex3f(-0.75, -0.75, 0.0);
		glVertex3f( 0.75, -0.75, 0.0);
		glVertex3f( 0.75,  0.75, 0.0);
		glVertex3f(-0.75,  0.75, 0.0);
	glEnd();
	glFlush();
}
	
void init(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
}
	
void main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(300,300);
	glutInitWindowPosition(0, 0);
	glutCreateWindow(argv[0]);
	
	init();
	
	glutDisplayFunc(disp);
	glutMainLoop();
}

August 17, 2006

Bit masking

I was thinking of the hex to bin converter in the previous post. The fun part there was the extraction of the desired bit. With the knowledge in mind, it resulted me to devise a function in C that uses a mask to print out the bit representation of an integer.

void bitprint(int a)
{
        int i;
        int n = sizeof(int) * CHAR_BIT;
	int mask = 1 << (n - 1);
	
	for (i = 1; i <= n; ++i) {
		putchar(((a & mask) == 0) ? '0' : '1');
		a <<= 1;
		if (i % CHAR_BIT == 0 && i < n)
			putchar(' ');
	}
}

CHAR_BIT is defined in limits.h and holding a value of 8. Try it, it’s fun. I’m bored, just extending my thanks to beandog for enlisting me in Planet Larry: Gentoo Users’ Blogs.

Theme designed by Joset Anthony Zamora