Skip to main content

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.

Comments

Popular posts from this blog

Architecture Complexity

Here are the items to consider: Coding to an interface Service Oriented Architecture Automated Testing Domain Driven Design Custom Data Access Layer Layered architecture Complexity is relatively equal the number of lines of code. Note that complexity is not bad. It must be justified.

Moving on

So I bought my self a 2.5″ hard drive enclosure for about 750 bucks. I wish I have my own tools for constructing a device as such. Instead of going for a 2GB USB flash drive, I chose to salvage the 30GB hard drive of an incapacitated laptop. Cool isn’t it? Though relatively slower than a typical USB flash drive, the storage capacity outweighs it. [To God] Thanks for being there always.

Bin to hex converter in 16-bit DOS assembly

Just a quick code like putting your keyboard where your brain is. There should be another way of doing this. I hate this code. start: xor al, al xor bl, bl xor cl, cl mov dl, 4 ; use dl as counter input: mov ah, 00 int 16h cmp ah, 1ch je exit cmp al, '0' jb input cmp al, '1' ja input convert: mov cl, dl dec cl ; dl - 1 sub al, 30h ; get original value shl al, cl ; place in the appropriate bit position or bl, al ; save in bl dec dl ; prepare for the next bit jnz input ; must be 4 bits cmp bl, 9 ja letter jbe number letter: add bl, 37h ; because 'A' - 37h is 0Ah jmp here number: add bl, 30h ; because '0' - 30h is 00h here: call display jmp start display: mov ah, 02h ...