1 of 57

Lecture 20:

Binary Search Trees

CS 136: Spring 2024

Katie Keith

2 of 57

Record on Zoom

3 of 57

  • Lab 6 this week: No starter code!
    • Recommended to allocate a bit more time for this lab
  • TA eval form (Due Fri April 18) : https://forms.gle/sbqCGVLAFnhUQ4i39
  • Colloquium this Friday: Dr. Ellie Pavlick

📣 Announcements

4 of 57

📚Readings

  • Sedgewick and Wayne. Algorithms. Section 3.2.

5 of 57

  • Wrap-Up Symbol Table Data Structure #2: Two arrays (sorted)
  • Overview of Binary Search Trees (BST)
  • BST get operation
  • BST put operation

🎯 Today’s Learning Objectives

6 of 57

Review: Symbol Table

A symbol table is an ADT where we save information (a value) that we can later search for and retrieve by specifying a key. Keys are unique (no duplicates).

Keys

Values

"USA"

40

"China"

40

"Japan"

20

"Saint Lucia"

1

Running Example:

Record the number of gold medals each country won in the 2024 Olympics

Julien Alfred Wins Saint Lucia's First Gold Medal in the 100 M Dash

7 of 57

Review: Python Dictionaries are Symbol Tables

gold_medals = {

"USA": 40,

"China": 40,

"Japan": 20,

"Saint Lucia": 1,

}

gold_medals["New Zealand"] = 10

print(gold_medals["USA"])

put operation

get operation (search!)

Key of a dictionary

Value of a dictionary

CS 134: How do we use dictionaries?

CS 136: How do we implement dictionaries “under the hood”?

8 of 57

Multiple data structures can implement the same ADT

Binary Search Tree

Data Structures

Linked List

Hash Tables

List

Finite number of elements

(same element may occur more than once)

Two arrays (sorted)

Stack

Last in first out (LIFO) operations

Symbol Table

Associates a key with a value

Graph

A set of vertices, pairs of which are connected by edges

Queue

First in first out (FIFO) operations

String[] keys;

int[] values;

Mon

Mon

Today!

Lab 6: Implement!

Fri

ADT

9 of 57

LinkedListST.java

put

💻

10 of 57

Analysis of Symbol Table’s Data Structures

Data Structure

put()

get()

(Unordered) Linked List

O(n)

O(n)

Two arrays (keys sorted, binary search)

Binary Search Tree

Hash Table (with separate chaining)

Hash table (with linear probing)

11 of 57

Analysis of Symbol Table’s Data Structures

Data Structure

put()

get()

(Unordered) Linked List

O(n)

O(n)

Two arrays (keys sorted, binary search)

O(n)

O(log n)

Binary Search Tree

Hash Table (with separate chaining)

Hash table (with linear probing)

Preview for Binary Search Trees: Can we do better for put?

12 of 57

  • Wrap-Up Symbol Table Data Structure #2: Two arrays (sorted)
  • Overview of Binary Search Trees (BST)
  • BST put operation
  • BST get operation

🎯 Today’s Learning Objectives

13 of 57

Binary Trees

A binary tree consists of nodes and links. Each node has two exactly two links and two “children” nodes.

(upside down tree)

root node

node that is the right child of root

a left link

a right link

null links

a subtree

14 of 57

Binary Search Tree

For a binary search tree, each key in any node is

  • Larger than the keys in all nodes in that node’s left subtree
  • Smaller than keys in all nodes in that node’s right subtree

'S'

'E'

'X'

'A'

'C'

'R'

'H'

root node

the key of this node is 'E'

'A' < 'E' && 'C' < 'E'

'R' > 'E' && 'H' > 'E'

left subtree

right subtree

15 of 57

Binary Search Tree to implement a Symbol Table

To implement a Symbol Table with a Binary Search Tree:

  • The keys of nodes still obey the left subtree/right subtree ordering
  • And nodes also have an associated value.

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

16 of 57

Binary Search Tree: Inner Node Class

public class BST<Key, Value> implements SymbolTable<Key, Value> {

private Node root; // root node of BST

private int numPairs; // number of key-value pairs in the symbol table

private class Node {

private Key key; // BSTs maintain left/right subtree ordering by key

private Value val; // value for the node

private Node left; // node at the beginning of the left subtree

private Node right; // node at the beginning of the right subtree

...

The beginning of a Java implementation of a Binary Search Tree

17 of 57

