Starting Out with C++ from Control Structures to Objects (8th Edition)
8th Edition
ISBN: 9780133769395
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 20, Problem 9RQE
A node without child is referred a “leaf node”.
Program Plan Intro
Binary tree:
A complete binary tree is a binary tree with the property that every node must have exactly two children, and at the last level the nodes should be from left to right.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Fill-in-the-Blank
In a(n) __________ list, each node has a pointer to the one before it and the one after it.
#ifndef BT_NODE_H#define BT_NODE_H
struct btNode{ int data; btNode* left; btNode* right;};
// pre: bst_root is root pointer of a binary search tree (may be 0 for// empty tree) and portArray has the base address of an array large// enough to hold all the data items in the binary search tree// post: The binary search tree has been traversed in-order and the data// values are written (as they are encountered) to portArray in// increasing positional order starting from the first elementvoid portToArrayInOrder(btNode* bst_root, int* portArray);void portToArrayInOrderAux(btNode* bst_root, int* portArray, int& portIndex);
// pre: (none)// post: dynamic memory of all the nodes of the tree rooted at root has been// freed up (returned back to heap/freestore) and the tree is now empty// (root pointer contains the null address)void tree_clear(btNode*& root);
// pre: (none)// post: # of nodes contained in tree rooted at root is returnedint…
Assume the tree node structure is following........
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node *root = null;
and there is a created new node function, called newnode(int new_data).
Please filled the Blank of Insertion function.
void insert(struct node *root, int key) {
struct node *current;
queue q;
q.enque(root);
while(!q.empty()
}
current = q.front();
q.deque();
if(current->left == NULL) {
break;
}
else
}
q.enque(
if(current->right == NULL) {
break;
else
q.enque(_
= newnode(key);
= newnode(key);
_-));
Chapter 20 Solutions
Starting Out with C++ from Control Structures to Objects (8th Edition)
Ch. 20.1 - Prob. 21.1CPCh. 20.1 - Prob. 21.2CPCh. 20.1 - Prob. 21.3CPCh. 20.1 - Prob. 21.4CPCh. 20.1 - Prob. 21.5CPCh. 20.1 - Prob. 21.6CPCh. 20.2 - Prob. 21.7CPCh. 20.2 - Prob. 21.8CPCh. 20.2 - Prob. 21.9CPCh. 20.2 - Prob. 21.10CP
Ch. 20.2 - Prob. 21.11CPCh. 20.2 - Prob. 21.12CPCh. 20 - Prob. 1RQECh. 20 - Prob. 2RQECh. 20 - Prob. 3RQECh. 20 - Prob. 4RQECh. 20 - Prob. 5RQECh. 20 - Prob. 6RQECh. 20 - Prob. 7RQECh. 20 - Prob. 8RQECh. 20 - Prob. 9RQECh. 20 - Prob. 10RQECh. 20 - Prob. 11RQECh. 20 - Prob. 12RQECh. 20 - Prob. 13RQECh. 20 - Prob. 14RQECh. 20 - Prob. 15RQECh. 20 - Prob. 16RQECh. 20 - Prob. 17RQECh. 20 - Prob. 18RQECh. 20 - Prob. 19RQECh. 20 - Prob. 20RQECh. 20 - Prob. 21RQECh. 20 - Prob. 22RQECh. 20 - Prob. 23RQECh. 20 - Prob. 24RQECh. 20 - Prob. 25RQECh. 20 - Prob. 1PCCh. 20 - Prob. 2PCCh. 20 - Prob. 3PCCh. 20 - Prob. 4PCCh. 20 - Prob. 5PCCh. 20 - Prob. 6PCCh. 20 - Prob. 7PCCh. 20 - Prob. 8PC
Knowledge Booster
Similar questions
- Computer Science lab3.h ------------- #include<stdio.h> #include<stdlib.h> #ifndef LAB3_H #define LAB3_H // A linked list node struct Node { int data; //Data struct Node *next; // Address to the next node }; //initialize: create an empty head node (whose "data" is intentionally missing); This head node will not be used to store any data; struct Node *init () { //create head node struct Node *head = (struct Node*)malloc(sizeof(struct Node)); } //Create a new node to store data and insert it to the end of current linked list; the head node will still be empty and data in the array in "main.c" are not stored in head node void insert(struct node *head, int data) { struct Node *newNode = (struct Node*)malloc(sizeof(struct Node)); new_node->data = data; new_node->next= head; } //print data for all nodes in the linked list except the head node (which is empty) void display (struct Node *head) { struct Node *current_node = head; while ( current_node != NULL) { printf("%d ",…arrow_forwardclass Graph: def __init__(self, num_nodes): self.num_nodes = num_nodes # Initialize a 2D matrix with zeros self.adj_matrix = [[0] * num_nodes for _ in range(num_nodes)] def add_edge(self, node1, node2): # Add an undirected edge between node1 and node2 self.adj_matrix[node1][node2] = 1 self.adj_matrix[node2][node1] = 1 def remove_edge(self, node1, node2): # Remove the edge between node1 and node2 self.adj_matrix[node1][node2] = 0 self.adj_matrix[node2][node1] = 0 def num_edges(self): num_edges = 1 for i in range(self.num_nodes): for j in range(i+1, self.num_nodes): if self.adj_matrix[i][j] == 1: num_edges += 1 return num_edges def num_edges_in_set(self, node, subset): # count the number of edges joining the given node and nodes in subset. # example input: 1, {0, 2} num_edges = 0 for s in subset: if…arrow_forwardtypedef struct node{int data;struct node *left,*right;}BST;The node structure of BST is shown above. Please design an efficient algorithm to return the maximum keyword value in BST. Completion function: int findmax (BST *T)arrow_forward
- Graphs: Depth First Traversal Starting with the same graph program as last assignment, implement a depth first traversal method. Test iy on nodes 1, 2, and 3 as start nodes. Graph program: #include <iostream>#include <vector>#include <string>using namespace std; class Edge;//-------------------------------------------------------------////class Node{public:Node(string iname){name = iname;}string name;int in_count = 0;bool visited = false; vector<Edge *> out_edge_list;};//-------------------------------------------------------------////class Edge{public:Edge(string iname, double iweight, Node *ifrom, Node *ito){name = iname;weight = iweight;from = ifrom;to = ito;} string name;double weight;Node *from;Node *to;bool visited = false;}; //-------------------------------------------------------------////class Graph{public:vector<Node *> node_list;vector<Edge *> edge_list; //----------------------------------------------------------//Node*…arrow_forwardDouble pointers: Describe how this operation can be done in O(1) time if there are pointers in each node to both the previous and the next node.arrow_forward#ifndef EXTENDEDAVLNODE_H #define EXTENDEDAVLNODE_H #include "AVLNode.h" #include <iostream> class ExtendedAVLNode : public AVLNode { private: int subtreeKeyCount; public: ExtendedAVLNode(int nodeKey) : AVLNode(nodeKey) { subtreeKeyCount = 1; std::cout << "Node created with key: " << nodeKey << std::endl; } int GetSubtreeKeyCount() { return subtreeKeyCount; } void UpdateSubtreeKeyCount() { int leftCount = GetLeft() ? ((ExtendedAVLNode*)GetLeft())->GetSubtreeKeyCount() : 0; int rightCount = GetRight() ? ((ExtendedAVLNode*)GetRight())->GetSubtreeKeyCount() : 0; subtreeKeyCount = 1 + leftCount + rightCount; std::cout << "Updated subtreeKeyCount for node " << GetKey() << ": " << subtreeKeyCount << std::endl; } virtual void SetLeft(BSTNode* newLeftChild) override { std::cout << "Setting left child for node " << GetKey() <<…arrow_forward
- Given Code: #include<iostream>#include<queue> struct treeNode{ char dataItem; struct treeNode *firstChild; struct treeNode *nextSibling;}; /* Preorder Traversalpreorder (v)visit(v)for each child w of vpreorder(w)*/void preorder(treeNode *currNode){ if(currNode != NULL){ std::cout << currNode->dataItem << " "; preorder(currNode->firstChild); preorder(currNode->nextSibling); } return;} /* Postorder Traversalpostorder (v)for each child w of vpostOrder(w)visit(v)*/void postorder(treeNode *currNode){ if(currNode != NULL){ preorder(currNode->firstChild); preorder(currNode->nextSibling); std::cout << currNode->dataItem << " "; } return;} /*Inorder Traversalif(v==NULL) returninOrder(v.left)visit(v)inOrder(v.right)*/void inorder(treeNode *currNode){ if(currNode != NULL){ inorder(currNode->firstChild); std::cout << currNode->dataItem…arrow_forwardCO LL * Question Completion Status: QUESTION 3 Write a recursive function, OnlyChild(..), that returns the number of nodes in a binary tree that has only one child. Consider binaryTreeNode structure is defined as the following. struct binaryTreeNode int info; binaryTreeNode *llink: binaryTreeNode *rlink; The function is declared as the following. You must write the function as a recursive function. You will not get any credits if a non-recursive solution is used. int OnlyChild(binaryTreeNode *p); For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Paragraph Arial 10pt B. ^三へ三 三山 三Ex? X2 = E E E 9 Click Save and Submit to save and submit. Click Save All Answers to save all ansuwers. Is E English (United States) Focus || 15 stv MacBook Air D00 O00 F4 F5 F8 64arrow_forwardQ5_3\ Represent the following tree by using array.arrow_forward
- 12:34 A cs61a.org Implement add_d_leaves, a function that takes in a Tree instance t and a number v. We define the depth of a node in t to be the number of edges from the root to that node. The depth of root is therefore 0. For each node in the tree, you should add d leaves to it, where d is the depth of the node. Every added leaf should have a label of v. If the node at this depth has existing branches, you should add these leaves to the end of that list of branches. For example, you should be adding 1 leaf with label v to each node at depth 1, 2 leaves to each node at depth 2, and so on. Here is an example of a tree t (shown on the left) and the result after add_d_leaves is applied with v as 5. 3 2 3 2 4 4 5 5 5 Hint: Use a helper function to keep track of the depth! def add_d_leaves(t, v): """Add d leaves containing v to each ngarrow_forwardFind the errors in the program then correct them. CODE: #include <bits/stdc++.h>using namespace std; /* A binary tree node has key, pointer to leftchild and a pointer to right child */struct Node { int key; struct Node *left, *right}; /* function to create a new node of tree andreturn pointer */struct Node* newNode(int key){ struct Node* temp = new node; temp->key = key; temp->left = temp->right = NULL; return temp;}; /* Inorder traversal of a binary tree*/void inorder(struct Node* temp){ if (!temp) return 0 inorder(temp->left); cout << temp->key << " "; inorder(temp->right)} /* function to delete the given deepest node(d_node) in binary tree */void deletDeepest(struct Node* root, struct Node* d_node){ queue<struct Node*> q q.push(root); // Do level order traversal until last node struct Node* temp; while (!q.empty()) { temp = q.front(); q.pop(); if (temp…arrow_forwardTask: Complete the function getMinDepth (Node *root), the function takes the root of a tree and returns the minimum depth of the tree. int getMinDepth(Node *root){ //write your code here } Constraints: The number of nodes in the tree is in the range [0, 100000]. -1000 <= Node.val <= 1000arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning