CS1132_Fall_2011_Lecture6_BB

# CS1132_Fall_2011_Lecture6_BB - Lecture 6 Trees Contd....

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

CS 103 1 Lecture 6 Trees Contd. ( Chapter 9 ) Trees Binary Search Tree Complexity of Binary Search Tree

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

View Full Document
Typical Binary Tree Code in C In C or C++, the binary tree is built with a node type like this. .. struct node { int data; struct node* left; struct node* right; }
int lookup(struct node* node, int target) { // 1. Base case == empty tree // in that case, the target is not found return false if (node == NULL) { return(false); } else { // 2. see if found here if (target == node->data) return(true); else { // 3. otherwise recur down the correct subtree if (target < node->data) return(lookup(node->left, target)); else return(lookup(node->right, target)); } } } Looku p /*Given a bi n return true if target data is Recurs dow n chooses the l e by comparin g to each node Looku p /*Given a bi n return true if target data is Recurs dow n chooses the l e by comparin g to each node

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

View Full Document
We saw this before Insert/Push a node in front of a linked list using a double pointer /*Takes a list and a data value. Creates a new link with the given data and pushes it onto the front of the list. The list is not passed in by its head pointer. Instead the list is passed in as a "reference" pointer to the head pointer - - this allows us to modify the caller's memory.*/ void Push(struct node** headRef, int data) { struct node* newNode = malloc(sizeof(struct node)); newNode->data = data; newNode->next = *headRef; // '*' dereferences back to the real head *headRef = newNode; // ditto }
Pointer Changing Code Instead of a double pointer There is a common problem with pointer intensive code: what if a function needs to change one of the pointer parameters passed to it? For example, the insert() function below may want to change the root pointer. In C and C++, one solution uses pointers-to-pointers (aka "reference parameters"). We saw this technique before, but here we will use the simpler technique. A function that wishes to change a pointer passed to it will return the new value of the pointer to the caller. The caller is responsible for using the new value. Suppose we have a change() function that may change the root, then a call to change() will look like this. .. // suppose the variable "root" points to the tree root = change(root);

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

View Full Document
Pointer Changing Code We take the value returned by change(), and use it as the new value for root. This construct avoids passing po inter parameters by reference which can be confusing
Insert() Insert() -- given a binary search tree and a number, insert a new node with the given number into the tree in the correct place. The insert() code is similar to lookup(), but with the complication that it modifies the tree structure. insert() returns the new tree pointer to use to its caller.

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

View Full Document
Calling insert() with the number 5 on this tree. .. 2
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/14/2011 for the course CSCI 1132 taught by Professor Haya during the Fall '11 term at GWU.

### Page1 / 38

CS1132_Fall_2011_Lecture6_BB - Lecture 6 Trees Contd....

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

View Full Document
Ask a homework question - tutors are online