I'm a Python guy. Learning C language and I've been trying to implement Binary Search Tree in C. I wrote down the code, and I've been trying from few hours but, not able to get the output as expected. Please help!

Please correct me.

```
#include<stdlib.h>
#include<stdio.h>
typedef int ElementType;
typedef struct TreeNode {
ElementType element;
struct TreeNode *left, *right;
} TreeNode;
TreeNode *createTree(){
//Create the root of tree
TreeNode *tempNode;
tempNode = malloc(sizeof(TreeNode));
tempNode->element = 0;
tempNode->left = NULL;
tempNode->right = NULL;
return tempNode;
}
TreeNode *createNode(ElementType X){
//Create a new leaf node and return the pointer
TreeNode *tempNode;
tempNode = malloc(sizeof(TreeNode));
tempNode->element = X;
tempNode->left = NULL;
tempNode->right = NULL;
return tempNode;
}
TreeNode *insertElement(TreeNode *node, ElementType X){
//insert element to Tree
if(node==NULL){
return createNode(X);
}
else{
if(X < node->element){
node->left = insertElement(node->left, X);
}
else if(X > node->element){
node->right = insertElement(node->right, X);
}
else if(X == node->element){
printf("Oops! the element is already present in the tree.");
}
}
}
TreeNode *displayTree(TreeNode *node){
//display the full tree
if(node==NULL){
return;
}
displayTree(node->left);
printf("| %d ", node->element);
displayTree(node->right);
}
main(){
//pointer to root of tree #2
TreeNode *TreePtr;
TreeNode *TreeRoot;
TreeNode *TreeChild;
//Create the root of tree
TreePtr = createTree();
TreeRoot = TreePtr;
TreeRoot->element = 32;
printf("%d\n",TreeRoot->element);
insertElement(TreeRoot, 8);
TreeChild = TreeRoot->left;
printf("%d\n",TreeChild->element);
insertElement(TreeRoot, 2);
insertElement(TreeRoot, 7);
insertElement(TreeRoot, 42);
insertElement(TreeRoot, 28);
insertElement(TreeRoot, 1);
insertElement(TreeRoot, 4);
insertElement(TreeRoot, 5);
// the output is not as expected :(
displayTree(TreeRoot);
}
```

Your `insertElement`

does not always return a value. This is why your recursive calls go wrong. Tell your compiler to warn you about mistakes like that (e.g., on gcc, use `-Wall`

).

`displayTree`

has a similar error, returning nothing when it is specified to return a `TreeNode*`

.

`main`

should also return a value (or you should declare it `void`

).

The problem is in the insertion. If `node`

is `NULL`

you create a new node and return it. But what if the node is not `NULL`

. You are making correct changes to the right/left subtree but you are not returning anything.

Change

```
if(X < node->element){
node->left = insertElement(node->left, X);
}
else if(X > node->element){
node->right = insertElement(node->right, X);
}
```

to:

```
if(X < node->element){
node->left = insertElement(node->left, X);
return node; // add this.
}
else if(X > node->element){
node->right = insertElement(node->right, X);
return node; // add this.
}
```

Similar Questions

Consider you have Binary Search Tree, in case multiply all nodes by -1 then can anybody please let me know if its still a Binary Search Tree? Whether is it possible to write a function which converts

I've been trying to implement a function in C that deletes a node in a binary tree that should (theoretically) take care of three all cases, i.e.: Node is a leaf Node has one child Node has two child

I am writing a program that reads in a student record if you will and then separates it into 4 binary search trees based on the data. I am attempting to delete a node, but instead of actually removi

I am trying to implement a method to create a node to insert into a binary tree (not bst). struct node{ struct node *left; int data; struct node *right; }; typedef struct node *n; void createNewNode()

So, here i have come up with Binary search tree prgram, where i am creating 2 binary trees tmp and tmp2, where i am trying to copy whole tmp2 to tmp, the node which is taken as input from user. But i

I have a homework which ask from me to create a struct of binary search tree where its node of the binary search tree is another binary search tree. The first BST has the Surnames of Students and the

Alrighty, so I wrote up a binary search tree, and I'm putting it up here, the whole code (not like it's hard to find the code for this online anyways...) in the hopes that someone can find my slight e

I have the implemented a binary search tree but I also want to make it generic. The code is the following: typedef struct treeNode { int data; struct treeNode *left; struct treeNode *right; } treeNode

Most of the code is from Weiss' Data structures and algorithm analysis in C++, after I typed the code on CodeBlock(gnu,gcc), compiler reported no error, but then I tried to create an instance and te

I was wondering if we can use a binary search tree to simulate heap operations (insert, find minimum, delete minimum), i.e., use a BST for doing the same job? Are there any kind of benefits for doing

So when i delete in binary search tree, do i need to have like 7 different cases i.e. 1.Left Leaf; 2.Right Leaf; 3.Left child with only left child.//i.e the node to be deleted is the left child of it'

I need to find if the binary tree is a perfect binary tree, meaning that each node has 2 nodes except for the last level. These are the methods I have so far but it doesn't seem to be working and I'm

First off, this is homework, so putting that out there. I'm supposed to implement a binary search tree with specific methods: void insert(String), boolean remove(String), and boolean find (String). I

I have been given two binary search trees. For example, A and B. Next, I was asked to delete the tree B from the tree A. By deletion, I mean delete all the nodes present in B from A. Note: B is not n

I am extremely sorry for being so naive. I am trying to implement a binary search tree in python (part of learning python in a way). I tried to google it, but I am unable to point out where I have fal

