MATLAB Answers

Binary search tree fill in

65 views (last 30 days)
Rick
Rick on 11 Aug 2014
Binary search trees
In these problems you will set up a binary search tree and write some associated functions that make the search tree useful.
Loading the data file students.mat (downloaded with this assignment) places a cell array called Students in the workspace. Students contains a list of student names and corresponding ID numbers. In this first task, you will write a function to insert a student's name and ID number into a binary search tree keyed to the ID numbers. Your function will have the declaration line
function newTree = bstInsert(bst, rootInd, name, idNumber)
The inputs are 1. bst: a structure array with fieldnames ∙ name ∙ ID ∙ left ∙ right (very similar to what was described in the lecture videos) representing the binary search tree. You may assume that the struct array is not empty and that the root of the binary search tree is the first element of the array. 2. rootInd: the current root index 3. name: the name to be inserted into the search tree 4. idNum: the ID number to be inserted into the search tree
Remember that a new element with the name and ID number to be inserted must be appended to the struct array bst before any elements of bst can be modied to point to its index.
The most natural way to write this function is with a recursive implementation that simply com- pares the ID number to a current node, and based on the result of that comparison calls itself recursively on the left or right subtree of the current node.
Note: For this problem, use the convention that if a key to be inserted is equal to an existing key, the key to be inserted will be placed in the left subtree of the existing key. The following code is useful for testing bstInsert. It initializes a binary search tree with the first row of the cell array Students and uses bstInsert to place the remainder of the student names and ID numbers in the search tree.
load students;
studentTree = struct('name',Students{1,1},...
'ID',Students{1,2},...
'left',0,...
'right',0);
for k = 2:size(Students,1)
studentTree = bstInsert(studentTree,1,Students{k,1},Students{k,2});
end
It may also be useful to modify the function bstinorder that was made available along with the lecture videos to view the elements of the binary search tree you create.
------------------------------------------------------------------------------------------------- This is the given code for bstinorder
function bstinorder(tree,index)
% Print the part of a tree under index using inorder traversal
% Input the tree and the index
% Assumes the fields are 'key', 'left' and 'right'
% descend the left subtree
if tree(index).left ~=0
bstinorder(tree,tree(index).left)
end
% print current key when there is none smaller
disp(tree(index).key);
% descend the right subtree
if tree(index).right ~=0
bstinorder(tree,tree(index).right)
end
The studentTree structure array only contains one student
studentTree =
name: 'Jim'
ID: 19907875
left: 0
right: 0
Here is my modification.
function newTree = bstInsert(bst, rootInd, name, idNumber)
% Print the part of a tree under index using inorder traversal
% Input the tree and the index
% Assumes the fields are 'key', 'left' and 'right'
% descend the left subtree
if bst(rootInd).left ~= 0
newTree = bstInsert(bst,bst(rootInd).left,bst(rootInd).name,bst(rootInd).idNumber);
end
% print current key when there is none smaller
disp(bst(rootInd).name);
% descend the right subtree
if bst(rootInd).right ~= 0
newTree = bstInsert(bst,bst(rootInd).left,bst(rootInd).name,bst(rootInd).idNumber);
end
I know since Jim has a value of left and right of 0, he must be a leaf node. His student ID also is the largest one, so I know he most be the bottom right leaf nodes in the binary tree. However, I don't know how to append the next student to be his parent. I run this and get the following error
Error in bstInsert (line 7)
if bst(rootInd).left ~=0
Output argument "newTree" (and maybe others) not assigned
during call to
"C:\Users\Owner\Documents\MATLAB\bstInsert.m>bstInsert".

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!