Java Methods ObjectOriented Programming and Data Structures 2
Java Methods Object-Oriented Programming and Data Structures 2 nd AP edition with Grid. World Maria Litvin ● Gary Litvin Binary Trees Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. 1
Objectives: • Learn about binary trees • Learn how to represent and handle a binary tree using the Tree. Node class • Learn about binary search trees • Review sets and maps, and the java. util classes that implement them 2
Some Applications of Trees • • • Data retrieval (search) Priority queues Decision systems Hierarchies Games 3
Binary Tree Terms Root Left child Leaves (nodes with no children) Right child node Number of levels (equals 5 here) node’s right subtree 4
Binary Tree Properties • A shallow tree can hold many nodes. For example, a binary tree with 20 levels can have 220 - 1 (approximately 1, 000) nodes. • At each node a decision can be made on where to proceed, left or right (used in binary search trees). • The path to the bottom is relatively short as compared to the total number of nodes. 5
The Tree. Node Class • Represents a node of a binary tree • Is similar to List. Node (Chapter 21) only instead of next has left and right public class Tree. Node { private Object value; private Tree. Node left; private Tree. Node right; . . . Holds a reference to the left child Holds a reference to the right child 6
The Tree. Node Class (cont’d) . . . // Constructors: public Tree. Node (Object v) { value = v; left = null; right = null; } public Tree. Node (Object v, Tree. Node lt, Tree. Node rt) { value = v; left = lt; right = rt; } // Methods: public Object get. Value ( ) { return value; } public Tree. Node get. Left ( ) { return left; } public Tree. Node get. Right ( ) { return right; } public void set. Value (Object v) { value = v; } public void set. Left (Tree. Node lt) { left = lt; } public void set. Right (Tree. Node rt) { right = rt; } } 7
Trees and Recursion • The tree structure is recursive by nature — the left and right subtrees are smaller trees: private void traverse (Tree. Node root) { // Base case: root == null, // the tree is empty -- do nothing if (root != null) // Recursive case { process (root. get. Value ( )); traverse (root. get. Left ( )); traverse (root. get. Right ( )); } } 8
Tree. Node Example 1 public int count. Nodes (Tree. Node root) { Base case if (root == null) return 0; else return 1 + count. Nodes (root. get. Left ( )) + count. Nodes (root. get. Right ( )); } (for the root) 9
Tree. Node Example 2 // returns a reference to a new tree, which is a // copy of the tree rooted at root public Tree. Node copy (Tree. Node root) { if (root == null) return null; else return new Tree. Node (root. get. Value ( ), copy (root. get. Left ( )), copy (root. get. Right ( ))); } 10
Traversals • Preorder: first process the root, then traverse the left and right subtrees. private void traverse. Preorder (Tree. Node root) { if (root != null) { process (root. get. Value()); traverse. Preorder (root. get. Left( )); traverse. Preorder (root. get. Right( )); } } A / B C / D E A B D E C 11
Traversals (cont’d) • Inorder: first traverse the left subtree, then process the root, then traverse the right subtree. private void traverse. Inorder (Tree. Node root) { if (root != null) { traverse. Inorder (root. get. Left( )); process (root. get. Value( )); traverse. Inorder (root. get. Right( )); } } A / B C / D E D B E A C 12
Traversals (cont’d) • Postorder: first traverse the left and right subtrees, then process the root. private void traverse. Postorder (Tree. Node root) { if (root != null) { traverse. Postorder (root. get. Left( )); traverse. Postorder (root. get. Right( )); process (root. get. Value( )); } } A / B C / D E D E B C A 13
Traversals (cont’d) • Preorder: root left right 1 2 • Inorder: left root right 2 1 • Postorder: left right root 3 3 3 1 2 14
Binary Search Trees (BST) • BST contains Comparable objects (or a comparator is supplied). • For each node, all the values in its left subtree are smaller than the value in the node and all the values in its right subtree are larger than the value in the node. a BST 15 / 8 20 / 1 12 not a BST 15 / 12 20 / 1 8 15
BST (cont’d) • BSTs combine the benefits of sorted arrays for quick searching and linked lists for inserting and deleting values. 16
BST (cont’d) Recursive contains // root refers to a BST; the nodes hold Strings private boolean contains (Tree. Node root, String target) { if (root == null) return false; int diff = target. compare. To ((String) root. get. Value ( )); if (diff == 0) return true; else if (diff < 0) return contains (root. get. Left ( ), target); else // if (diff > 0) return contains (root. get. Right ( ), target); } 17
BST (cont’d) Iterative contains private boolean contains (Tree. Node root, String target) { Tree. Node node = root; while ( node != null ) { int diff = target. compare. To (node. get. Value ()); if (diff == 0) return true; else if (diff < 0) node = node. get. Left (); else // if diff > 0 node = node. get. Right (); } return false; } 18
A BST Class public class My. Tree. Set { private Tree. Node root; . . . private boolean contains (Object target) { return contains (root, target); } Private recursive helper method private boolean contains (Tree. Node root, Object target) { if (root == null) return false; . . . } 19
BST (cont’d) The smallest node The largest node 20
Adding a Node Private recursive helper method private Tree. Node add (Tree. Node node, Object value) { if (node == null) node = new Tree. Node(value); else { int diff = ((Comparable<Object>)value). compare. To (root. get. Value( )); if (diff < 0) root. set. Left (add (root. get. Left( ), value)); else // if (diff > 0) root. set. Right (add (root. get. Right( ), value)); } return node; } 21
BST: Removing the Root Node Step 1: Find the smallest node of the right subtree Step 2: Unlink that node and promote its right child into its place 50 50 30 25 60 75 40 60 20 90 30 25 70 40 20 69 75 90 70 69 72 72 50 Step 3: Link that note in place of the root and remove the old root 60 30 25 20 75 40 90 70 69 72 22
BST (cont’d) • When the tree is balanced (“bushy”) the add, remove, and contains methods take O(log n) time, where n is the number of nodes in the tree. • If nodes are added randomly, the BST can degenerate into a nearly linear shape. • More sophisticated algorithms help keep the tree balanced. 23
java. util. Tree. Set<E> • Is implemented as a balanced BST. • compare. To (or comparator’s compare) is used by the add, contains, and remove methods. • An iterator returned by the iterator method implements inorder traversal. • Inorder traversal of any BST retrieves values in ascending order. 24
java. util. Tree. Set<E> (cont’d) Never mind. . . 25
java. util. Tree. Map<K, V> • Is implemented as a BST for keys. • compare. To (or comparator’s compare) for keys is used by the put, get, contains. Key, and remove methods. • The key. Set method returns a Tree. Set<K>. 26
java. util. Tree. Map<K, V> (cont’d) 27
Review: • • • Name a few applications of trees. Why is recursion applicable to trees? What is a leaf of a tree? What is a subtree? Which property makes binary trees suitable for data retrieval applications? 28
Review (cont’d): • What is the largest possible number of nodes in a binary tree with 10 levels? • Can the Tree. Node class be used to represent a node in a doubly-linked list? • What is the sequence of values in preorder and postorder traversals of the following tree? A / B C / D E 29
Review (cont’d): • What is a Binary Search Tree? • Inorder traversal of a BST has a neat property. What is it? • Which of the following trees are BSTs? 3 / 1 5 / 2 4 1 / 2 3 / 4 5 4 / 2 5 / 1 3 30
- Slides: 30