Output: 12 18 30 72 90 87. struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;



Similar documents
Data Structure with C

Ordered Lists and Binary Trees

OPTIMAL BINARY SEARCH TREES

Binary Search Trees (BST)

Data Structures and Algorithms

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Algorithms and Data Structures Written Exam Proposed SOLUTION

From Last Time: Remove (Delete) Operation

Analysis of Algorithms I: Optimal Binary Search Trees

Analysis of Algorithms I: Binary Search Trees

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Lecture Notes on Binary Search Trees

TREE BASIC TERMINOLOGIES

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Why Use Binary Trees?

Conditionals (with solutions)

Lecture Notes on Binary Search Trees

Chapter 14 The Binary Search Tree

Binary Heap Algorithms

Alex. Adam Agnes Allen Arthur

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Help on the Embedded Software Block

Binary Trees and Huffman Encoding Binary Search Trees

Algorithms Chapter 12 Binary Search Trees

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Stacks. Linear data structures

Converting a Number from Decimal to Binary

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Full and Complete Binary Trees

Section IV.1: Recursive Algorithms and Recursion Trees

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

CS 241 Data Organization Coding Standards

Parallelization: Binary Tree Traversal

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Binary Search Trees. Ric Glassey

The ADT Binary Search Tree

Illustration 1: Diagram of program function and data flow

C++FA 5.1 PRACTICE MID-TERM EXAM

Data Structures CSC212 (1) Dr Muhammad Hussain Lecture - Binary Search Tree ADT

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Binary Heaps. CSE 373 Data Structures

Symbol Tables. Introduction

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Graduate Assessment Test (Sample)

Sample Questions Csci 1112 A. Bellaachia

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Big Data and Scripting. Part 4: Memory Hierarchies

Rotation Operation for Binary Search Trees Idea:

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Binary Search Trees 3/20/14

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Union-Find Problem. Using Arrays And Chains

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Sample CSE8A midterm Multiple Choice (circle one)

Writing Functions in Scheme. Writing Functions in Scheme. Checking My Answer: Empty List. Checking My Answer: Empty List

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Chapter 5. Recursion. Data Structures and Algorithms in Java

Algorithms and Data Structures

Lecture 12 Doubly Linked Lists (with Recursion)

Optimal Binary Search Trees Meet Object Oriented Programming

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Persistent Binary Search Trees

J a v a Quiz (Unit 3, Test 0 Practice)

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Specific Simple Network Management Tools

DATABASE DESIGN - 1DL400

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

GRAPH THEORY LECTURE 4: TREES

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

CSE 326: Data Structures B-Trees and B+ Trees

Introduction to data structures

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Virtuozzo Virtualization SDK

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Binary Search Trees CMPSC 122

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Data Structures For IP Lookup With Bursty Access Patterns

PES Institute of Technology-BSC QUESTION BANK

Cpt S 223. School of EECS, WSU

DESIGN AND ANALYSIS OF ALGORITHMS

COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm

Transcription:

50 20 70 10 30 69 90 14 35 68 85 98 16 22 60 34 (c) Execute the algorithm shown below using the tree shown above. Show the exact output produced by the algorithm. Assume that the initial call is: prob3(root) and that the tree nodes and pointers are defined as shown. struct treenode struct treenode *left, *right; struct treenode *tree_ptr; void prob3(struct tree_ptr *node_ptr) if (node_ptr!= NULL) if (node_ptr->data % 3 == 0) printf( %d, node_ptr->data); prob3(node_ptr->left); else if (node_ptr->data % 3 == 1) printf( %d, node_ptr->data+2); prob3(node_ptr->right); else if (node_ptr->data % 3 == 2) prob3(node_ptr->left); prob3(node_ptr->right); Output: 12 18 30 72 90 87

1. [ 9 pts ] For the binary tree given below root is a pointer to the root of the tree. Redraw (on the following page) the tree shown above when the following function is executed. Assume that the initial call is modifyt(root, 7, 65). struct treenode struct treenode *left; struct treenode *right; ; void modifyt(struct treenode* node_ptr, int key, int num) if (node_ptr!= NULL) if (node_ptr->data % 3 == 0) node_ptr->data += key; modifyt(node_ptr->left, key + 2, num - key); modifyt(node_ptr)->right, key 3, num + key); else if (node_ptr->data % 5 == 0) node_ptr->data -= key; modifyt(node_ptr->right, key - 4, num); modifyt(node_ptr->left, key, num + 5); else node_ptr->data = num; modifyt(node_ptr->right, key - 2, num + 10); modifyt(node_ptr->left, key + 5, num 7); Computer Science B Page 1 of 10 KEY

Answer for Problem 3 1 point for each correct node modification 1 point extra if the entire tree is correct Computer Science B Page 2 of 10 KEY

