course-web-page-Fall-2021

Repository for the Fall 2021 course web page

View the Project on GitHub IUDataStructuresCourse/course-web-page-Fall-2021

Lecture: Binary Trees

Reminder: the difference between identity and equality in Java.

IntelliJ tip: reformat code

Motivation for Binary Search Trees

Taking stock with respect to Flood-it! and the Set ADT

  array linked list sorted array
membership test O(n) O(n) O(log(n))
insertion O(1) O(1) O(n)

Binary Node and Tree Classes

class BinaryNode<T> {
		T data;
		BinaryNode<T> left;
		BinaryNode<T> right;

		BinaryNode(T d, BinaryNode<T> l, BinaryNode<T> r) {
				data = d; left = l; right = r;
		}
}

public class BinaryTree<T> {
		BinaryNode<T> root;

		public BinaryTree() { root = null; }
		// ...
}

We can traverse a binary tree in several different ways:

Example:

			 10
			/ \
		   /   \
		  5     14
		 / \     \
		2   7     19

		pre:  10,5,2,7,14,19
		in:   2,5,7,10,14,19
		post: 2,7,5,19,14,10

Next and Previous operations with respect to inorder traversal

Running example binary tree:

      8
    /  \
   /    \
  3     10
 / \      \
1   6      14
   / \    /
  4   7  13