Binary Search Tree Homework - Setup BST & Write Functions

Click For Summary
The discussion focuses on setting up a binary search tree (BST) for a homework assignment involving student names and ID numbers. Participants address the implementation of the `bstInsert` function, which is intended to insert a new student into the BST based on their ID number. Issues arise regarding the handling of leaf nodes and ensuring that the function returns a value for `newTree`, particularly when both left and right branches are zero. The conversation highlights the importance of correctly managing the structure of the BST and suggests that additional code may be needed to maintain balance within the tree. Ultimately, the problem was resolved with assistance, and the need for balancing techniques like AVL trees was noted.
gfd43tg
Gold Member
Messages
947
Reaction score
48

Homework Statement


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

Code:
function newTree = bstInsert(bst, rootInd, name, idNumber)

The inputs are
1. bst: a structure array with fieldnames
##\bullet## name
##\bullet## ID
##\bullet## left
##\bullet## 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 modi ed 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 rst
row of the cell array Students and uses bstInsert to place the remainder of the student names
and ID numbers in the search tree.
Code:
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.


Homework Equations





The Attempt at a Solution


This is the given code for bstinorder
Code:
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

Code:
studentTree = 

     name: 'Jim'
       ID: 19907875
     left: 0
    right: 0

Here is my modification.
Code:
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

Code:
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".
 
Physics news on Phys.org
As written, your function will not assign a value to newTree unless either the left or right branch are zero.
There must be more to bstInsert that you are not showing us.

Also, the last two argument of bstInsert are not used in the code you show.

You're also not telling us whether any of your disp's are being executed.
 
I copy pasted the entire code I wrote for bstInsert. I modified it to at least include all input arguments
Code:
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 && bst(rootInd).ID > idNumber && strcmpi(bst(rootInd).name,name)
 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 && bst(rootID).ID < idNumber && strcmpi(bst(rootInd).name,name)
  newTree = bstInsert(bst,bst(rootInd).left,bst(rootInd).name,bst(rootInd).idNumber);
end

The first student, Jim, does display

Code:
Jim
Error in bstInsert (line 7)
if bst(rootInd).left ~= 0 && bst(rootInd).ID > idNumber

Output argument "newTree" (and maybe others) not
assigned during call to
"C:\Users\Owner\Documents\MATLAB\bstInsert.m>bstInsert".
 
Last edited:
If that's all of bstInsert, then the error message makes sense.
What do you want to do when both left and right are zero? As it stands now, you print bst(rootInd).name ("Jim") and then return from bstInsert with no value to assign to newTree (hence the "not assigned" error).

So what should you do when both left and right are 0?
 
Last edited:
I got help from a grad student for my class, so the problem has been solved now.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
9K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
4K