We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Binary Search Tree : Insertion
Binary Search Tree : Insertion
Sort by
recency
|
568 Discussions
|
Please Login in order to post a comment
For those wanted to do C#, they don't have the stub code, so I wrote it.
"c language" struct node* newNode(int data) { struct node* node=(struct node*)malloc(sizeof(struct node)); node->data=data; node->left=NULL; node->right=NULL; return(node); }
struct node* insert(struct node* root, int data) { if(root==NULL) {
return(newNode(data)); } else { struct node* cur; if(data<=root->data) { cur=insert(root->left,data); root->left=cur; } else { cur=insert(root->right,data); root->right=cur; } } return root; }
typedef struct Node { int data; struct Node *left; struct Node *right; } Node;
Node *createNode(int data) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; }
Node *insert(Node *root, int data) { if (root == NULL) { return createNode(data); }
}
void printInorder(Node *root) { if (root != NULL) { printInorder(root->left); printf("%d ", root->data); printInorder(root->right); } }