  1. What will mystery(root) print for the example below?
  2. In general, what is this method doing?

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

💡Think-pair-share

18 of 57

  • Wrap-Up Symbol Table Data Structure #2: Two arrays (sorted)
  • Overview of Binary Search Trees (BST)
  • BST get operation
  • BST put operation

🎯 Today’s Learning Objectives

19 of 57

BST get (psuedocode)

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

  • If the target is less than the key at the current node, move to the left subtree.
  • Else if the target is greater than the key at the current node, move to the right subtree.
  • Else if the target equals the key at the current node, return the value (search hit)
  • Else return null (search miss)

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

Can implement either recursively (in most classic implementations) or iteratively

20 of 57

BST get example, search hit

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

Example: Search for ‘C’ (search hit).

  • If the target is less than the key at the current node, move to the left subtree.
  • Else if the target is greater than the key at the current node, move to the right subtree.
  • Else if the target equals the key at the current node, return the value (search hit)
  • Else return null (search miss)

21 of 57

BST get example, search miss

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

  • If the target is less than the key at the current node, move to the left subtree.
  • Else if the target is greater than the key at the current node, move to the right subtree.
  • Else if the target equals the key at the current node, return the value (search hit)
  • Else return null (search miss)

Example: Search for ‘Q’ (search miss).

22 of 57

  • Wrap-Up Symbol Table Data Structure #2: Two arrays (sorted)
  • Overview of Binary Search Trees (BST)
  • BST get operation
  • BST put operation
  • BST analysis

🎯 Today’s Learning Objectives

23 of 57

BST put example

BST<Character, Integer> bst = new BST<Character, Integer>();

bst.put(‘S’, 30);

bst.put(‘E’, 105);

bst.put(‘L’, 76);

Q: Where should we insert the ‘L’ key?

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

24 of 57

BST put example

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

Step 1. Search for ‘L’. This ends in a null link.

BST<Character, Integer> bst = new BST<Character, Integer>();

bst.put(‘S’, 30);

bst.put(‘E’, 105);

bst.put(‘L’, 76);

25 of 57

BST put example

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

Step 1. Search for ‘L’. This ends in a null link.

Step 2. Create new node and update the link of the parent node.

'L’

76

BST<Character, Integer> bst = new BST<Character, Integer>();

bst.put(‘S’, 30);

bst.put(‘E’, 105);

bst.put(‘L’, 76);

26 of 57

BST put algorithm example (key already exists)

BST<Character, Integer> bst = new BST<Character, Integer>();

bst.put(‘S’, 30);

bst.put(‘E’, 105);

bst.put(‘L’, 76);

bst.put(‘A’, 100);

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

Step 1. Search for ‘A’. This ends in a node that already exists.

Step 2. Update the value at that node.

'L’

76

100

27 of 57

  • Wrap-Up Symbol Table Data Structure #2: Two arrays (sorted)
  • Overview of Binary Search Trees (BST)
  • BST get operation
  • BST put operation
  • BST analysis

🎯 Today’s Learning Objectives

28 of 57

Suppose we are searching target key, T. How many comparisons (comparing target T to another node’s key) will be made for each of the following binary search trees?

Tree 2

Tree 1

Tree 3

Notes:

  • The same 7 nodes are in each of the three trees
  • T is a character (not a generic 🤣)

💡Think-pair-share

29 of 57

Binary Search Trees

Advantages:

  • BSTs allow us to keep the keys in order which makes sorting easy and cheap
  • On average, put and get are O(log n) (a balanced BST)
  • In-order traversal (in ascending order) is O(n)

Disadvantages:

  • The running times of BST algorithms depend on the tree’s shape (balanced?), which, in turn, depend on the order in which keys are inserted.
  • Worse case for put and get is still O(n)

30 of 57

Analysis of Symbol Table’s Data Structures

Data Structure

put

get

(Unordered) Linked List

O(n)

O(n)

Two arrays (keys sorted, binary search)

O(n)

O(log n)

Binary Search Tree

Ave = O(log n)

Worst = O(n)

Ave = O(log n)

Worst = O(n)

Hash Table (with separate chaining)

Hash table (with linear probing)

31 of 57

