1 of 34

Section 14

Binary Tree Modification

CSE 123 - Spring 2023 || Fall 2023

Section AK || Section AA

Section 14 - Spring 2023

2 of 34

Agenda

Section 14 - Spring 2023

Announcements and Discussion (5 min)

01

Review (10 min)

02

Practice! (35 min)

03

3 of 34

Section 14 - Spring 2023

Discussion

01

4 of 34

Discussion Questions

Feel free to head over to Ed and enter your responses there!�Find a partner to discuss with too!

Given the following binary search tree, what is one possible ordering that the elements need to be inserted in order to produce this exact tree?

Below is one student's attempt at writing a method insertLeftLeaf() that adds a new leaf node to the given binary tree in the leftmost position.

However, after calling insertLeftLeaf() on an existing binary tree, the student notices that the tree remains unchanged.�Debug this program! What does the student need to add to their code for the method to correctly add a new IntTreeNode in the leftmost position?

Section 14 - Spring 2023

5 of 34

Section 14 - Spring 2023

Binary Search Tree

02

6 of 34

Binary Search Trees

Section 14 - Spring 2023

A Binary Search Tree (BST) is a binary tree with a special sorting property.

BST Conventions:

  • All nodes in left subtree are “less than” the root
  • All nodes in right subtree are “greater than” than the root
  • All subtrees are also BSTs
  • Does not store duplicates (for the purpose of CSE 123)

Motivation:

  • Take advantage of the sorted property and decrease the number of operations (find, insert, delete)

The following visualizations show some valid BST to represent a list of integers [1, 2, 3, 4, 5, 6].

7 of 34

x = change(x)

Section 14 - Spring 2023

public void add(int value) {

overallRoot = add(overallRoot, value);

}�

private IntTreeNode add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

root.left = add(root.left, value);

} else {

root.right = add(root.right, value);

}

return root;

}

public void add(int value) {

add(overallRoot, value);

}�

private void add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

add(root.left, value);

} else {

add(root.right, value);

}

}

8 of 34

x = change(x)

Section 14 - Spring 2023

public void add(int value) {

overallRoot = add(overallRoot, value);

}�

private IntTreeNode add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

root.left = add(root.left, value);

} else {

root.right = add(root.right, value);

}

return root;

}

public void add(int value) {

add(overallRoot, value);

}�

private void add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

add(root.left, value);

} else {

add(root.right, value);

}

}

9 of 34

Section 14 - Spring 2023

change Version 1

10 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

add(overallRoot, value); // value = 4

}�

private void add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

add(root.left, value);

} else {

add(root.right, value);

}

}

11 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

add(overallRoot, value); // value = 4

}�

private void add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

add(root.left, value);

} else {

add(root.right, value);

}

}

overallRoot

12 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

add(overallRoot, value); // value = 4

}�

private void add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

add(root.left, value);

} else {

add(root.right, value);

}

}

root

13 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

add(overallRoot, value); // value = 4

}�

private void add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

add(root.left, value);

} else {

add(root.right, value);

}

}

root

14 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

add(overallRoot, value); // value = 4

}�

private void add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

add(root.left, value);

} else {

add(root.right, value);

}

}

root

15 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

add(overallRoot, value); // value = 4

}�

private void add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

add(root.left, value);

} else {

add(root.right, value);

}

}

root

16 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

add(overallRoot, value); // value = 4

}�

private void add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

add(root.left, value);

} else {

add(root.right, value);

}

}

root

17 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

add(overallRoot, value); // value = 4

}�

private void add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

add(root.left, value);

} else {

add(root.right, value);

}

}

root

4

18 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

add(overallRoot, value); // value = 4

}�

private void add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

add(root.left, value);

} else {

add(root.right, value);

}

}

root

4

Oh no! The tree was never modified! 😭😱😨😩🥴

Let’s fix this.

19 of 34

Section 14 - Spring 2023

change Version 2

20 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

overallRoot = add(overallRoot, value); // value = 4

}�

private IntTreeNode add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

root.left = add(root.left, value);

} else {

root.right = add(root.right, value);

}

return root;

}

21 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

overallRoot = add(overallRoot, value);

}�

private IntTreeNode add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

root.left = add(root.left, value);

} else {

root.right = add(root.right, value);

}

return root;

}

overallRoot

22 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

overallRoot = add(overallRoot, value);

}�

private IntTreeNode add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

root.left = add(root.left, value);

} else {

root.right = add(root.right, value);

}

return root;

}

root

23 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

overallRoot = add(overallRoot, value);

}�

private IntTreeNode add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

root.left = add(root.left, value);

} else {

root.right = add(root.right, value);

}

return root;

}

root

24 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

overallRoot = add(overallRoot, value);

}�

private IntTreeNode add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

root.left = add(root.left, value);

} else {

root.right = add(root.right, value);

}

return root;

}

root

25 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

overallRoot = add(overallRoot, value);

}�

private IntTreeNode add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

root.left = add(root.left, value);

} else {

root.right = add(root.right, value);

}

return root;

}

root

26 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

overallRoot = add(overallRoot, value);

}�

private IntTreeNode add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

root.left = add(root.left, value);

} else {

root.right = add(root.right, value);

}

return root;

}

root

27 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

add(overallRoot, value); // value = 4

}�

private void add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

add(root.left, value);

} else {

add(root.right, value);

}

return root;

}

root

4

28 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

overallRoot = add(overallRoot, value);

}�

private IntTreeNode add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

root.left = add(root.left, value);

} else {

root.right = add(root.right, value);

}

return root;

}

root

4

29 of 34

add

Section 14 - Spring 2023

Goal: Add a node storing the value 4 in this tree�

public void add(int value) {

overallRoot = add(overallRoot, value);

}�

private IntTreeNode add(IntTreeNode root,� int value) {

if (root == null) {

root = new IntTreeNode(value);

} else if (value < root.data) {

root.left = add(root.left, value);

} else {

root.right = add(root.right, value);

}

return root;

}

overallRoot

4

30 of 34

Tips for Binary Tree Modification

Section 14 - Spring 2023

  • Consider the simplest tree (empty? One node?)
    • This is often going to be base case
  • Recursive definition of a tree
    • Handle the empty versus non-empty tree
  • Use public/private method pair as usual! Keep track of the current node (root) in the private helper.
    • At least two recursive calls per trace, one with root.left, and one using the root.right (this makes sense because each subtree is essentially just another binary tree to continue on)
  • NEW: use the x = change(x) paradigm to modify binary trees. Analogous to LinkedLists, the only ways to modify a tree are changing overallRoot, root.left, and/or root.right�(in comparison to front and current.next)

31 of 34

[Starter Programming Problem]

Section 14 - Spring 2023

Remove Leaves

🌿

32 of 34

[Complex Programming Problem]

Section 14 - Spring 2023

Read Tree [Helpful for BrettFeed]

📝

33 of 34

[Starter Programming Problem]

Section 14 - Spring 2023

Complete ToLevel [if time]

🌿

34 of 34

THANKS!

See you next section!

Reach out to me with any questions :)