Monday, 9 March 2015

CSC148 - Entry 6

Entry no. 6, Week 8:

Topics

1. Impression on Week 7


This weeks topic is on Week 7; Binary Trees (BT), Traversal and Binary Search Trees(BST).

In my previous post I used a Binary Tree to help illustrate a use of Recursion.
For this entry, I'll be writing more in depth on BT's, Traversal and BST's.

The weeks material is something I've learned before, so it's nothing new. As a result I feel like I already have a firm grasp on it.

BT's are variations of Trees in the sense that they are limited to 2 predecessors, Left and Right. We call their predecessors children and can each represent another BT. An empty sub tree can be identified as a NoneType because it is in the set of everything.

Traversal is the ways that we visit each node in a BT. The three simple traversals are Pre-Order, In-Order and Post-Order, and will each go through the tree in different ways. These are recursive definitions that were defined in class.
  • Pre-Order:  
    • Visit the current node
    • Traverse the left sub tree Pre-Order
    • Traverse the right sub tree Pre-Order
  • In-Order:  
    • Traverse the left sub tree In-Order
    • Visit the current node
    • Traverse the right sub tree In-Order
  • Post-Order:
    • Traverse the left sub tree Post-Order
    • Traverse the right sub tree Post-Order
    • Visit the current node
BST's are BT's that store comparable values and separate their left sub tree with data that values less than itself and a right tree with data that values greater than itself. We assume that if a value is added to a BST that already contains that value, it is not added.

BST's are useful because they cut down the search time to Big-O(log2(n)) where n represents the amount of data stored within the tree. Using the properties of a BST, we can eliminate an entire half of the BST and check the other half for the value. However, if a BST is not balanced (ie. all the data is stored within the either only the right or only the left sub trees) the search time grows to Big-O(n).

No comments:

Post a Comment