  • Wrap-Up Symbol Table Data Structure #2: Two arrays (sorted)
  • Overview of Binary Search Trees (BST)
  • BST get operation
  • BST put operation
  • BST analysis

🎯 Today’s Learning Objectives

32 of 57

33 of 57

34 of 57

  • What will mystery(root) print for the example below?
  • In general, what is this method doing?

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

Answer

💡Think-pair-share

35 of 57

Review: Call Stack (Lecture 12)

A call stack tracks method calls during program execution

  • Method call: push stack frame (local environment and return address)
  • Method finishes execution (e.g., returns) : pop stack frame

Example:

Call stack: LIFO, Last method called is the first to be completed and removed from the stack

public static int fib(int n) {

if (n == 0|| n == 1) {

return n;

} else {

return fib(n - 1) + fib(n - 2);

}

}

36 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

Call stack: LIFO

>>

x.key

‘S’

x.val

30

Values of local variables for this stack frame

💡Think-pair-share

37 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>>

x.key

‘S’

x.val

30

Call stack: LIFO

💡Think-pair-share

38 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>>

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

Call stack: LIFO

💡Think-pair-share

39 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>>

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

Call stack: LIFO

💡Think-pair-share

40 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>>

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘A’

x.val

35

Call stack: LIFO

💡Think-pair-share

41 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>>

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘A’

x.val

35

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x

null

Execution finishes so we pop this stack frame

Call stack: LIFO

💡Think-pair-share

42 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>>

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘A’

x.val

35

Control flow moves back to stack frame with node ‘A’

Call stack: LIFO

💡Think-pair-share

43 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘A’

x.val

35

Call stack: LIFO

💡Think-pair-share

44 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘A’

x.val

35

Call stack: LIFO

💡Think-pair-share

45 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘A’

x.val

35

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘C’

x.val

12

Call stack: LIFO

💡Think-pair-share

46 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘A’

x.val

35

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘C’

x.val

12

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x

null

Execution finishes so we pop this stack frame

Call stack: LIFO

💡Think-pair-share

47 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35 12

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘A’

x.val

35

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘C’

x.val

12

Call stack: LIFO

💡Think-pair-share

48 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35 12

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘A’

x.val

35

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘C’

x.val

12

Call stack: LIFO

💡Think-pair-share

49 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35 12

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘A’

x.val

35

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘C’

x.val

12

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x

null

Call stack: LIFO

💡Think-pair-share

50 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35 12

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘A’

x.val

35

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘C’

x.val

12

Execution finishes so we pop this stack frame

Call stack: LIFO

💡Think-pair-share

51 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35 12

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘A’

x.val

35

Execution finishes so we pop this stack frame

Call stack: LIFO

💡Think-pair-share

52 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35 12

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

Control flow moves back to method call with node ‘E’

Call stack: LIFO

💡Think-pair-share

53 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35 12 105

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

Call stack: LIFO

💡Think-pair-share

54 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35 12 105

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

Call stack: LIFO

💡Think-pair-share

55 of 57

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35 12 105

x.key

‘S’

x.val

30

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘E’

x.val

105

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

x.key

‘R’

x.val

45

Many more method calls later…

Call stack: LIFO

💡Think-pair-share

56 of 57

'E'

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

>> 35 12 105 24 45 30 106

Many more method calls later…

A

C

E

H

R

S

X

Call stack: LIFO

💡Think-pair-share

57 of 57

  • What will mystery(root) print for the example below?
  • In general, what is this method doing?

public static void mystery(Node x) {

if (x != null) {

mystery(x.left);

System.out.print(x.val + " ");

mystery(x.right);

}

}

'E'

root node

the key of this node is ‘E’

105

'S'

30

'X'

106

'A'

35

'C'

12

'H'

24

'R'

45

the value of this node is 105

Answer #1:

>> 35 12 105 24 45 30 106

A

C

E

H

R

S

X

Answer #2: In-order traversal of keys (ascending/alphabetical order) and printing their corresponding values

💡Think-pair-share