COP3502_25_BinaryTrees4

# COP3502_25_BinaryTre - Binary Trees Practice Problems Computer Science Department University of Central Florida COP 3502 Computer Science I Binary

This preview shows pages 1–6. Sign up to view the full content.

Computer Science Department University of Central Florida Binary Trees: Practice Problems COP 3502 – Computer Science I

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Binary Trees: Practice Problems page 2 Binary Trees: Practice Problems Warmup Problem 1: Searching for a node in a BST int find (struct tree_node *current_ptr, int val) { // Check if there are nodes in the tree. if (current_ptr != NULL) { // Found the value at the root. if (current_ptr->data == val) return 1; // Search to the left. if (val < current_ptr->data) return find(current_ptr->left, val); // Or. ..search to the right. else return find(current_ptr->right, val); } else return 0; }
Binary Trees: Practice Problems page 3 Binary Trees: Practice Problems Warmup Problem 2: Searching for a node in an arbitrary tree Not a BST Doesn’t have the ordering property int Find(struct tree_node *current_ptr, int val) { if (current_ptr != NULL) { if (current_prt->data == val) return 1; return (Find(current_ptr->left, val) || Find(current_ptr->right, val)) } else return 0; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Binary Trees: Practice Problems page 4 Binary Trees: Practice Problems Warmup Problem 3: Summing the values of nodes in a tree int add(struct tree_node *current_ptr) { if (current_ptr != NULL) return current_ptr->data + add(current_ptr->left)+ add(current_ptr->right); else return 0; }
Binary Trees: Practice Problems page 5 Binary Trees: Practice Problems Count Nodes: Write a function that counts (and returns) the number of nodes in a binary tree Details:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/21/2011 for the course COP 3330 taught by Professor Staff during the Spring '08 term at University of Central Florida.

### Page1 / 18

COP3502_25_BinaryTre - Binary Trees Practice Problems Computer Science Department University of Central Florida COP 3502 Computer Science I Binary

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online