Mosh Trees Tutorial

Non Linear Data structures – trees

Tree is a data structure that stores element in a hierarchy.

Root – Top node of tree
Root is parent of its children.
Each node can have up to two nodes…this is called a binary tree.

Trees

– represent hierarchical data
– databases
– autocompletion
– compilers (syntax tree to parse expressions)
– compression (JPEG, MP3)

left < node < right At every step of comparison, we throw away half of the tree. This is what we call Logarithmic Time

Lookup
O(log n)

Inserting node
Finding a Node O(log n) + Inserting Node O(1) = O(log n)

Delete Node
O(log n)

Depth First Search (DFS)

pre-order – (left of node)
in-order – (bottom of node) ascending order
post-order – (right of node)

Breadth First – Level by level (BFS)

Recursion

Iteration – loops.
Recursion – no loops.

Base condition terminates Recursion.

Depths and Height

Depths starts at the root

Depths of root is 0. As it goes deeper, it increases.
Depth of 10 is 1.
Depth of 21 is 2.
Depth of 8 is 3.

Depth – # of edges from root to target node.

Height is opposite of depth.

Height starts at the leaf

Height of leafs are 0.
As we go up, height increases. Longest path from target node to leaf.

Height of 6 is 1.

Height of 30 is 1.

Height of 10 is 2. We actually have 3 different paths. 3 to 10. 8 to 10. 21 to 10. Longest path is 2.

Height of 20 is 3, because it’s the longest path.

1 + max(height(L) + height(R))

Post Traversal – visit all leaves first

Find minimum value in BST

If it’s a standard BST, use recursion, and get leftmost leaf.

But if its NOT a BST, values are all over the place, so

Exercises

– height of tree. return Math.max of left subtree and right subtree. That way, you get the height at the root.

– # of nodes at distance k. simple pre-order and print.

– isBST? pass in parameter min, max. Make sure node value stays within range. If not, false. Kick off with -Infinity and +Infinity.

Minimum – isBST? if yes, get leftmost. If not, then must have parameter reference and recurse down, do compare with every value.

Maximum – isBST? if yes, get rightmost. If not, then must have parameter reference and recurse down, do compare with every value.

– Are the two tree equal (Equality Check)?

– traverse BST (Level Order Traversal)

for i to height of tree
let arr = getNodesAtDistance(i)
for j to arr.length
log arr[j];

Additional exercises

1- Implement a method to calculate the size of a binary tree. Solution: Tree.size()

2- Implement a method to count the number of leaves in a binary tree. Solution: Tree.countLeaves()

4- Implement a method to check for the existence of a value in a binary tree using recursion. Compare this method with the find() method. The find() method does the same job using iteration. Solution: Tree.contains()

5- Implement a method to check to see if two values are siblings in a binary tree. Solution: Tree.areSibling()

6- Implement a method to return the ancestors of a value in a List. Solution: Tree.getAncestors()

SOLUTION

Test