5. [ 8 pts ] Write a recursive function that compares two given binary trees. It returns 1 if two trees are different and it returns 0 otherwise. Use the node structure and the function prototype provided below: struct treenode struct treenode * left; struct treenode * right; ; int check(struct treenode *A, struct treenode *B) One possible solution: int check(struct treenode *A, struct treenode *B) if(a == NULL && B == NULL) return 0; else if(a == NULL B == NULL) return 1; else if(a->data!= b->data) return 1; if(check(a->left, B->left) check(a->right, B->right)) return 1; else return 0; Grading: Base cases: The trees are the same if both trees are NULL (1 point) The trees are different if one is NULL, the other isn t (1 point) The trees are different if neither is NULL, but the data differs (1 point) Recursive cases: The two trees are different if either the left or right is different (3 points) The two trees are the same only if both the left and right are the same (2 points) Computer Science B Page 3 of 10 KEY

5. a) [5 pts] Indicate in few words, the purpose of the following function. The struct treenode is the same as the one defined in problem 4 on the previous page. int f(struct treenode * p) int val; if (p == NULL) return 0; val = p->data; if (val%2 == 0) return val + f(p->left) + f(p->right); else return f(p->left) + f(p->right); Returns the sum of the nodes storing even values in the tree. Grading: 2 pts for sum, 1 pt for node values, 2 pts for even b) [5 pts] What does the function return, given the following tree? 52 83 76 11 60 87 51 23 17 70 14 78 73 Answer: 78+52+76+60+70+14 = 350 Computer Science B Page 4 of 10 KEY

3. (10 points) Write a recursive function to compute the height of a tree, defined as the length of the longest path from the root to a leaf node. For the purposes of this problem a tree with only one node has height 1 and an empty tree has height 0. Your function should make use of the following tree node structure: struct treenode struct treenode* left; struct treenode* right; ; int height(struct treenode* root) int leftheight, rightheight; if(root == NULL) return 0; leftheight = height(root->left); rightheight = height(root->right); if(leftheight > rightheight) return leftheight + 1; return rightheight + 1; Computer Science B Page 5 of 10 KEY

3. Write a recursive function struct node * largest( struct node * B) which returns a pointer to the node containing the largest element in a BST ( binary search tree). The node structure is as follows: struct node int node_value; struct node * left, *right; ; [8 pts] struct node* largest(struct node *B) if ( B==NULL) return NULL; else if (B->right ==NULL) return B; else return largest(b->right); Grading: 4 pts for an iterative solution 4. In a binary tree, each node may have a single child, two children, or no child. Write a recursive function int one (struct tree_node *p) for a binary tree which returns the number of nodes with a single child. Use the node structure struct tree_node struct tree_node * left, *right; ; [10 pts] int one ( struct tree_node *p) if ( p!= NULL) if( p->left == NULL) if( p->right!= NULL) return 1+ one(p->right); else if( p->right == NULL) if( p->left!= NULL) return 1+ one(p->left); else return one (p->left) + one(p->right) ; Computer Science B Page 6 of 10 KEY

4) (10 pts) DSN (Binary Trees) Consider the problem of finding the k th smallest item in a binary search tree storing unique values. Write a recursive function with the prototype shown below to solve this problem. The first parameter to the function will be a pointer to the root node of the binary tree and the second value will be the 1-based rank of the item to be returned. (If k is 1, you should return the smallest item in the tree.) Note that the number of nodes in the subtree at each node is stored inside the node. You may assume that the value of k will be in between 1 and the value of numnodes in the struct pointed to by root. typedef struct treenode int numnodes; struct treenode *left; struct treenode *right; treenode; int kthsmallestvalue(treenode* root, int k) for this case. if (root->left == NULL && k == 1) return root->data; for this case. int leftcnt = root->left->numnodes; if (k - 1 == leftcnt) return root->data; // 3 pts for this case. if (k - 1 < leftcnt) return kthsmallestvalue(root->left, k); // 3 pts for this case. return kthsmallestvalue(root->right, k-leftcnt-1); Computer Science B Page 7 of 10 KEY

4) (10 pts) DSN (Binary Trees) Write a function that is given a pointer to the root of a valid binary search tree with unique elements and prints out a list of all the odd numbers stored in the binary search tree, in descending order. Use the struct definition and function prototype provided. typedef struct treenode struct treenode *left; struct treenode *right; treenode; void printodddescending(treenode* root) if (root == NULL) return; printodddescending(root->right); if (root->data%2 == 1) printf("%d ", root->data); printodddescending(root->left); // 1 pt // 1 pt for correct // order of calls. Computer Science B Page 8 of 10 KEY

4) (10 pts) DSN (Binary Trees) Mark and his buddy Travis have devised a password scheme to secure files that they send among themselves. Their scheme hides the password in a string of English letters. The password is the alphabetically ordered sequence of the consonants in the string. So as not to have to compute the password each time, Mark has written a function called printpassword, which takes the letters of the original string stored in a binary search tree and prints out the password. For example, if the string in the message is mental, the password printed out would be lmnt. Or if the string was fragile, then the password would be fglr. You may call the following function in your solution: // Returns 1 if c is a consonant, 0 otherwise. int isconsonant(char c) Using the struct definition given below, complete the function in the space provided. typedef struct treenode char ch; struct treenode *left; struct treenode *right; treenode; void printpassword(treenode* root) if (root!= NULL) printpassword(root->left); if (isconsonant(root->ch)) printf("%c", root->ch); printpassword(root->right); Computer Science B Page 9 of 10 KEY