السلام عليكم
عندي كودين تعمل نفس الغرض
ترتب الكلمات المدخلة ترتيب ابجدي زي الدكشنري
مطلوب مننا اننا ندور الاكواد جاهزة و نضيف له الرن تايم فانكشن و كمان اهم شي انه يكون المدخلات بملف لحال و نعمل له استدعاء من الكود الاساسي
لقينا الاكواد و اضفنا له الرن تايم فانكشن و الحمد لله اشتغل
بس مشكلتنا الان حاولنا كثير اننا نخلي المدخلات بملف لحال
هذا الكود الاول شغال
// C program to demonstrate Ternary Search Tree (TST) insert, travese
// and search operations
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
#include <time.h>
FILE *infile;
FILE *outfile;
// A node of ternary search tree
clock_t start = clock();
clock_t end;
struct Node
{
char data;
// True if this character is last character of one of the words
unsigned isEndOfString: 1;
struct Node *left, *eq, *right;
};
// A utility function to create a new ternary search tree node
struct Node* newNode(char data)
{
struct Node* temp = (struct Node*) malloc(sizeof( struct Node ));
temp->data = data;
temp->isEndOfString = 0;
temp->left = temp->eq = temp->right = NULL;
return temp;
}
// Function to insert a new word in a Ternary Search Tree
void insert(struct Node** root, char *word)
{
// Base Case: Tree is empty
if (!(*root))
*root = newNode(*word);
// If current character of word is smaller than root's character,
// then insert this word in left subtree of root
if ((*word) < (*root)->data)
insert(&( (*root)->left ), word);
// If current character of word is greate than root's character,
// then insert this word in right subtree of root
else if ((*word) > (*root)->data)
insert(&( (*root)->right ), word);
// If current character of word is same as root's character,
else
{
if (*(word+1))
insert(&( (*root)->eq ), word+1);
// the last character of the word
else
(*root)->isEndOfString = 1;
}
}
// A recursive function to traverse Ternary Search Tree
void traverseTSTUtil(struct Node* root, char* buffer, int depth)
{
if (root)
{
// First traverse the left subtree
traverseTSTUtil(root->left, buffer, depth);
// Store the character of this node
buffer[depth] = root->data;
if (root->isEndOfString)
{
buffer[depth+1] = '\0';
printf( "%s\n", buffer);
}
// Traverse the subtree using equal pointer (middle subtree)
traverseTSTUtil(root->eq, buffer, depth + 1);
// Finally Traverse the right subtree
traverseTSTUtil(root->right, buffer, depth);
}
}
// The main function to traverse a Ternary Search Tree.
// It mainly uses traverseTSTUtil()
void traverseTST(struct Node* root)
{
char buffer[MAX];
traverseTSTUtil(root, buffer, 0);
}
// Function to search a given word in TST
int searchTST(struct Node *root, char *word)
{
if (!root)
return 0;
if (*word < (root)->data)
return searchTST(root->left, word);
else if (*word > (root)->data)
return searchTST(root->right, word);
else
{
if (*(word+1) == '\0')
return root->isEndOfString;
return searchTST(root->eq, word+1);
}
}
// Driver program to test above functions
int main()
{
struct Node *root = NULL;
insert(&root, "cat");
insert(&root, "cats");
insert(&root, "up");
insert(&root, "bug");
insert(&root, "able");
insert(&root, "acid");
insert(&root, "angry");
insert(&root, "automatic");
insert(&root, "awake");
insert(&root, "bad");
insert(&root, "beautiful");
insert(&root, "bent");
insert(&root, "bitter");
insert(&root, "black");
insert(&root, "blue");
insert(&root, "boiling");
insert(&root, "bright");
insert(&root, "broken");
insert(&root, "brown");
insert(&root, "certain");
insert(&root, "cheap");
insert(&root, "chemical");
insert(&root, "chief");
insert(&root, "clean");
insert(&root, "clear");
insert(&root, "cold");
insert(&root, "common");
insert(&root, "complete");
insert(&root, "complex");
insert(&root, "conscious");
insert(&root, "cruel");
insert(&root, "cut");
insert(&root, "dark");
insert(&root, "dead");
insert(&root, "dear");
insert(&root, "deep");
insert(&root, "delicate");
insert(&root, "dependent");
insert(&root, "different");
insert(&root, "dirty");
insert(&root, "dry");
insert(&root, "early");
insert(&root, "elastic");
insert(&root, "electric");
insert(&root, "equal");
insert(&root, "false");
insert(&root, "fat");
insert(&root, "feeble");
insert(&root, "female");
insert(&root, "fertile");
insert(&root, "first");
insert(&root, "fixed");
insert(&root, "flat");
insert(&root, "foolish");
insert(&root, "free");
insert(&root, "frequent");
insert(&root, "full");
insert(&root, "future");
insert(&root, "general");
insert(&root, "good");
insert(&root, "gray");
insert(&root, "great");
insert(&root, "green");
insert(&root, "hanging");
insert(&root, "happy");
insert(&root, "hard");
insert(&root, "healthy");
insert(&root, "high");
insert(&root, "hollow");
insert(&root, "ill");
insert(&root, "important");
insert(&root, "kind");
insert(&root, "last");
insert(&root, "late");
insert(&root, "left");
insert(&root, "like");
insert(&root, "living");
insert(&root, "long");
insert(&root, "loose");
insert(&root, "loud");
insert(&root, "low");
insert(&root, "male");
insert(&root, "married");
insert(&root, "material");
insert(&root, "medical");
insert(&root, "military");
insert(&root, "mixed");
insert(&root, "narrow");
insert(&root, "natural");
insert(&root, "necessary");
insert(&root, "new");
insert(&root, "normal");
insert(&root, "old");
insert(&root, "open");
insert(&root, "opposite");
insert(&root, "parallel");
insert(&root, "past");
insert(&root, "physical");
insert(&root, "political");
insert(&root, "poor");
insert(&root, "possible");
insert(&root, "present");
insert(&root, "private");
insert(&root, "probable");
printf("Following is traversal of ternary search tree\n");
traverseTST(root);
/* printf("\nFollowing are search results for cats, bu and cat respectively\n");
searchTST(root, "cats")? printf("Found\n"): printf("Not Found\n");
searchTST(root, "bu")? printf("Found\n"): printf("Not Found\n");
searchTST(root, "cat")? printf("Found\n"): printf("Not Found\n");
*/
end = clock();
printf("\n%f\n", (double)(end - start));
return 0;
}وهذا الكود
بعد محاولة فصل المدخلات بملف صيغتها و اسمها "Eb.txt"و استدعاها
// C program to demonstrate Ternary Search Tree (TST) insert, travese
// and search operations
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
#include <time.h>
FILE *infile;
FILE *outfile;
// A node of ternary search tree
clock_t start = clock();
clock_t end;
struct Node
{
char data;
// True if this character is last character of one of the words
unsigned isEndOfString: 1;
struct Node *left, *eq, *right;
};
// A utility function to create a new ternary search tree node
struct Node* newNode(char data)
{
struct Node* temp = (struct Node*) malloc(sizeof( struct Node ));
temp->data = data;
temp->isEndOfString = 0;
temp->left = temp->eq = temp->right = NULL;
return temp;
}
// Function to insert a new word in a Ternary Search Tree
void insert(struct Node** root, char *word)
{
// Base Case: Tree is empty
if (!(*root))
*root = newNode(*word);
// If current character of word is smaller than root's character,
// then insert this word in left subtree of root
if ((*word) < (*root)->data)
insert(&( (*root)->left ), word);
// If current character of word is greate than root's character,
// then insert this word in right subtree of root
else if ((*word) > (*root)->data)
insert(&( (*root)->right ), word);
// If current character of word is same as root's character,
else
{
if (*(word+1))
insert(&( (*root)->eq ), word+1);
// the last character of the word
else
(*root)->isEndOfString = 1;
}
}
// A recursive function to traverse Ternary Search Tree
void traverseTSTUtil(struct Node* root, char* buffer, int depth)
{
if (root)
{
// First traverse the left subtree
traverseTSTUtil(root->left, buffer, depth);
// Store the character of this node
buffer[depth] = root->data;
if (root->isEndOfString)
{
buffer[depth+1] = '\0';
printf( "%s\n", buffer);
}
// Traverse the subtree using equal pointer (middle subtree)
traverseTSTUtil(root->eq, buffer, depth + 1);
// Finally Traverse the right subtree
traverseTSTUtil(root->right, buffer, depth);
}
}
// The main function to traverse a Ternary Search Tree.
// It mainly uses traverseTSTUtil()
void traverseTST(struct Node* root)
{
char buffer[MAX];
traverseTSTUtil(root, buffer, 0);
}
// Function to search a given word in TST
int searchTST(struct Node *root, char *word)
{
if (!root)
return 0;
if (*word < (root)->data)
return searchTST(root->left, word);
else if (*word > (root)->data)
return searchTST(root->right, word);
else
{
if (*(word+1) == '\0')
return root->isEndOfString;
return searchTST(root->eq, word+1);
}
}
// Driver program to test above functions
int main()
{
{
FILE *fp;
char *fileName = "Eb.txt";
char letter;
struct Node *root = NULL;
fp = fopen(fileName, "r");
/*
insert(&root, "cat");
insert(&root, "cats");
insert(&root, "up");
insert(&root, "bug");
insert(&root, "able");
insert(&root, "acid");
insert(&root, "angry");
insert(&root, "automatic");
insert(&root, "awake");
insert(&root, "bad");
insert(&root, "beautiful");
insert(&root, "bent");
insert(&root, "bitter");
insert(&root, "black");
insert(&root, "blue");
insert(&root, "boiling");
insert(&root, "bright");
insert(&root, "broken");
insert(&root, "brown");
insert(&root, "certain");
insert(&root, "cheap");
insert(&root, "chemical");
insert(&root, "chief");
insert(&root, "clean");
insert(&root, "clear");
insert(&root, "cold");
insert(&root, "common");
insert(&root, "complete");
insert(&root, "complex");
insert(&root, "conscious");
insert(&root, "cruel");
insert(&root, "cut");
insert(&root, "dark");
insert(&root, "dead");
insert(&root, "dear");
insert(&root, "deep");
insert(&root, "delicate");
insert(&root, "dependent");
insert(&root, "different");
insert(&root, "dirty");
insert(&root, "dry");
insert(&root, "early");
insert(&root, "elastic");
insert(&root, "electric");
insert(&root, "equal");
insert(&root, "false");
insert(&root, "fat");
insert(&root, "feeble");
insert(&root, "female");
insert(&root, "fertile");
insert(&root, "first");
insert(&root, "fixed");
insert(&root, "flat");
insert(&root, "foolish");
insert(&root, "free");
insert(&root, "frequent");
insert(&root, "full");
insert(&root, "future");
insert(&root, "general");
insert(&root, "good");
insert(&root, "gray");
insert(&root, "great");
insert(&root, "green");
insert(&root, "hanging");
insert(&root, "happy");
insert(&root, "hard");
insert(&root, "healthy");
insert(&root, "high");
insert(&root, "hollow");
insert(&root, "ill");
insert(&root, "important");
insert(&root, "kind");
insert(&root, "last");
insert(&root, "late");
insert(&root, "left");
insert(&root, "like");
insert(&root, "living");
insert(&root, "long");
insert(&root, "loose");
insert(&root, "loud");
insert(&root, "low");
insert(&root, "male");
insert(&root, "married");
insert(&root, "material");
insert(&root, "medical");
insert(&root, "military");
insert(&root, "mixed");
insert(&root, "narrow");
insert(&root, "natural");
insert(&root, "necessary");
insert(&root, "new");
insert(&root, "normal");
insert(&root, "old");
insert(&root, "open");
insert(&root, "opposite");
insert(&root, "parallel");
insert(&root, "past");
insert(&root, "physical");
insert(&root, "political");
insert(&root, "poor");
insert(&root, "possible");
insert(&root, "present");
insert(&root, "private");
insert(&root, "probable");
*/
printf("Following is traversal of ternary search tree\n");
do
{
fscanf(fp, "%c", &letter);
printf("%c", letter);
}
while(!feof(fp));
printf("\n");
fclose(fp);
traverseTST(root);
}
/* printf("\nFollowing are search results for cats, bu and cat respectively\n");
searchTST(root, "cats")? printf("Found\n"): printf("Not Found\n");
searchTST(root, "bu")? printf("Found\n"): printf("Not Found\n");
searchTST(root, "cat")? printf("Found\n"): printf("Not Found\n");
*/
end = clock();
printf("\n%f\n", (double)(end - start));
}يظهر لنا الاوون بوت و يطلع لنا الرن تايم
بس الاون بوت ما يترسل للديكشنري فنكشن يعني ما يكون مرتب ترتيب ابجدي
و كمان يكرر اخر حرف باخر كلمة
حاولنا كثييييييييييير فيه نحنا ما درسنا شي زي كدا و بحثنا بالنت عن الطريقة و جربناها زي الكود اللي فوق و ما زبط.gif)