Write the following functions: a) The function gets a root of a Binary Tree of ints, and a boolean predicate pred. It returns a pointer to a vertex v for which the data satisfies pred, i.e. pred(v->>data)==true. if no such vertex is not found, the function returns -1. If there are several such vertices, the function returns the first such v in pre-order .

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write the following functions:

a) The function gets a root of a Binary Tree of ints, and a boolean predicate pred. It returns a pointer to a vertex v for which the data satisfies pred, i.e. pred(v->>data)==true. if no such vertex is not found, the function returns -1. If there are several such vertices, the function returns the first such v in pre-order .

// finds the first node in pre-order satisfying pred

// if such node not found, returns NULL

BTnode_t* find(const BTnode_t* root, bool (*pred)(int));

struct BTnode {
int value;
struct BTnode*left;
struct BTnode*right;
struct BTnode*parent;
};

typedef struct BTnode BTnode_t;

BTNODE.C:

BTnode_t* create_node(int val) {
BTnode_t* newNode = (BTnode_t*) malloc(sizeof(BTnode_t));
newNode->value = val;
newNode->left = NULL;
newNode->right = NULL;
newNode->parent = NULL;

returnnewNode;
}

void set_left_child(BTnode_t* parent, BTnode_t* left_child) {
if (parent)
parent->left = left_child;
if (left_child)
left_child->parent = parent;
}

void set_right_child(BTnode_t* parent, BTnode_t* right_child) {
if (parent)
parent->right = right_child;
if (right_child)
right_child->parent = parent;
}

void print_pre_order(BTnode_t* root) {
if (root == NULL)
return;

printf("%d ", root->value);
print_pre_order(root->left);
print_pre_order(root->right);
}

void print_in_order(BTnode_t* root) {
if (root == NULL)
return;

print_in_order(root->left);
printf("%d ", root->value);
print_in_order(root->right);
}

void print_post_order(BTnode_t* root) {
if (root == NULL)
return;

print_post_order(root->left);
print_post_order(root->right);
printf("%d ", root->value);
}

test for the function:

bool test_q3find() {
/***
// creates the following tree
// 1
// / \
// 2 3
****/
BTnode_t* one = create_node(1);
BTnode_t* two = create_node(2);
BTnode_t* three = create_node(3);
set_left_child(one, two);
set_right_child(one, three);
 
BTnode_t* v = find(one, is_odd);
if (v == one) {
printf("Q3-find ok\n");
returntrue;
}
else {
printf("Q3-find ERROR\n");
returnfalse;
}
}
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Binomial Heap
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education