1 / 6

Binary Trees & Insertion

Binary Trees & Insertion. COP 3502. Binary Search Tree Insertion. Inserting a Node into a Binary Search Tree Similar to searching for a node We have to “trace out” the same path, to find where this node belongs in the tree. Let’s say we were going to search for 5 in the following tree:. 6.

zizi
Télécharger la présentation

Binary Trees & Insertion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary Trees & Insertion COP 3502

  2. Binary Search Tree Insertion • Inserting a Node into a Binary Search Tree • Similar to searching for a node • We have to “trace out” the same path, to find where this node belongs in the tree. • Let’s say we were going to search for 5 in the following tree: 6 6 2 2 9 1 4 4 7 12 5 would be here if it was in the tree, So we know this is where 5 belongs when inserting the value. 5

  3. Binary Search Tree Insertion • Strategy to insert a node recursively: • If the tree is empty, just return a pointer to a node containing the new value. • Otherwise see which sub-tree the node should be inserted into by comparing the value stored at the root. • If we want to insert in the left sub-tree and it’s NULL, then we know this is where we attach the node so no recursive call is necessary. • Same with the right… • Then recursively either insert into the left sub-tree or the right sub-tree.

  4. Binary Search Tree Insertion • Create the code in class. node* insertNode(node *curr, node *temp) { // Inserting into an empty tree. // temp2 should be inserted to the right. // There is a right subtree to insert the // Place the node directly to the right of // temp2 should be inserted to the left. // There is a left subtree to insert the node. // Place the node directly to the left of // Return the curr pointer of the updated tree. }

  5. Binary Search Tree Insertion • Run some insertion, traversal, and search examples in code. • Draw out some examples on the board.

  6. Summing the nodes in a Binary Tree • We can really use any of the traversals to implement this. • All we need to do add the values from the three portions of the three together and return this answer. • Notice how succinct this code is! int Add(structtree_node *current_ptr) { if (current_ptr != NULL) return current_ptr->data+Add(current_ptr->left)+ Add(current_prt->right); else return 0; } int Add(structtree_node *current_ptr) { if (current_ptr != NULL) return current_ptr->data+ Add(current_ptr->left)+ Add(current_prt->right); else return 0; } int Add(structtree_node *current_ptr) { if (current_ptr != NULL) return current_ptr->data+ Add(current_ptr->left)+ Add(current_prt->right); else return 0; } int Add(structtree_node *current_ptr) { if (current_ptr != NULL) return current_ptr->data+ Add(current_ptr->left)+ Add(current_prt->right); else return 0; }

More Related