Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees Trees.

Similar presentations


Presentation on theme: "Trees Trees."— Presentation transcript:

1 Trees Trees

2 a binary tree a Binary search Tree not a tree
Some Trees and Graphs root root root 9 6 5 3 8 7 root root 6 6 5 6 8 7 7 8 5 4 3 2 8 3 7 7 a binary tree a tree a tree and a ... a Binary search Tree not a tree Terminology 9 6 5 3 8 7 root A Tree - each child node has a single parent node, except for the root which has no parent parent nodes children nodes right sub tree left sub tree Trees leaf nodes

3 Binary Trees root A tree where each node has at most 2-children
9 6 5 3 8 7 root left right data A tree where each node has at most 2-children Binary Search Trees(BST) A binary search tree is a binary tree where the elements of the left sub-tree are smaller than the root, and the elements of the right sub-tree are larger than the root. Standard processes on a BST: public interface BinarySearchTree{ public void inOrderTraversal(); public void preOrderTraversal(); public void postOrderTraversal(); public void insert(Comparable item); public boolean search(Comparable item); public void remove(Comparable item); } Traversals x 3 Insert to tree Remove from tree Search tree Trees

4 BST Operations BST t = new BST();
root boolean b = t.search(new Integer(3)); 4 9 6 3 8 7 root 5 root 6 t.insert(new Integer(6)); root 6 t.insert(new Integer(4)); 4 root 6 b = t.search(new Integer(5)); 4 4 9 6 3 8 7 root 5 t.insert(new Integer(3)); root 3 6 t.insert(new Integer(8)); 4 8 3 root 6 t.insert(new Integer(8)); 4 8 b = t.search(new Integer(2)); 4 9 6 3 8 7 root 5 3 root 6 t.insert(new Integer(5)); 4 8 3 5 Trees

5 BST Operations - Traversals
4 9 6 3 8 7 root 5 Post-order Traversal: left sub tree - Right sub tree - Root Pre-order Traversal: Root - left sub tree - Right sub tree In-order Traversal: left sub tree - Root - Right sub tree What traversal technique should be employed if you wanted to save the contents of the tree in order to recreate the same tree at a later time? Trees

6 BST Operations - Removing a node
6 4 5 9 3 2 6 4 5 9 3 2 remove(2) => no children 6 4 5 9 3 2 6 4 5 9 3 2 remove(3) => one child 6 4 5 9 3 2 6 4 5 9 3 2 remove(6) two children Replace by the right-most node in the left sub-tree Trees

7 Binary Search Trees Animation of In order Traversal Trees left right
data import java.util.*; public class BST implements BinarySearchTree{ class TreeNode{ TreeNode left; TreeNode right; Comparable data; TreeNode(Comparable d){ data = d; left = null; right = null;} } TreeNode root; public BST(){ root = null;} public void inOrderTraversal(){ inOrderTraversalHelper(root);} private void inOrderTraversalHelper(TreeNode node){ if (node != null){ inOrderTraversalHelper(node.left); System.out.println(node.data.toString()); inOrderTraversalHelper(node.right); //pre and post order traversals very similar to inorder public boolean search(Comparable item){ return searchHelper(item,root); private boolean searchHelper(Comparable target, TreeNode node){ if (node == null) return false; if (target.compareTo(node.data) == 0) return true; if (target.compareTo(node.data) > 0) return searchHelper(target,node.right); return searchHelper(target, node.left); Animation of In order Traversal Trees

8 Binary Search Trees Trees public void insert(Comparable item){
if (search(item)) return;//prevent duplicates root = insertHelper(item,root); } private TreeNode insertHelper(Comparable item, TreeNode node){ if (node == null) return new TreeNode(item); if (node.data.compareTo(item)>0) node.left = insertHelper(item,node.left); else node.right = insertHelper(item,node.right); return node; public void remove(Comparable item) throws NoSuchElementException{ root = removeHelper(item,root); private TreeNode removeHelper(Comparable item, TreeNode node) throws NoSuchElementException{ if (node == null) throw new NoSuchElementException(); if (node.data.compareTo(item) == 0){ if (node.left == null) return node.right; if (node.right == null) return node.left; Comparable data = findRightMostDataValue(node.left); node.left = removeHelper(data, node.left); node.data = data; if (node.data.compareTo(item) > 0) node.left = removeHelper(item, node.left); if (node.data.compareTo(item) < 0) node.right = removeHelper(item, node.right); private Comparable findRightMostDataValue(TreeNode node){ if (node.right == null) return node.data; else return findRightMostDataValue(node.right); Trees

9 Why Trees? Improve Insert/Search/Delete performance over linear structures. If the tree is “well balanced”, then expected performance: Insert/Delete/Search O(log2n) (Under what circumstance?) Worst case times for a BST: Insert/Delete/Search O(n) (Under what circumstance?) Other tree structures: Goal is to grow a well balanced trees that are not too tall Trees where nodes have more than 2-children Trees that detect and modify themselves when they become unbalanced Trees that are grown from the bottom up - thereby ensuring balanced growth. Trees


Download ppt "Trees Trees."

Similar presentations


Ads by Google