CS 231 Data Structures and Algorithms Fall 2018 Binary Search Trees Lecture 23 October 29, 2018 Prof. Zadia Codabux 1
Agenda Ternary Operator Binary Search Tree Node based implementation Complexity 2
Administrative None. 3
Ternary Operator 4
Ternary Operator Java supports an interesting operator that can replace short if/else statements like this: if (<Condition>) <Evaluate if true> else <Evaluate if false> with <Condition>? <Evaluate if true> : <Evaluate if false>; 5
Ternary Operator This can make your code more concise. For example, let's say we had int a = 4; and int b = 5; and we wanted to determine which one was the minimum. We could write: int min = (a < b)? a : b; 6
Ternary Operator public static void main(string[] args) { int a = 4; int b = 5; int min = (a < b)? a : b; System.out.println(min); } 7
Binary Search Tree 8
Binary Search Trees (BST) A binary search tree is a sorted binary tree in which the key field value of the root node is greater than the key field values of all of the nodes in the root's left subtree, and less than the key field values of all of the nodes in the root's right subtree. Is each subtree in the tree also a BST? 9
Example The value of the key field of the tree's root node, 50, is greater than all the keys in its left subtree (40, 35, 47, and 43), and it is also less than all the keys in its right subtree (63, 55, 70, 68, and 80). In addition, all subtrees in the tree are also binary trees, which can be verified by inspecting them. For example, consider the subtree whose root node is 63. This subtree is also a binary search tree because all keys in 63's left subtree (55) are less than 63, and all of the keys in the root's right subtree (70, 68, and 80) are greater than 63. 10
Traversals of BST In-order traversal: 1 2 3 4 5 6 7 8 9 Post-order traversal: 1 2 4 3 6 7 9 8 5 Pre-order traversal: 5 3 2 1 4 8 7 6 9 11
Key-Value Pairs Binary search trees generally store more general data than just ints. They often store data as key-value pairs. Think of a BST node as a look-up table: It maps keys to values that we store. Key/value pairs are also called entries. Key Value Jane 18 Jack 25 12
Key-Value Pairs Often, we organize the BST according to the keys. Once we find the key in the tree (name), we look up the associated value (age). Key tend to be compact and helpful for looking up the value --- usually Integers or Strings. Unique keys map to unique values (i.e. duplicate keys return the same value). The value could be a single piece of data (a string, number, etc), arrays, or even whole data structures. 13
Key-Value Pairs class KeyValuePair { private K key; private V value; public KeyValuePair(K key, V value) { this.key = key; this.value = value; } public K getkey() { return key; } public V getvalue() { return value; } public void setvalue(v value) { this.value = value; } } 14
Complexity put(int n): O(n) get(int n): O(n) sum(): O(n) 15
Node-Based BST For the implementation, we ll use a Node class that will store int values and keep a reference to each child: class TreeNode { private int data; private TreeNode left; private TreeNode right; public TreeNode (int data, TreeNode left, TreeNode right) { this.data = data; this.left = left; this.right = right; } } 16
Node-Based BST Then, let s add the starting node (root) of our tree. public class BinaryTree { private TreeNode root; public BinaryTree() { root = null; } } 17
Node-Based BST Methods in Node-Based Tree put: if new value is larger than the root s value, it is the left child of the root; otherwise, right child. sum: sum all node values in this NodeBasedTree The sum of a tree is the sum of its left subtree plus the sum of its right subtree plus the root value. When calculate the sum of left subtree, it repeat the same process for the left subtree. It is a recursive function. printinorder: print the traverse of the tree in inorder 18
put(int data) First, we have to find the place where we want to add a new node in order to keep the tree sorted. We ll follow these rules starting from the root node: if the new node s value is lower than the current node s, we go to the left child if the new node s value is greater than the current node s, we go to the right child when the current node is null, we ve reached a leaf node and we can insert the new node in that position 19