@BiancaGando

Intro to

Trees

and Searching

@BiancaGando

Trees

@BiancaGando

Constructor:

- storage

- root

Methods:

  • insert(key): This inserts a new key in the tree
  • search(key): This searches for the key in the tree and returns true if it exists and returns false if the node doesn't

  • min/max: This returns the min or max value/key in the tree

  • remove(key): This removes the key from the tree

Interface: Trees

Concept: Linked Lists

A linked list is a tree structure with only one child per node.

Each node in the list contains:

  1. Stored data -- a node value
  2. Stored reference -- a link -- to the next item in the list

@BiancaGando

@BiancaGando

Diagram: Linked List

@BiancaGando

Diagram: Adding an Item

@BiancaGando

Diagram: Removal

@BiancaGando

1. Constructor Function

  - storage

  - head

 

2. Methods

  - .addToTail(val) //adds node to tail

  - .remove(node) //removes node from list & returns it

 

Interface: Linked List

@BiancaGando

Pseudocode: Linked List

//Constructor

//Add to tail

//Remove node

@BiancaGando

Pseudocode: Tree

//Constructor

//Add a child

@BiancaGando

Adding nodes: O(1)*

Removing nodes: O(1)*

Searching: O(n)

 

 

 

*when you have a reference to the node.

Time Complexity

@BiancaGando

Fast addition

Fast removal

Linked List

Slow look-up without reference to the node

Pros

Cons

@BiancaGando

Linked List Exercise

Create a scavenger hunt game using a Linked List. 

 

Let's discuss how you think we may go about doing that before diving in.

Exercise Time!

@BiancaGando