COP3502_25_BinaryTrees4 editted

COP3502_25_BinaryTrees4 editted - Binary Trees: Practice...

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

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida Binary Trees: Practice Problems COP 3502 – Computer Science I
Background image of page 1

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

View Full DocumentRight Arrow Icon
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; }
Background image of page 2
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; }
Background image of page 3

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

View Full DocumentRight Arrow Icon
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; }
Background image of page 4
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: If the “root” is not NULL, then the root increases our count Shown by the return of 1 We then call count on the left and right subtrees of root int count(struct tree_node *root) { if (current_ptr != NULL) return 1 + count(root->left)+ add(root->right); else return 0; }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 22

COP3502_25_BinaryTrees4 editted - Binary Trees: Practice...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online