This preview shows pages 1–12. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS163: Data structures Week 7: Trees (cont.) 7/2008 dbtien Data structures What is in CS163 today? Examine the algorithm to insert Examine the algorithm to remove Begin discussing efficiency of tree Are there any alternatives? 23 234 (next time) redblack trees (next time) AVL (next time) dbtien Data structures Tree Insert From last time... everyone should have prepared an algorithm for insert remember, insert always inserts data at a leaf this is similar, in many regards to inserting data at the end of a linear linked list; the good news is that we dont have to special case the situation where we are trying to rearrange pointers by inserting in the middle dbtien Data structures LLL Recursive Insert For example, lets review what it would be like to insert into a LLL  adding at the end all of the time: void insert(node * & head, data & d){ if (!head) { head = new node; head>d = d; head>next = NULL; } else insert(head>next, d); } dbtien Data structures LLL Recursive Insert Why does this work? Why does head need to be passed in? Why cant we just use a data member named head? Why does head need to be passed by reference? How does it connect up the nodes? Why was this inefficient for a linear linked list? dbtien Data structures LLL Recursive Insert Another way to write this: node * insert(node * head, data & d){ if (!head) { head = new node; head>d = d; head>next = NULL; return head; } head>next = insert(head>next,d); return head; } dbtien Data structures LLL Recursive Insert Is this approach more or less efficient? How do the nodes get connected? Does it handle the special case where head is null to begin with? Does it ever dereference a null pointer? How about copies being placed on the program stack? How does this compare with the previous recursive solution? dbtien Data structures Tree Recursive Insert Now lets apply what we have learned to insert into a binary search tree Remember, if the data being inserted is less than the root, we want to traverse left If the data being inserted is greater than the root, we want to traverse right If it is the same, pick a consistent approach to deal with it (either left or right) dbtien Data structures Tree Recursive Insert void insert(node * & root, data & d){ if (!root) { root = new node; root>d = d; root>left = NULL; root>right = NULL; } else if (root>d > d) insert(root>left, d); else insert(root>right, d); } dbtien Data structures Tree Recursive Insert node * insert(node * root, data & d){ if (!root) { root = new node; root>d = d; root>left = NULL; root>right = NULL; } else if (root>d > d) root>left = insert(root>left, d); else root>right=insert(root>right, d); return root; } dbtien Data structures Tree Recursive Insert Do both of these approaches work?...
View
Full
Document
This note was uploaded on 11/03/2010 for the course CS 16x taught by Professor Tang during the Spring '10 term at Aachen University of Applied Sciences.
 Spring '10
 Tang
 Data Structures

Click to edit the document details