{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

COP3502_23_BinaryTrees2

# COP3502_23_BinaryTrees2 - Binary Trees Search Insert...

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

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

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

View Full Document
Binary Trees: Search & Insert 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: Search & Insert 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: Search & Insert 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: Search & Insert 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: Search & Insert 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.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern