add2tree - new_tree = old_tree if new_element<...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
function [new_tree] = add2tree(old_tree, new_element) % % adds new element to old tree and returns new tree % % base case is if old_tree isempty if isempty(old_tree) new_tree.val = new_element; new_tree.left = []; new_tree.right = []; else % initialize new_tree to be old_tree
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: new_tree = old_tree; if new_element < new_tree.val new_tree.left = add2tree(new_tree.left, new_element); elseif new_element > new_tree.val new_tree.right = add2tree(new_tree.right, new_element); end end % end if end % end add2tree.m...
View Full Document

This note was uploaded on 11/16/2009 for the course ENGIN 7 taught by Professor Horowitz during the Spring '08 term at Berkeley.

Ask a homework question - tutors are online