Vous êtes sur la page 1sur 3

/* Warning: untested code with no error checking included.

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* A tree node. Holds pointers to left and right sub-trees, and some data (a string).
*/
typedef struct node {
struct node *left;
struct node *right;
char *string;
} node;
node *root; /* pointers automatically initialized to NULL */
int insert(const char *string, node *root) {
/* Add a string to the tree. Keeps in order, ignores dupes.
*/
int num = strcmp(root->string, string);
node *temp;
for(;;) {
if ( 0 == num)
/* duplicate string - ignore it. */
return 1;
else if (-1 == num) {
/* create new node, insert as right sub-tree.
*/
if ( NULL == root -> right ) {
temp = malloc(sizeof(node));
temp -> left = NULL;
temp -> right = NULL;
temp -> string = strdup(string);
return 2;
}
else
root = root -> right;
}
else if ( NULL == root ->left ) {
/* create new node, insert as left sub-tree.
*/
temp = malloc(sizeof(node));
temp -> left = NULL;
temp -> right = NULL;
temp -> string = strdup(string);
return 2;
}
else
root = root -> left;
}
}
void print(node *root) {

/* in-order traversal -- first process left sub-tree.


*/
if ( root -> left != NULL )
print(root->left);
/* then process current node.
*/
fputs(root->string, stdout);

/* then process right sub-tree


*/
if ( root->right != NULL )
print(root->right);

int main() {
char line[100];
/* Let user enter some data. Enter an EOF (e.g., ctrl-D or F6) when done.
*/
while ( fgets(line, 100, stdin))
insert(line, root);
/* print out the data, in order
*/
print(root);
return 0;
}

//////////////////////////////////////////////////////////////////

Difference between graph and tree:


A tree can be described as a specialized case of graph with no self loops and circuits.
There are no loops in a tree whereas a graph can have loops.
There are three sets in a graph i.e. edges, vertices and a set that represents their relation while a
tree consists of nodes that are connected to each other. These connections are referred to as
edges.
In tree there are numerous rules spelling out how connections of nodes can occur whereas
graph has no rules dictating the connection among the nodes.

What is the difference between Tree and Graph


in Data Structure?

Tree is considered as a special case of graph. It is also termed as a


minimally connected graph.
Every tree can be considered as a graph, but every graph cannot be
considered as a tree.
Self-loops and circuits are not available in the tree as in the case of graphs.
For designing tree, you require a parent node and various sub-nodes. For
designing a graph, you require vertices and edges. Edge is a pair of vertices.
The above discussion concludes that tree and graph are the most popular
data structures that are used to resolve various complex problems. Graphs
are a more popular data structure that is used in computer designing,
physical structures and engineering science. Most of the puzzles are
designed with the help of graph data structure. Shortest distance problem is
the most commonly used data structure. In this problem, we have to
calculate the shortest distance between two vertices.

Vous aimerez peut-être aussi