Given an arbitrary set of values V and building a tree by inserting values left to right, what does it mean if I'm asked if my orderings of these values (to construct a minimum height and maximum heig

For a given binary tree, find the largest subtree which is also binary search tree? Example: Input: 10 / \ 50 150 / \ / \ 25 75 200 20 / \ / \ / \ / \ 15 35 65 30 120 135 155 250 Output: 50 / \ 25

I'm currently implementing a red-black binary search tree for geometric interval search. I'm saving in the tree segments containing a startpoint and a endpoinit where the startpoint is the key entry t

I'm trying to make a list of all items in a binary search tree. I understand the recursion but I don't know how to make it return each value and then append it into a list. I want to create a function

I'm creating Binary Search Tree class I got all my functions working accept the Successor. My tree input is 30 10 45 38 20 50 25 33 8 12 When I print Inorder traversal it come up as 8 10 12 20 25 30

I am trying to write a remove from a binary tree function. I'm kinda lost so I'm trying to handle it case by case, starting with if the value I'm trying to remove is in the root of the BST. To test my

I am trying to search for a number in a BST and the function is always printing YES (Number was found) Here is the code I have in main printf(Enter a number to search: ); scanf(%d, &num); i

I have a working search function that will search for the exact value in a database, but my goal is for it to search for substrings in the database of the stored data and when searching for substrings

I have a small doubt here... If I know that a search element in a list ,say containing 32 elements sorted in order, is appearing within first four positions, which is the best searching algorithm. Lin

Can anyone please explain the difference between binary tree and binary search tree with an example?

I am trying to figure out how to remove a node from a binary search tree, I understand that it is different for each node whether it is a leaf, has one child or two children. So as of now my function

Does this remove function for a Binary Search Tree look correct? When I try to delete a node, it runs and brings back the switch menu it's called from, but doesn't actually delete it when you reprint

I am working on a function that searches a templated binary search tree for a value with a given key and then returns a pointer to that data value. If the given key doesn't exist in the tree, the poin

If I am attempting to minimize the height of a Binary Search Tree, are these the correct steps?: 1) produce a sorted array from the tree 2) reconstruct the tree by adding the sorted elements into the

I recently wrote a fairly simple piece of code attempting to implement a Binary Search Tree in C with insertion, search, deletion and display operations. Unfortunately, the code does not seem to work.

Introduction I'm building an HTML5 web application that creates a visual representation of a binary search tree from a given list of numbers. Currently, I have an algorithm which calculates the visual

I need help in understanding this interview question: Q: find an algorithm to find the ‘next’ node (e.g., in-order successor) of a given node in a binary search tree where each node has a link to its

So, the question is to find the largest sub-tree (the largest sub-tree is the node with maximum size) in a binary tree which is a BST. I found the following website which enlists an algorithm. http://

I've been stuck on the insertion part of the binary search tree. I get so confused with nested structs. The basic idea of this program is to create a bst that is able to hold names and double values w

i am having a normal binary tree that i am trying to apply iterative deepening depth first search on using c : struct node { int data; struct node * right; struct node * left; }; typedef struct node n

Not sure if I should put this on math stackexchange instead, but oh well. On page 300 of CLRS... Theorem 12.4 The expected height of a randomly built binary search tree on n distinct keys is O(lgn).

I'm currently working on a school project. The assignment is to write the implementation of a templated binary search tree given the corresponding header file. My issue is that since it's templated,

I'm working through the exercises in the book Beginning Haskell. Exercise 4-8 is to make a binary search tree an instance of Functor and define fmap. This is what the tree looks like: data BinaryTre

Hi I have an array list which has some numbers in it like {23,16,45,26,2,5,9} and I want to make a binary search tree with this array list which is array and its elements are objects that has 2 fiel

So I am working with Binary Search Trees and one method I am having trouble with is a method called stickCt, which basically it will count the number of sticks in a tree and return the number. For it

I wrote this recursive function, which works as expected. It validates a binary tree i.e., it checks whether the given binary tree is a binary search tree or not and gives the correct answer too. Howe

So I've looked around a lot for some information on how to take elements from a binary search tree and write them to a file, and then later read them back into the tree as a type of load function. I

I want to go through every item in a dictionary in Java. To clarify what I want to do, this is the C# code: Dictionary<string, Label> LableList = new Dictionary<string, Label>(); foreach (

I am reviewing for my final and one of the practice problem asks to implement a function that puts a value into a binary search tree in Python. Here is the Tree implementation I am using. class Tree(o

I have an assignment in C where im making a binary search tree for words that i read from a file. My problem is that there are some newlines what i dont manage to catch and get rid of, therefore they

Which would take longer? print all items stored in a binary search tree in sorted order or print all items stored in a hash table in sorted order. It would take longer to print the items of a hash ta

The list is sorted. I have a List and I d like to do binary search on it. T has members like StartIndex, EndIndex etc. I can do binary search on the list with StartIndex, ie: I have implemented ICompa

How can I implement a function to delete an element in a binary search tree? This is my tree: data Tree a = Leaf | Node a (Tree a) (Tree a) I know that in case my tree is a Leaf delete :: (Ord a) =&g

I'm working on a program that works with a binary tree. I'm getting an error with adding new nodes into the tree. I can add one node, but after adding another, I get a STATUS_ACCESS_VIOLATION error. I

If I delete node x and then node y or delete y and the x, after this deleted I will stay with the same binary search tree? I tried a few examples and I think that's true. But how can i prove this?