Principles of Computer Science Binary Trees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 1
Today s Topics Extending LinkedList with Fast Search Sorted Binary Trees Tree Concepts Traversals of a Binary Tree Java Interfaces for Trees Operations on binary trees maketree() search( ) 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 2
What is not great about using Arrays? Very difficult to maintain when the data is dynamic: How do you insert an element after the array is already sorted? How do you delete an element after the array is already sorted? Not easy (can be done using heaps). 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 3
Trees Tree data structures provide a natural way of managing hierarchical organizations 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 4
Hierarchical Organization Example: A university's organization A university's administrative structure. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 5
Tree Terminology A tree is A set of nodes Connected by edges The edges indicate relationships among nodes Nodes are arranged in levels Indicate the nodes' hierarchy Top level is a single node called the root 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 6
Tree Terminology Nodes at a given level are children of nodes at the previous level A node with children is the parent node of those children Nodes having the same parent are siblings A node with no children is a leaf node The only node with no parent is the root node All others have one parent each 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 7
Tree Terminology A node is reached from the root by a path The length of the path is the number of edges that compose it The height of a tree is the number of levels in the tree The subtree of a node is a tree rooted at a child of that node 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 8
Binary Trees Each node has at most two children Three binary trees. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 9
Why Binary Trees? Binary Trees: Support fast insert and delete Support fast sort Support fast search 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 10
Binary Trees A binary tree is either empty or has the following form T left and T right are themselves binary trees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 11
Binary Trees Every nonleaf in a full binary tree has exactly two children A complete binary tree is full to its next-to-last level Leaves on last level are filled from left to right The height of a binary tree with n nodes that is either complete or full is log 2 (n + 1) 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 12
Binary Trees The number of nodes in a full binary tree as a function of the tree's height. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 13
Binary Trees Full Tree Height Number of Nodes The number of nodes in a full binary tree as a function of the tree's height. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 14
Traversals of a Tree Visiting a node Processing the data within a node This is the action performed on each node during the traversal of a tree A traversal can pass through a node without visiting it at that moment For a binary tree Visit the root Visit all nodes in the root's left subtree Visit all nodes in the root's right subtree 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 15
Traversals of a Tree Preorder traversal: visit root before the subtrees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 16
Traversals of a Tree Inorder traversal: visit root between visiting the subtrees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 17
Traversals of a Tree Postorder traversal: visit root after visiting the subtrees These are examples of a depth-first traversal. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 18
Traversals of a Tree Level-order traversal: begin at the root, visit nodes one level at a time This is an example of a breadth-first traversal. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 19
Traversals of a General Tree A general tree has traversals that are Level order Preorder Postorder Inorder traversal is not well defined for a general tree 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 20
Traversals of a General Tree (a) preorder. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 21
Traversals of a General Tree (b) postorder. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 22
Java Interfaces for Trees An interface that specifies operations common to all trees 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 23
Java Interfaces for Trees Interface of traversal methods for a tree 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 24
Implement BinaryTree in Java class BinaryTreeNode<T> { public T content; public BinaryTreeNode<T> leftchild; public BinaryTreeNode<T> rightchild; } <lots of methods> class BinaryTree<T> { public BinaryTreeNode<T> root; } <lots of methods> 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 25
Binary Tree Consists of a root node. If a node is a leaf node if: leftchild = null, and rightchild = null. 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 26
Binary Tree Traversal In Order class BinaryTree { BTNode root;... public void printinorder() { _printinorder(root); } private void _printinorder(btnode n) { if (n == null) return; } _printinorder(n.left); // print left subtree first System.out.println(n); // print the current node _printinorder(n.right); // print right subtree next }... 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 27
Binary Tree Traversal Pre Order class BinaryTree { BTNode root;... public void printpreorder() { _printpreorder(root); } private void _printpreorder(btnode n) { if (n == null) return; } System.out.println(n); // print the current node first _printpreorder(n.left); // print left subtree next _printpreorder(n.right); // print right subtree last }... 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 28
Binary Tree Traversal Post Order class BinaryTree { BTNode root;... public void printpostorder() { _printpostorder(root); } private void _printpostorder(btnode n) { if (n == null) return; } _printpostorder(n.left); // print left subtree first _printpostorder(n.right); // print right subtree second System.out.println(n); // print the current node last }... 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 29
Sorted Binary Tree A sorted binary tree is a binary tree with the property: for every node n, Left descendants n <= n < Right descendants of n 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 30
Sorted Binary Trees Sorted binary trees are sometimes called Binary Search Trees These provide log(n) search, insertion and deletion In practice if you think that you need to search through a binary tree, ensure that it is sorted 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 31
In Order Traversal of Sorted Binary Trees In order traversal of a sorted binary tree reaches nodes in increasing (sorted) order In order traversal 1 2 3 4 6 8 Sorted 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 32
Sorted Binary Tree - Insertions public void insertsorted(int value) { if (root == null) { // if this is the first value to be stored, root = new BTNode(value); // set it as the root return; } } _insertsorted(root, value); // else call the helper routine to store this // value at its appropriate location 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 33
Sorted Binary Tree - Insertions private void _insertsorted(btnode n, int value) { if (n.value >= value) { // (1) if value less than current node, go left if (n.left!= null) { // if left child exists, recur _insertsorted(n.left, value); } else { BTNode nn = new BTNode(value); // if nothing at left child, store value as left n.left = nn; // child of this node } } else { // (1) else go right if (n.right!= null) { // if right child exists, recur } } _insertsorted(n.right, value); } else { BTNode nn = new BTNode(value); // if nothing at right child, store value as n.right = nn; // right child of this node } 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 34
Binary Search Trees A search tree organizes its data so that a search is more efficient Binary search tree Nodes contain Comparable objects A node's data is greater than the data in the node's left subtree A node's data is less than the data in the node's right subtree 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 35
Searching Through Sorted Binary Tree Given a sorted binary tree, we need to find the node that contains the value of interest: class BinaryTree { BinaryTreeNode<T> search(t value); } 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 36
Binary Search Through Sorted Tree class BinaryTree<T> {... public BinaryTreeNode<T> search(t value) { return searchatnode(value, this.root); } public BinaryTreeNode<T> searchatnode( T value, BinaryTreeNode<T> node) { if(node == null) return null; } } if(node.content == value) return node; else if(node.content > value) return searchatnode(value, node.leftchild); else return searchatnode(value, node.rightchild); 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 37
Sorted Binary Trees - Deletions Case 1: Deleting a leaf node parent node (parent.left) 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 38
Sorted Binary Trees - Deletions Case 1: Deleting a leaf node This case is easy. Just remove the node. parent.left = null 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 39
Sorted Binary Trees - Deletions Case 2: Deleting a node with a single child (i.e., having only left or right subtree but not both) parent node (parent.left) child (node.right) 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 40
Sorted Binary Trees - Deletions Case 2: Deleting a node with a single child (i.e., having only left or right subtree but not both) Replace the node with its child parent.left = node.left 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 41
Sorted Binary Trees - Deletions Case 3: Deleting a node with two children (i.e., having both left and right subtrees) parent node (parent.left) in order predecessor of parent 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 42
Sorted Binary Trees - Deletions Case 3: Deleting a node with two children (i.e., having both left and right subtrees) If the current node is the left child of the parent, replace node with the in order predecessor of parent node (parent.left) parent 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 43
Sorted Binary Trees - Deletions Case 3: Deleting a node with two children (i.e., having both left and right subtrees) If the current node is the right child of the parent, replace node with the in order successor of parent 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 44
Sorted Binary Trees - Deletions Case 3: Deleting a node with two children (i.e., having both left and right subtrees) Or simply replace the node with either its inorder successor or inorder predecessor 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 45
In Order Predecessor and Successor In order traversal 1 2 3 4 6 8 Sorted In the example above, 4 is in order predecessor of 6 and 8 is in order successor of 6. Simply check how these nodes are printed in in order predecessor 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 46
In Order Successor Input: node Output: succ Case 1: if node.left is not null, succ is the smallest element in the left subtree of node Case 2: otherwise, travel up using parent pointers until you reach a node that is the left child of its parent, the parent node is the succ Don t forget to take care of the special cases, e.g., what if the node has no succ? 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 47
In Order Predecessor Input: node Output: pred Case 1: if node.right is not null, pred is the smallest element in the left subtree of node Case 2: otherwise, travel up using parent pointers until you reach a node that is the right child of its parent, the parent node is the pred Don t forget to take care of the special cases, e.g., what if the node has no pred? 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 48
Summary Tree terminology Tree Traversals Binary Search Tree 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 49
Readings Ch. 17 08/11/2013 CSCI 2010 - Binary Trees - F.Z. Qureshi 50