Trees#

To download the demo code for this lecture, run:

$ dmget wb-trees --demo

Introduction#

Goals#

  • Introduce terminology

  • Create a tree class and methods

  • Learn uses for trees

What Is a Tree?#

Tree Poem CS Tree

A computer science tree is similar to an upside-down version of trees that exist in nature.

Terminology#

  • node: basic unit

  • children: nodes directly below a node

  • descendants: nodes directly below a node

  • parent: node directly above a node

  • ancestor: node that is above a node

  • root node: node at the top of tree

  • leaf node: node without any children

An Org Chart Is A Tree#

Org Chart

A Filesystem Is A Tree#

Filesystem

HTML DOM Is A Tree#

DOM Tree

A Taxonomy Is A Tree#

Taxonomy

Trees in JavaScript#

The Node Class#

class Node {
  constructor(data, children=[]) {
    this.data = data;
    this.children = children;
  }
}
const rachna = new Node("Rachna");
const morgan = new Node("Morgan", [rachna]);
console.log(morgan.children);
// Node {data: 'Rachna'}

morgan.children.push(new Node("Taylor"));
console.log(morgan.children);
// [Node {data: 'Rachna'}, Node {data: 'Taylor'}]

Finding a Node#

Starting at Morgan, find Riley:

Find Riley

Highest-Ranking Riley#

Say there was another Riley in the organization.

Find the highest ranking Riley in the org chart:

Find Riley

Tree Class#

A Tree class would just keep track of the root node:

class Tree {
  constructor(root) {
    this.root = root;
  }
}

const tree = new Tree(new Node('Morgan'));
console.log(tree);   // Tree {root: Node {data: 'Morgan'} }

To implement find in a Tree class, just call the find method on the root node:

class Tree {
  findInTree(data) {
    return this.root.find(data);
  }
}

Linked Lists and Trees#

Every linked list is a tree.

But not every tree is a linked list.

Binary Search Trees#

A Binary Search Tree#

Binary Search Tree

  • Also a tree, made of nodes

  • Each node has max 2 children, a left child and a right child

  • Nodes are ordered according to a rule

Binary Search Tree Nodes#

class BinarySearchNode {
  constructor(data, left=null, right=null) {
    this.data = data;
    this.left = left;
    this.right = right;
  }
}

Binary Search Tree Find#

Binary Search Tree

class BinarySearchNode {
  find(dataToFind) {
    let current = this;

    while (current !== null) {
      if (current.data === dataToFind) {
        return current;
      }
      else if (dataToFind < current.data) {
        current = current.left;
      }
      else {
        current = current.right;
      }
    }
  }

Find C: E → left to B → right to D → left to null

Every choice we make reduces # options by half!

What’s the runtime of searching a BST?

  • For n nodes in the tree, we need to search at most log2n nodes.

  • So the runtime is O(log n)!

  • We can search >1,000 nodes in only 10 steps!

  • We can search >1,000,000 nodes in only 20 steps!

Badly Balanced Binary Trees#

Unbalanced Binary Tree

  • Can find A efficiently

  • Can find missing C efficiently

  • Can’t find G efficiently

  • Tree needs to be “balanced”

Advanced Ideas#

Can’t Move Up#

Org Chart

const isi = orgChart.find('isi');

isi.findParent();  // not possible!

Some Trees Point Both Ways#

Bidirectional Tree

class BidirectionalNode {
  constructor(data, parent, children=[]) {
    this.data = data;
    this.parent = parent;
    this.children = children;
  }
}

You Can Store Trees In Databases#

Coming Up#

  • Recursion!

    • “Each node is like a tree, itself”

  • Lots of whiteboard questions & challenges