Do you want to play a game? | Week 1 Day 5
Algorithms and data structures were the orders of business for the final day of week one at App Academy. Our morning lecture delved (literally) into the concept of a branching data structure called trees. We had previously covered arrays — a linear data structure that can be used as a stack or a queue — but data trees have properties that make them advantageous in terms of time complexity. (I sort of know what that means.)
Whereas arrays are ordered in a line, trees are branching structures that are composed of nodes (the things that actually hold data) connected by links. If a node has sub-nodes, it is called a 'parent' and the nodes it connects to are 'children.' This parent to child structure continues until you reach the bottom layer of nodes that have no children, referred to as 'leaves' of the tree.
One advantage of this structure is that you don't have to check every node in order to find your target value or piece of data. Some examples are depth-first search and breadth-first search.
A depth-first search conducted on a binary tree will recursively travel down the tree adding to the 'call stack,' constantly checking the left child node of each parent until it reaches the end of the branch or finds the target. If the search gets to the end of a given branch and does not return the target, it will backtrack up the nodes until it can move to a right node. This pattern will continue until the entire tree is searched, or the target is identified.
In a breadth-first search, the tree is checked layer by layer. This is good if you know that the target node is close to the parent. This is accomplished using a queue. The parent node begins in the queue, and is then dequeued and checked against the search value. If they are equivalent, the parent is returned. If not, the child nodes of the parent are added to the queue, and the process is continued until the entire tree is searched or the target is found.
At the end of today, we coded up an AI for a tic-tac-toe game using a branching data structure. This was one of the most rewarding and fun things so far at App Academy. The feeling of satisfaction that accompanied running a computer player against another computer player and watching them continually play to a draw is hard to describe.