Skip to content
Sharina Stubbs edited this page Jan 27, 2020 · 27 revisions

Resources:

Writeup from CodeFellows - nice diagrams

https://codefellows.github.io/common_curriculum/data_structures_and_algorithms/Code_401/class-15/resources/Trees.html

Drawings from Code Fellows 401 Java class:

https://github.com/codefellows/seattle-java-401d6/tree/master/class-14

Great simple drawing of inorder, postorder, preorder

Various Interview Problems for Trees, with recursion

From Michelle's lecture on Oct 10, 2019, Class 19.

See also Front Row video, if you can access it. This recording going through the above tree problems, including how to step through a recursive tree problem, on Oct 10, 2019.

Types of Trees

Binary Trees

Only contain at most 2 children. No particular sorting order exists for binary trees. Add nodes where ever there's space.

Full Binary Tree

A tree in which every node other than the leaves has two children.

           3
          / \
         2   10
       / \   / \
      14  1 6   7

Complete Binary Tree

A tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

           3
          / \
         2   10
       / \   / 
      14  1 6   

A complete binary tree is a balanced binary tree... but not the other way around.

Binary Search Trees

All values smaller than the root are placed to the left, and all values greater than the root are placed to the right. Note that this is different than a binary tree.

Balanced Tree

All leaves are within one 1 h of each other, where h is the height of the tree. if the person uses d, that is referring to depth of the tree.

If no two values of depth are no more than 1 away from each other, it's a balanced tree. If the depth of one branch is greater or lesser than the depth of another branch by > 1, it's not a balanced tree.

Careful, though, there are variations on the definition of a balanced tree.

Time and Space

For a Balanced Tree:

  • Sorting is O(log n) time
    • log2(1000000000) is only 29.9

For a mostly balanced tree

Big O is O(n) where n is the height of the tree. Usually written like O(h).

Regarding Space, O(1).

Tree Traversal

A treenode has a value, a left and a right.

Two Main Ways to Traverse:

  • Depth First
    • PreOrder
    • InOrder
    • PostOrder
  • Breadth First

Depth First

Depth First has three ways to travels the HEIGHT of a tree.

(1) Preorder - root, left, right

ALGORITHM PreOrder(node)
// INPUT <-- root Node
// OUTPUT <-- preorder output of tree nodes

    OUTPUT <-- node.Value

    if node.LeftChild is not Null
        PreOrder(node.LeftChild)

    if node.RightChild is not NULL
        PreOrder(node.RightChild)

Also roughly written like:

preOrder Print(Node root) {
  print(root.value);
  if(left not null) preOrderPrint(root.left)
  if(right not null) preOrderPrint(root.right)
}

(2) Inorder - left, root, right

ALGORITHM InOrder(node)
// INPUT <-- root node
// OUTPUT <-- inorder output of tree nodes

    if node.LeftChild is not NULL
        InOrder(node.LeftChild)

    OUTPUT <-- node.Value

    if node.RightChild is not null
        InOrder(node.RightChild)

Also roughly written like:

InOrder Print(Node root) {
  if(left not null) InOrderPrint(root.left)
  print(root.value);
  if(right not null) InOrderPrint(root.right)
}

(3) Postorder - left, right, root

ALGORITHM PostOrder(node)
// INPUT <-- root node
// OUTPUT <-- postorder output of tree nodes

    if node.LeftChild is not NULL
        InOrder(node.LeftChild)

    if node.RightChild is not null
        InOrder(node.RightChild)

    OUTPUT <-- node.Value

Written roughly, this is:

PostOrder Print(Node root) {
  if(left not null) PostOrderPrint(root.left)
  if(right not null) PostOrderPrint(root.right)
  print(root.value);
}

Breadth First

Iterates through a tree by traveling through each level of the tree, node by node. 

  • Uses a **queue **
ALGORITHM BreadthFirst(root)
//INPUT <-- root node
// OUTPUT <-- front node of queue to console

    Queue breadth <-- new Queue()
	breadth.Enqueue(root)
		
	while breadth.Peek
	    Node front = breadth.Dequeue()
		OUTPUT <-- front.Value

		if front.LeftChild is not null
			breadth.Enqueue(front.LeftChild)

		if front.RightChild is not NULL
			breadth.Enqueue(front.RightChild)
Clone this wiki locally