Need help writing this code in C programming code: ?Binary Tree Notes/Pseudo Cod
ID: 3718079 • Letter: N
Question
Need help writing this code in C programming code:
?Binary Tree Notes/Pseudo Code: "Binary Tree: A binary tree is either empty or consists of a single vertex and two disjoint subsets each of which is also a binary tree. Structure of a Binary Tree: typedef struct treenode { int data; struct treenode *left, *right; } *binarytree; Declaring a binary tree: binarytree root; Initialize a binary tree: use pass by reference set root to NULL Checking for empty tree: use pass by value if root is NULL, then tree is empty Checking for full tree: use pass by value can we allocate memory the size of a treenode structure? Adding to a tree: assuming tree is NOT full use pass by reference if current position is empty allocate memory the size of a tree node structure put data in it set both left and right children to NULL otherwise (keep looking for empty spot) if data to add is <= current node's data use recursive call to add to left subtree... add(&(*t)->left, datatoadd) otherwise use recursive call to add to right subtree
Deleting from a tree: assuming tree is NOT empty and user has specified data to be removed use pass by reference to send tree and pass by value to send data to be removed if not empty if current node is data we are looking for: if no children set temp to (*t) set (*t) to NULL free temp if left child only set temp to (*t) move (*t) to left child free temp if right child only set temp to (*t) move (*t) to right child free temp if two children EITHER (attach left child to left most pointer of right subtree) set temp to (*t)-> right while temp->left != NULL move temp to left subtree set temp->left to (*t)->left move temp back to (*t) move (*t) to (*t)->right free temp OR (attach right child to right most pointer of left subtree set temp to (*t)->left while temp->right != NULL move temp to right subtree set temp->right to (*t)->right move temp back to (*t) move (*t) to (*t)->left free temp otherwise if data we are looking for is <= current data call delete recursively sending left subtree and data to remove otherwise call delete recursively sending right subtree and data to remove otherwise data not in the tree (reached bottom without finding it)
Printing the contents of the binary tree: Assuming you've already checked to make sure the tree isn't empty or printed the appropriate message in main, you will pass the tree using pass-by-value since no changes will be made. LNR (Left, Node, Right) / Ascending Order if the tree is not empty call LNR recursively sending the left subtree print the data portion of the current node call LNR recursively sending the right subtree
RNL (Right, Node, Left) / Descending Order if the tree is not empty call RNL recursively sending the right subtree print the data portion of the current node call RNL recursively sending the left subtree?"
Explanation / Answer
try running this code hope it helps
struct node
{
int data;
struct node *left;
struct node *right;
};
/* newNode() allocates a new node with the given data and NULL left and
right pointers. */
struct node* newNode(int data)
{
// Allocate memory for new node
struct node* node = (struct node*)malloc(sizeof(struct node));
// Assign data to this node
node->data = data;
// Initialize left and right children as NULL
node->left = NULL;
node->right = NULL;
return(node);
}
int main()
{
/*create root*/
struct node *root = newNode(10);
/* following is the tree after above statement
10
/
NULL NULL
*/
root->left = newNode(20);
root->right = newNode(30);
/* 20 and 30 become left and right children of 1
1
/
20 30
/ /
NULL NULL NULL NULL
*/
root->left->left = newNode(40);
/* 4 becomes left child of 2
10
/
20 30
/ /
40 NULL NULL NULL
/
NULL NULL
*/
getchar();
return 0;
}
*****************************callling LNR recursively****************************************
void ascending(struct node* node)
{
if(node==NULL)
return;
else{
inorder(node->left);
printf("%d",node->data);
inorder(node-right);
}
}
************************************************************************************
void descending(struct node* node)
{
if(node==NULL)
return;
else{
descending(node-right);
printf("%d",node->data);
descending(node->left);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.