Removing the last node from Complete Binary Tree - c

Every node in a Complete Binary Tree can be identified by its label. In other words, a level order traversal of the CBT means accessing nodes in the ascending order of labels. I wrote a function getPointer to return the node given the Root and the Label. For example in the complete binary tree shown below, the key 5 has the label 4, the key 38 has label 3 and so on.
1
/ \
2 38
/
5
Where I am going wrong in the following approach?
I have node structure.
node
{
rightChild
leftChild
value
label
}
C-styled Pseudocode :
getPointer(root, label)
if(label == 1) return root
else
{
temp_node = getPointer(root,label/2);
child = temp_node->left;
if(label == child->label) return child;
else return temp_node->right;
}

I think your code does not handle following scenario:
1
/ \
2 38
\
5
You can simply apply BFS for this problem.

Shouldn't you be checking if your root is a null ptr. There could be a case when the level is not 1, but you are passing a null ptr to your methods. For example, if during recursion, when the right child is null.

Related

Binary Tree nodes and relations to the parent [duplicate]

This question already has answers here:
C How to "draw" a Binary Tree to the console [closed]
(10 answers)
Closed 3 years ago.
I know how to code a binary search tree, and to traverse it in inorder, preorder or postorder fashion.
But, I want to know how I can know where the actual node is:
For example let's say we have the values: 70 90 50 60 80 40
So the root would be 70, 90 to its right, 50 to its left, 60 to 50's right and so on.
So if I were to print this inorder, it would be 40 50 60 70 80 90, increasing order pretty much.
I do this by doing this in the recursive traverse function:
traverse(root->left);
printf("%d ", root->data);
traverse(root->right);
But this doesn't let me know (as far as I know) where the node currently is.
Is there a way to print the tree like this?:
70
50 (70 L) 90 (70 R) // We know which parent the node is from and we know if it's the left or the right of it.
40 (50 L) 60 (50 R) 80 (90 L)
and so on.. if the tree is bigger.
I don't really know how I'd do it. Would I need to link them to their parents? But how would I know the right-left thing if I do just that? Or do I need to print the child when the iterator is on the parent still. Thank you in advance.
Edit: I looked up printing the tree by traversing level by level but I don't think I get to know the parent and the side of the node.. I am trying to implement some codes to c code and I always get crashes.
Seems you want to print the tree level by level, i.e.
level 0: node0
level 1: node1 (node0 L) node2 (node0 R)
level 2: ...
So you need a way to track the current level and the target level (i.e. the one you want to print). Also you need a way to track when there is "no more levels" (i.e. no more to print).
That can be done in many ways. The pseudo code below should give you an idea of one way to do it. It's not the most efficient way but it's pretty simple.
Something like:
bool btPrintLevel(node* root, int targetlevel, int currentlevel)
{
if (targetlevel == currentlevel) return false; // no need to go further down in levels
bool result = false;
result |= btPrintLevel(root->left, targetlevel, currentlevel + 1);
// psedo code
if ((currentlevel + 1 == targetlevel)
{
if (childern_exists)
{
printChildern(...)
result = true;
}
}
result |= btPrintLevel(root->right, targetlevel, currentlevel + 1);
return result;
}
Called like:
int level = 0;
while(btPrintLevel(head, level)) ++level;
This How to print elements from Binary Tree by level in c may also help. It doesn't do exactly what you want but with minor modifications you can achieve your goal.
Recursive algorithms are fine for depth-first traversing of trees, but less suitable for broadth-first. The latter can be done pretty efficiently using a FIFO (pseudo code):
push(fifo, { root, 0 });
while(!isEmpty(fifo))
{
node* n = pop(fifo);
print(n);
if(n->left)
push(fifo, { n->left, 'L' });
if(n->right)
push(fifo, { n->right, 'R'});
}
I've foreseen an additional field designating being left or right child of parent. If you your nodes have a link to their parent, you can instead decide upon n->parent->left == n ? 'L' : 'R'.
Leaving implementing the FIFO up to you (you'll find quite a number of questions here on SO about)...

C - How to traverse a Huffman Tree and generate corresponding Huffman codes?

I'm currently working on a program that reads in text from standard input, creates a Huffman tree from the data, and then generates the Huffman codes for the symbols in the Huffman tree.
I'm able to create the Huffman tree, which consists of Tree Node structs that contain the symbol, the frequency of the symbol, the Huffman code for the symbol, and pointers to the let and right nodes. However, I'm kind of stuck on how to traverse the tree and modify each node's "code" according to where it is in the tree.
Essentially what I'm asking is how to traverse a binary tree while modifying a node's elements based on where it is in the tree.
Originally I tried something along the lines of:
void generateCodes (struct HuffTreeNode* root, unsigned long bitShift)
{
struct HuffTreeNode* current = root;
if (root != NULL)
{
current->code |= 0<<bitShift;
generateCodes(root->left,bitShift+1);
current->code |= 1<<bitShift;
generateCodes(root->right,bitShift+1);
}
}
However, that didn't work, and I'm kind of at a loss for what to try next. Any ideas?
You have formed the tree. SO now remains the traversing.
17
/\
/ 7
/ /\
10 / 3
/\ C4 /\
A6 B4 D1 E2
Now initilaly you just put them( the 0's or 1's) in array.Left child link - 1 right child link-0
So in the array you put a[currrent]=1 then call huffmancode(tree->left,current+1)
OR if going to right subtree a[current]=0 then call huffmancode(tree->right,current+1).
At reaching the leaf node print array.
Pseudocode: (printtree)
if (root->left exists)
{
a[current] = 0;
printtree(root->left, a, current + 1);
}
if (root->right exists)
{
a[current] = 1;
printtree(root->right, a,current + 1);
}
if (root is leaf)
{
print a[].
}
I assume that your tree is correctly (and optimally) built.
Each Huffman code is a path from the root to a leaf defining a symbol. You add a 0 when you traverse a left arc and a 1 when you traverse a right arc.
So, a way for getting a list of all the pairs symbol,code consists in traversing recursively the tree. You maintain the current path from the root to the current visited node and when you reach a leaf you generate the pair symbol (which would be stored in the leaf) and the current path.
A data structure for managing the current path is a stack.
So, an algorithm could be as follows:
void codes(struct Node * root, struct Stack * s)
{
if (is_leaf(root))
{
char symbol = // symbol stored in the node
char * code = // the reversed stack content; that is the code of current leaf
printf("symbol %c has code %s\n", symbol, code);
}
s->push('0');
codes(LLINK(root), s);
s->pop();
s->push('1');
codes(RLINK(root), s);
s->pop();
}
I leave to you the details of implementation of the stack and other meta-parts (is_leaf(), LLINK(root), ...).
You can improve the algorithm to generate bits
I hope will be helpful

Search and Inserting in a binary tree recursively

first of all happy new year. I was trying to fix a piece of code I've been stumped on for hours. (Note: I am not a solid coder.)
What I am trying to do is to write a function "searchInsert", which will take in a binary tree and some integer i. It will then try to find the integer i in the tree. If it's not there it is inserted into the tree.
Other information: If we do in fact find the integer in the tree, return a pointer pointing to the node of to it. If we do not find it as we said before, insert it in BUT return a pointer pointing to the root of the tree.
I also must do this recursively.
Now I have tested it using an arbitrary tree along with i = 98, as follows:
Before what it looks like.
4
/ \
2 6
/ \ / \
1 3 5 7
After, what it should look like:
4
/ \
2 6
/ \ / \
1 3 5 7
\
98
But my code doesn't seem to be working.
treelink searchInsert(treelink t, TreeItem i){
treelink keyNode = NULL;
if (t == NULL) {
t = insertTreeNode(t, i);
} else if(i < t->item){
keyNode = searchInsert(t->left,i);
} else if(i > t->item){
keyNode = searchInsert(t->right,i);
} else {
keyNode = t;
return keyNode;
}
return t;
}
Other important notes: treelink is a pointer to a binary tree. Assume insertTreeNode works as it was a function given to us.
Any help would be appreciated thanks.
Among other problems, you have lost all context when you realize that you have not found the item that you are looking for:
if ( t == NULL ) {
t = insertTreeNode(t, i) ;
}
So you are always calling insertTreeNode with NULL as the first argument.
While recursion is a great way to step through a tree, you might instead want to create a pointer and iterate through the tree, so that you have the original t around when you decide to call insert.
{
treelink ptr= t ;
while ( ptr )
{
if ( ptr-> item == i ) return ptr ;
ptr= ( ptr-> item > i ) ? ptr-> left : ptr-> right ;
}
return insertTreeNode( t, i ) ;
}
A new node is created, but it is not linked to the tree. You never change your left and right pointers.
what you need is to update the link after recursive call, e.g.:
else if (i < t->item) {
t->left = searchInsert(t->left, i);
} ...
But then of course you cannot simply return a pointer to a found item if it's found, otherwise it would break the tree. That's because the statement of your task is NOT recursive: you have to return either root or an existing (inner) node. So you might want to write a recursive function which e.g. always returns a pointer to the root, but also returns a pointer to a found item (through an additional treelink* argument).
Or maybe it would be simpler to split the function into two: search which returns a pointer to an existing node, and insert which returns a pointer to the root. Both of them would be recursive and quite simple.

Implementing a deletion function for a binary search tree in C

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 children
Is there a way to handle the whole deletion function without checking separately each case? As a commenter below noted I do check for a lot of cases and perhaps the whole problem can be addressed recursively by checking for one fundamental case.
I'm particularly interested in the case where I delete a node within the tree that has a parent and itself is a parent of two children nodes.
Both answers below have been useful but I don't think they address the problem in its entirety.
Here's what I have:
typedef struct Node
{
int key;
int data;
struct Node *left;
struct Node *right;
struct Node *parent;
} Node;
/* functions that take care of inserting and finding a node and also traversing and freeing the tree */
...
void delete(Node *root, int key)
{
Node *target = find(root, key); // find will return the node to be deleted
Node *parent = target->parent; // parent of node to be deleted
// no children
if (target->left == NULL && target->right == NULL)
{
// is it a right child
if (target->key > parent->key)
parent->right = NULL;
// must be a left child
else
parent->left = NULL;
free(target);
}
// one child
else if ((target->left == NULL && target->right != NULL) || (target->left != NULL && target->right == NULL))
{
// here we swap the target and the child of that target, then delete the target
Node *child = (target->left == NULL) ? target->right : target->left;
child->parent = parent;
if (parent->left == target) parent->left = child;
else if (parent->right == target) parent->right = child;
free(target);
}
// two children
else
{
// find the largest node in the left subtree, this will be the node
// that will take the place of the node to be deleted
Node *toBeRepl = max(target->left);
// assign the data of the second largest node
target->key = toBeRepl->key;
target->data = toBeRepl->data;
// if new node immediately to the left of target
if (toBeRepl == target->left)
{
target->left = toBeRepl->left;
Node *newLeft = target->left;
if (newLeft != NULL) newLeft->parent = target;
}
else
{
delete(target->left, toBeRepl->key);
// Node *replParent = toBeRepl->parent;
// replParent->right = NULL;
}
}
I would greatly appreciate your feedback.
edit: Just to clarify, I'm trying to delete a particular node without touching its subtrees (if there are any). They should remain intact (which I've handled by swapping the values of the node to be deleted and (depending on the case) one of the nodes of its substrees).
edit: I've used as a reference the following wikipedia article:
http://en.wikipedia.org/wiki/Binary_search_tree#Deletion
Which is where I got the idea for swapping the nodes values in case of two children, particularly the quote:
Call the node to be deleted N. Do not delete N. Instead, choose either
its in-order successor node or its in-order predecessor node, R.
Replace the value of N with the value of R, then delete R.
There is some interesting code in C++ there for the above case, however I'm not sure how exactly the swap happens:
else //2 children
{
temp = ptr->RightChild;
Node<T> *parent = nullptr;
while(temp->LeftChild!=nullptr)
{
parent = temp;
temp = temp->LeftChild;
}
ptr->data = temp->data;
if (parent!=nullptr)
Delete(temp,temp->data);
else
Delete(ptr->rightChild,ptr->RightChild->data);
}
Could somebody please explain what's going on in that section? I'm assuming that the recursion is of a similar approach as to the users comments' here.
I don't see any "inelegance" in the code, such formatting and commented code is hard to come by. But yes, you could reduce the if-else constructs in your delete function to just one case. If you look at the most abstract idea of what deletion is doing you'll notice all the cases basically boil down to just the last case (of deleting a node with two children).
You'll just have to add a few lines in it. Like after toBeRepl = max(left-sub-tree), check if it's NULL and if it is then toBeRepl = min(right-sub-tree).
So, Case 1 (No children): Assuming your max() method is correctly implemented, it'll return NULL as the rightmost element on the left sub-tree, so will min() on the right sub-tree. Replace your target with the toBeRepl, and you'll have deleted your node.
Case 2 (One child): If max() does return NULL, min() won't, or vice-versa. So you'll have a non-NULL toBeRepl. Again replace your target with this new toBeRepl, and you're done.
Case 3 (Two children): Same as Case 2, only you can be sure max() won't return NULL.
Therefore your entire delete() function would boil down to just the last else statement (with a few changes). Something on the lines of:
Node *toBeRepl = max(target->left);
if toBeRepl is NULL
{
toBeRepl = min(target->right);
}
if toBeRepl is not NULL
{
target->key = tobeRepl->key;
target->data = toBeRepl->data;
deallocate(toBeRepl); // deallocate would be a free(ptr) followed by setting ptr to NULL
}
else
{
deallocate(target);
}
I would do it using recursion, assuming that you have null at the end of your tree, finding null would be the 'go back' or return condition.
One possible algorithm would be:
Node* delete(Node *aNode){
if(aNode->right != NULL)
delete(aNode->right);
if(aNode->left != NULL)
delete(aNode->left);
//Here you're sure that the actual node is the last one
//So free it!
free(aNode);
//and, for the father to know that you're now empty, must return null
return NULL;
}
It has some bugs, for sure, but is the main idea.
This implementation is dfs like.
Hope this helps.
[EDIT] Node *aNode fixed. Forgot the star, my bad.
I finished this a long time ago and I thought it would be good to add a sample answer for people coming here with the same problem (considering the 400+ views this question has accumulated):
/* two children */
else
{
/* find the largest node in the left subtree (the source), this will be the node
* that will take the place of the node to be deleted */
Node* source = max(target->left);
/* assign the data of that node to the one we originally intended to delete */
target->key = source->key;
target->data = source->data;
/* delete the source */
delete(target->left, source->key);
}
Wikipedia has an excellent article that inspired this code.

Deletion in binary search tree

So when I delete in binary search tree, do I need to have like 7 different cases i.e.
Left Leaf;
Right Leaf;
Left child with only left child. //i.e the node to be deleted is the left child of it's parent and it has only left child.
Left Child with only right child.
Right child with only left child.
Right child with only right child.
Node to be deleted has both the children i.e. right and left.
Now when this code is using if-else it gets pretty nasty.. is there any other way of doing this.
Here is my code snippet
if(current->left==NULL && current->right==NULL && current->key<prev->key) //left leaf
prev->left=NULL;
else if(current->left==NULL && current->right==NULL && current->key>prev->key) // right leaf
prev->right=NULL;
else if(current->left!=NULL && current->right==NULL && current->key<prev->key) // left child with one child
prev->left=current->left;
else if(current->left==NULL && current->right!=NULL && current->key<prev->key)
prev->left=current->right;
else if(current->left!=NULL && current->right==NULL && current->key>prev->key)
prev->right=current->left;
else if(current->left==NULL && current->right!=NULL && current->key>prev->key)
prev->right=current->left;
else if(current->left!=NULL && current->right!=NULL)
{
check=current->right;
check1=check;
while(check->left!=NULL)
{
check1=check;
check=check->left;
}
*current=*check;
check1->left=NULL;
}
You can keep it a lot simpler than that, and simply restrict yourself to three cases when deleting a node from a BST (binary search tree) :
a node without children (a leaf) : just remove it - nothing special needs to be done
a node with one child : remove it, and move the child in its place
a node with two children : swap it with either its in-order predecessor or successor, and then remove it
The wiki page contains an example of how this could look in code.
Or as a very basic example in C :
if (current->left==NULL && current->right==NULL) {
/* leaf node */
bst_replace(current, NULL);
}
else if (current->left==NULL || current->right==NULL) {
/* node with one child */
bst_replace(current, ((current->left) ? current->left : current->right));
}
else {
/* node with two children */
Node* successor = bst_next(current);
current->data = successor->data;
bst_replace(successor, successor->right);
}
I don't really understand the protocol used for deleting here. You seem to not have a binary 'search' tree (no ordering in the tree).
But to just make the code simple. You could do something like this:
bool b1 = (current->left == NULL);
bool b2 = (current->right == NULL);
bool b3 = (current->key > prev->key);
int decision_case = b1 * 4 + b2 * 2 + b3;
switch(decision_case) {
case 0: // fill in code here
break;
...
...
case 7: // fill in code here
break;
}
Also, you should use delete to avoid memory leaks here.
Hope that helps.
Deleting a NULL pointer has no ill effect. So, you should be able to do this with no special cases. The basic part is just:
delete current->left;
delete current->right;

Resources