Binary Search Tree implemented. C++

This is my personal implementation of a binary search tree. My tree uses iteration instead of recursion for its insertion, deletion and searches for faster execution because function stack overhead is avoided. Recursion is still used for tree deletion because I didn’t want to implement a stack for the traversal of the tree which is required for deletion.

A realistic C++ line counter

This program asks the user for a c++ source file. It displays the file’s linecount to the screen. Program exits on invalid input. This sourcefile is 90 lines of code(according to itself).
Each of the following counts as one line of code:

  1. Preprocessor directives
  2. if()
  3. while()
  4. do..while()
  5. switch()
  6. non-repeating semicolons

Can’t use a string in a C++ switch statement? Here is a simple alternative.

Many programmers are surprised to find that switch statements aren’t compatible with strings in C/C++.

When attempting to compile, you will see an error:

Visual Studio 2010 —–  error C2450: switch expression of type ‘std::string’ is illegal

Netbeans 6.9.1 —– error: switch quantity not an integer

Here is an example of invalid code

The problem is that strings are not a basic type in the C++ language. Whenever a C++ programmer uses basic variables types like int, bool, and char, nothing extra needed. But when a C++ programmer wishes to use strings in a program, they must add #include <string> to the top of the code to enable that functionality. Basic switch statements don’t have the capability to understand “add on” features such as strings and other filestreams.

There is still hope. The people who created the string library were kind enough to include a function to compare strings – strcmp(). The string compare function can be used along with if..else statements to achieve the same logic as a switch statement.

This is the same logic as before without using a switch statement:

A Lexical Analyizer for C++ tokens

Lexical analysis is step 1 of compiling code down to machine language. The process breaks source code down into a long list of pieces called tokens. This list of tokens is used by a parser algorithm that extracts meaning from the order and arrangement of the tokens. Here is a small example of lex analysis:

code:

int main(void) {

float myvar = 2.5;

return 0;

}

list of tokens:

  1. int type
  2. main reserved word
  3. (
  4. void keyword
  5. )
  6. {
  7. float keyword
  8. myvar identifier
  9. = operator
  10. 2.5 floating point constant
  11. ; end statement
  12. return keyword
  13. 0 integer constant
  14. ; end statemant
  15. }

As you can see, the list of tokens gets long rather quickly. Absolutely no syntax checking is done during lex. That happens later down the line.

I have written a basic lexical analyzer to break C++ code into tokens.  Its own source code is C++ as well.

Doubly Linked List (C++)

This code is an implementation and test driver of a doubly linked list. This list is pointer based and uses dynamic memory.

Open Text File the Right Way. (C++)

This code is a simple example of how to safely open a text file in C++. Notice the very important check that the file was opened correctly. This covers a range of errors,  most commonly file not found.

Press any key to Continue…(C++)

this snippet of code is an alternative to system(“pause”) in MS Visual Studio.

String to Int function (C++)

This little piece of code can convert a string of decimal characters into an integer value.  If you seek to convert a string into a double, see this post.

A Pretend Radio Class (C++)

Download the  entire code: RadioClass.zip

Doubly linked Stack Class (C++)

Definition file, stack.h

Implementation file, stack.cpp

Test driver