1 / 9

Trees

Trees. Some Trees and Graphs. root. root. 6. 6. 5. 5. 8. 8. 3. 3. 7. 7. 9. 9. root. root. 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. parent nodes.

Télécharger la présentation

Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Trees Trees

  2. Some Trees and Graphs root root 6 6 5 5 8 8 3 3 7 7 9 9 root root 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 parent nodes A Tree - each child node has a single parent node, except for the root which has no parent children nodes right sub tree left sub tree Trees leaf nodes

  3. Binary Trees left data root right 6 5 8 3 7 9 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. 6 root root root root 6 6 6 4 4 4 8 8 8 3 3 3 5 5 5 7 7 7 9 9 9 BST Operations root boolean b = t.search(new Integer(3)); BST t = new BST(); t.insert(new Integer(6)); root 6 t.insert(new Integer(4)); 4 root 6 b = t.search(new Integer(5)); 4 t.insert(new Integer(3)); root 3 6 4 8 t.insert(new Integer(8)); 3 root 6 t.insert(new Integer(8)); 4 8 b = t.search(new Integer(2)); 3 root 6 t.insert(new Integer(5)); 4 8 5 3 Trees

  5. BST Operations - Traversals root 6 4 8 3 5 7 9 Post-order Traversal: left sub tree - Right sub tree - Root 3 - 5 - 4 - 7 - 9 - 8 - 6 Pre-order Traversal: Root - left sub tree - Right sub tree 6 - 4 - 3 - 5 - 8 - 7 - 9 In-order Traversal: left sub tree - Root - Right sub tree 3 - 4 - 5 - 6 - 7 - 8 - 9 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. 6 6 6 6 6 6 4 4 4 4 4 4 9 9 9 9 9 9 3 5 3 3 3 3 3 5 5 5 5 5 2 2 2 2 2 2 BST Operations - Removing a node remove(2) => no children remove(3) => one child remove(6) two children Replace by the right-most node in the left sub-tree Trees

  7. Binary Search Trees left data right 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 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; return node; } if (node.data.compareTo(item) > 0) node.left = removeHelper(item, node.left); if (node.data.compareTo(item) < 0) node.right = removeHelper(item, node.right); return node; } 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

More Related