Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
    #include <stdio.h>
    #include <stdlib.h>
    #define strcasecmp _stricmp
    struct BSTnode {
    	char word[128], meaning[256];
    	struct BSTnode *left, *right;
    };    
    struct BSTnode *root = NULL;    
    struct BSTnode * createNode(char *word, char *meaning) {
    	struct BSTnode *newnode;
    	newnode = (struct BSTnode *)malloc(sizeof(struct BSTnode));
    	strcpy(newnode->word, word);
    	strcpy(newnode->meaning, meaning);
    	newnode->left = newnode->right = NULL;
    	return newnode;
    } 
    void insert(char *word, char *meaning) {
    	struct BSTnode *parent = NULL, *current = NULL, *newnode = NULL;
    	int res = 0;
    	if (!root) {
    		root = createNode(word, meaning);
    		return;
    	}
    	for (current = root; current != NULL;
    		current = (res > 0) ? current->right : current->left) {
    		res = strcasecmp(word, current->word);
    		if (res == 0) {
    			printf("Duplicate entry!!\n");
    			return;
    		}
    		parent = current;
    	}
    	newnode = createNode(word, meaning);
    	res > 0 ? (parent->right = newnode) : (parent->left = newnode);
    	return;
    }   
    void deleteNode(char *str) {
    	struct BSTnode *parent = NULL, *current = NULL, *temp = NULL;
    	int flag = 0, res = 0;
    	if (!root) {
    		printf("BST is not present!!\n");
    		return;
    	}
    	current = root;
    	while (1) {
    		res = strcasecmp(current->word, str);
    		if (res == 0)
    			break;
    		flag = res;
    		parent = current;
    		current = (res > 0) ? current->left : current->right;
    		if (current == NULL)
    			return;
    	}
    	if (current->right == NULL) {
    		if (current == root && current->left == NULL) {
    			free(current);
    			root = NULL;
    			return;
    		}
    		else if (current == root) {
    			root = current->left;
    			free(current);
    			return;
    		}
    		flag > 0 ? (parent->left = current->left) :
    			(parent->right = current->left);
    	}
    	else {
    		temp = current->right;
    		if (!temp->left) {
    			temp->left = current->left;
    			if (current == root) {
    				root = temp;
    				free(current);
    				return;
    			}
    			flag > 0 ? (parent->left = temp) :
    				(parent->right = temp);
    		}
    		else {
    		 
    			struct BSTnode *successor = NULL;
    			while (1) {
    				successor = temp->left;
    				if (!successor->left)
    					break;
    				temp = successor;
    			}
    			temp->left = successor->right;
    			successor->left = current->left;
    			successor->right = current->right;
    			if (current == root) {
    				root = successor;
    				free(current);
    				return;
    			}
    			(flag > 0) ? (parent->left = successor) :
    				(parent->right = successor);
    		}
    	}
    	free(current);
    	return;
    }
    void findElement(char *str) {
    	struct BSTnode *temp = NULL;
    	int flag = 0, res = 0;
    	if (root == NULL) {
    		printf("Binary Search Tree is out of station!!\n");
    		return;
    	}
    	temp = root;
    	while (temp) {
    		if ((res = strcasecmp(temp->word, str)) == 0) {
    			printf("Word   : %s", str);
    			printf("Meaning: %s", temp->meaning);
    			flag = 1;
    			break;
    		}
    		temp = (res > 0) ? temp->left : temp->right;
    	}
    	if (!flag)
    		printf("Search Element not found in Binary Search Tree\n");
    	return;
    }    
    void inorderTraversal(struct BSTnode *myNode) {
    	if (myNode) {
    		inorderTraversal(myNode->left);
    		printf("Word    : %s", myNode->word);
    		printf("Meaning : %s", myNode->meaning);
    		printf("\n");
    		inorderTraversal(myNode->right);
    	}
    	return;
    }
    int main() {
    	int ch;
    	char str[128], meaning[256];
    	while (1) {
    		printf("\n1. Insertion\t2. Deletion\n");
    		printf("3. Searching\t4. Traversal\n");
    		printf("5. Exit\nEnter ur choice:");
    		scanf_s("%d", &ch);
    		getchar();
    		switch (ch) {
    		case 1:
    			printf("Word to insert:");
    			fgets(str, 100, stdin);
    			printf("Meaning:");
    			fgets(meaning, 256, stdin);
    			insert(str, meaning);
    			break;
    		case 2:
    			printf("Enter the word to delete:");
    			fgets(str, 100, stdin);
    			deleteNode(str);
    			break;
    		case 3:
    			printf("Enter the search word:");
    			fgets(str, 100, stdin);
    			findElement(str);
    			break;
    		case 4:
    			inorderTraversal(root);
    			break;
    		case 5:
    			exit(0);
    		default:
    			printf("You have entered wrong option\n");
    			break;
    		}
    	}
    	return 0;
}


What I have tried:

this code for dictionary implementation in C using Binary Trees i want convert to c#
Posted
Updated 10-Mar-18 10:54am
Comments
Richard MacCutchan 10-Mar-18 5:41am    
Then you need to rewrite it using C# classes and methods.
kh medo 10-Mar-18 5:44am    
ok thank you
BillWoodruff 11-Mar-18 3:11am    
fyi: Dictionary in .NET is implemented using HashTables. Are you sure a .NET Dictionary is what you want ?

The code you show suggests a Tree.

Don't. Converting code from C to C# will not produce good C# code - C is all pointer based, and C# isn't. C# is also class based, which C has no knowledge of.
And C# uses .NET which includes a wealth of collections which already handle all "the fiddly stuff" for you - including Dictionaries, Lists, and HashTables.

Write it from scratch in C# - you will get a much better result than you possibly would converting from C!
 
Share this answer
 
Comments
CPallini 10-Mar-18 16:50pm    
5.
BillWoodruff 11-Mar-18 13:58pm    
I ain't the only one was surprised to find the BCL, and the other Collection libraries, have no Tree Collections, but, it ain't that hard to implement them, and there's lots of examples available.
As Griff wisely suggested, don't do that. Rewrite it from scratch using available classes. I guess the SortedSet(T) Class (System.Collections.Generic)[^] could fit the bill.
 
Share this answer
 
Comments
BillWoodruff 11-Mar-18 3:09am    
My vote of #1. How is SortedSet relevant to the OP's question ? You could at least explain that.
CPallini 11-Mar-18 5:49am    
Wow, thank you very much Bill.
And no, I am not going to explain. I'm sure the OP (and you) can get the link.
BillWoodruff 11-Mar-18 13:38pm    
I believe you are better than this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900