Friday, April 1, 2016

Week 8

Dear Citizens of the World,

Week 8's lecture discussed Binary Trees.

Our generic tree design has changed. We now have two named children called left and right, and a tree that is empty is represented by None.

To evaluate a Binary Tree:

No empty expressions:

If it's a leaf return its value. If it is not a leaf evaluate the left tree, and the right tree. After doing so combine the right, and left values with the binary operation given.

If there are empty expressions:

Use Python's built in eval method

>>>eval("7*9")
63

Inorder:

The recursive definition is to visit the left subtree inorder, visit the node, and then visit the right subtree inorder. The coding for inorder is extremely similar to the above definition.

Preorder:

The recursive definition is to visit the node itself, visit the left subtree preorder, and visit the right subtree preorder. Again, just like the inorder coding, preorder coding is also similar to the given definition.

Postorder:

The recursive definition:

Visit the left subtree postorder, visit the right subtree postorder, and visit the node itself.

Postorder coding is similar to the definition in layout.

Level Order:

Visit the node, visit the node children, visit node's grandchildren and great grandchildren, and so on.

Binary Search Trees

Ordering conditions to Binary Trees:

Data must be comparable. The data in the left subtree must be less than the value of node.data, and the data value in the right subtree must be more than the value of node.data

Height:

If arity is 2:

BST with 1 node has height 1.
BST with 4 nodes has a height of 3.
BST with 7 nodes can have a height of 3.

I will upload a diagram in the future to go into better detail and explanation of height.

Good day! :)

No comments:

Post a Comment