The K-ary Tree Concept
The k-ary Tree
·
The K-ary tree is theory derived from Graph theories.
Also sometimes referred as a k-way tree, an N-ary tree, or an M-ary tree.
The K-ary tree is theory derived from Graph theories.
Also sometimes referred as a k-way tree, an N-ary tree, or an M-ary tree.
Program Implementation of K-ary Tree where k=3 Using structure As Pointers has been given (implemented in C):
#include<stdio.h>
#include<conio.h>
struct NODE
{
char* word;
int depth, children;
struct node **child
};
typedef struct NODE node;
node *createTree();
node *createNode(char* w, int d);
int main()
{
node *root, *pos;
root=createNode("root",0); //Height of the Root node is initially set as Zero
pos=root; //Current navigation position of the tree is at the root.
printf("root has been created with word: %s \n",pos->word);
char *array[]={"String1","String2","String3"};
int i;
pos->child = malloc(3 * sizeof(node *));
for (i=1;i<=3;i++)
{
pos->child[i]=createNode(array[i],(pos->depth)+1);
pos->children++;
printf("%s has been inserted to the tree\n",pos->word);
}
}
node *createNode(char *word,int depth){
node *new_node;
new_node=malloc(sizeof(node));
new_node->word=word;
new_node->depth=depth;
new_node->children=0;
new_node->child=NULL;
}
OUTPUT:
root has been created with word: root
root has been inserted to the tree
root has been inserted to the tree
root has been inserted to the tree
Comments
Post a Comment