answersLogoWhite

0


Best Answer

Use a recursive function.

void delete_node (node* n) {

/* ensure the given pointer is valid */

if (n==NULL) return;

/* recursively delete the left child */

delete_node (n->left);

n->left = NULL;

/* recursively delete the right child */

delete_node (n->right);

n->right = NULL;

/* delete the data (assuming the data is a pointer)*/

free (n->data);

n->data = NULL;

/* finally, delete the node itself */

free (n);

n=NULL;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you delete a node with two children in binary tree using c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is parent node?

In programming, a parent node is any node that holds some reference to a child node. The child node may itself be a parent node. A classic example of parent/child node usage is the binary tree, where every node in the tree may hold a reference to up to 2 children. Nodes that have no children are known as leaf nodes.


Is null node equal to leaf node?

No. A leaf node is a node that has no child nodes. A null node is a node pointer that points to the null address (address zero). Since a leaf node has no children, its child nodes are null nodes.


What is a binary tree?

A binary tree is a data structure consisting of binary nodes. A binary node is a data structure with two branches, each of which may hold a reference to another binary node. These branches are known as the left and right branches respectively. Since the nodes maintain references to every other node in the tree, it is only necessary to keep track of the root node.


What is the difference between extended binary tree and a binary search tree?

A strictly binary tree is one where every node other than the leaves has exactly 2 child nodes. Such trees are also known as 2-trees or full binary trees. An extended binary tree is a tree that has been transformed into a full binary tree. This transformation is achieved by inserting special "external" nodes such that every "internal" node has exactly two children.


Algoritm for deleting the last element from a list?

Given a list and a node to delete, use the following algorithm: // Are we deleting the head node? if (node == list.head) { // Yes -- assign its next node as the new head list.head = node.next } else // The node is not the head node { // Point to the head node prev = list.head // Traverse the list to locate the node that comes immediately before the one we want to delete while (prev.next != node) { prev = prev.next; } end while // Assign the node's next node to the previous node's next node prev.next = node.next; } end if // Before deleting the node, reset its next node node.next = null; // Now delete the node. delete node;

Related questions

explain the process of converting tree to binary tree with example?

A binary tree is a type of tree data structure in which each node has at most two children. To convert a tree to a binary tree, we can follow these steps: Choose a root node for the binary tree. This will be the node at the top of the tree, and all other nodes will be connected to it. For each child node of the root node, add it as a left or right child of the root node, depending on its position relative to the root node. For each child node of the root node, repeat step 2 for its child nodes, adding them as left or right children of the appropriate parent node.


What is parent node?

In programming, a parent node is any node that holds some reference to a child node. The child node may itself be a parent node. A classic example of parent/child node usage is the binary tree, where every node in the tree may hold a reference to up to 2 children. Nodes that have no children are known as leaf nodes.


Is null node equal to leaf node?

No. A leaf node is a node that has no child nodes. A null node is a node pointer that points to the null address (address zero). Since a leaf node has no children, its child nodes are null nodes.


What is a binary tree?

A binary tree is a data structure consisting of binary nodes. A binary node is a data structure with two branches, each of which may hold a reference to another binary node. These branches are known as the left and right branches respectively. Since the nodes maintain references to every other node in the tree, it is only necessary to keep track of the root node.


What is the difference between extended binary tree and a binary search tree?

A strictly binary tree is one where every node other than the leaves has exactly 2 child nodes. Such trees are also known as 2-trees or full binary trees. An extended binary tree is a tree that has been transformed into a full binary tree. This transformation is achieved by inserting special "external" nodes such that every "internal" node has exactly two children.


Algoritm for deleting the last element from a list?

Given a list and a node to delete, use the following algorithm: // Are we deleting the head node? if (node == list.head) { // Yes -- assign its next node as the new head list.head = node.next } else // The node is not the head node { // Point to the head node prev = list.head // Traverse the list to locate the node that comes immediately before the one we want to delete while (prev.next != node) { prev = prev.next; } end while // Assign the node's next node to the previous node's next node prev.next = node.next; } end if // Before deleting the node, reset its next node node.next = null; // Now delete the node. delete node;


What is strictly binary tree?

If every non-terminal node (any node except root node whose degree is not zero) in a binary tree consists of non-empty left and right subtree, then such a tree is called strictly binary tree.


In binary tree what is the name given to node that share the same parent node?

Sibling.


What is the difference between binary tree and tree data structure?

binary tree is a specific tree data structure where each node can have at most 2 children nodes. In a general Tree data structure nodes can have infinite children nodes.


What is the difference between avl tree and binary tree?

A binary tree is a tree data structure in which each node has at most two children. Typically the child nodes are called left and right. One common use of binary trees is binary search trees; another is binary heaps. A binary search tree (BST) is a binary tree data structure which has the following properties: ->each node has a value; ->a total order is defined on these values; ->the left subtree of a node contains only values less than the node's value; ->the right subtree of a node contains only values greater than or equal to the node's value. An AVL tree is a self-balancing binary search tree. In an AVL tree the heights of the two child subtrees of any node differ by at most one, therefore it is also called height-balanced. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases. Additions and deletions may require the tree to be rebalanced by one or more tree rotations.


General tree to binary tree conversion?

The process of converting the general tree to a binary tree is as follows: * use the root of the general tree as the root of the binary tree * determine the first child of the root. This is the leftmost node in the general tree at the next level * insert this node. The child reference of the parent node refers to this node * continue finding the first child of each parent node and insert it below the parent node with the child reference of the parent to this node. * when no more first children exist in the path just used, move back to the parent of the last node entered and repeat the above process. In other words, determine the first sibling of the last node entered. * complete the tree for all nodes. In order to locate where the node fits you must search for the first child at that level and then follow the sibling references to a nil where the next sibling can be inserted. The children of any sibling node can be inserted by locating the parent and then inserting the first child. Then the above process is repeated.


How do you delete node from acircular linked list?

Three steps for deleting a node from a linked list: 1) set currentNode->prev->next to currentNode->next (i.e. the previous node's next pointer should be the current node's next pointer). 2) set currentNode->next->prev to currentNode->prev (i.e. the next node's previous pointer should be the current node's previous pointer). 3) Free the memory used by currentNode (using delete, for example).