COP3502_23_BinaryTrees2

COP3502_23_BinaryTrees2 - Binary Trees: Search &amp; Insert...

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

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

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

View Full Document
Binary Trees: page 2 Binary Search Tree Binary Search Trees Ordering Property: For each node N, all the values stored in the left subtree of N are LESS than the value stored in N. Also, all the values stored in the right subtree of N are GREATER than the value stored in N. Why might this property be a desireable one? Searching for a node is super fast! Normally, if we search through n nodes, it takes O(n) time But notice what is going on here: This ordering property of the tree tells us where to search We choose to look to the left or look to the right of a node We are HALVING the search space O(log n) time
Binary Trees: page 3 Binary Search Tree: Searching Binary Search Trees Searching for a node: Algorithm: 1) IF the tree is NULL, return false. ELSE 1) Check the root node. If the value we are searching for is in the root, return true. 2) If not, if the value is less than that stored in the root node, recursively search in the left subtree. 3) Otherwise, recursively search in the right subtree.

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

View Full Document
Binary Trees: page 4 Binary Search Tree: Searching Binary Search Trees Searching for a node (Code): 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: page 5 Binary Search Tree: Creation Insertion into a Binary Search Tree Before we can insert a node into a BST, what is the one obvious thing that we must do? We have to actually create the node that we want to insert malloc space for the node And save appropriate data value(s) into it Here’s our struct from last time: struct tree_node { int data; struct tree_node *left_child; struct tree_node *right_child; }

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

View Full Document
Binary Trees: page 6 Binary Search Tree: Creation Creating a Binary Search Tree In main, we simply make a pointer of type struct tree_node and initialize it to NULL struct tree_node *my_root = NULL; So this is the ROOT of our tree You then get your values to insert into the tree This could be automated You could have the user enter a value(s)
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 09/21/2011.

Page1 / 27

COP3502_23_BinaryTrees2 - Binary Trees: Search &amp; Insert...

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

View Full Document
Ask a homework question - tutors are online