fixed memory leaks by balancing dynamic memory allocation operations in the queue and tree constructs