The term tree in computer science refers to an abstract data type (ADT) that has similarities to a real world tree turned upside down with the root at the top.
root ---> A / | \ B C D / / \ E F G
In general a tree can have any number of children. Our illustration above shows a tree with the value A in the root node, and the A node having three children. The B node has one child and the D node has 2.
Java has a two data structures (containers) that are based on the tree ADT which we will use in this course: TreeSet and TreeMap. We will not learn how to write these data structures as we did with the ArrayList class ( i.e ArrBag). We will just learn how to define a variable of the type and use it to solve problems. In a later course such as CS-1501 you will write your own implemementation of a binary search tree or a B tree etc.
The TreeMap and TreeSet containers that Java offers us are a specific kind of tree called binary search trees. The word binary means a maximum of two children - but it might have one or zero children. The term search tree means every node in the tree has the following properties:
root ---> A / \ C F / \ / \ B D E G
Java has a TreeSet class that uses a BST (binary search tree) and as elements are added into the tree it is kept as balanced (triangular) as possible such that if there are N nodes (values) in the BST then the maximum depth (number of levels / longest path) is at most log2N. This results in a big O runtime of log2N for the fundamental operations of the BST:
boolean add() runs in log2N returns T/F if the add succeeds/fails (How could it fail? You tried to add a duplicate element) boolean remove() runs in log2N returns T/F if the remove succeeds/fails (How could it fail? You tried to remove a non-existent element) boolean contains() runs in log2N returns T/F if the search succeeds/fails (How could it fail? You searched for a non-existent element)
Notice the strings come out in alphabetical order