1 / 12

Tree

Tree. A recursive data structure that simulates a hierarchical tree A tree has zero or one root node A node has links to its sub-trees A sub-tree has only one incoming link. A tree is a graph with no cycle (i.e., no loop) Related terms

burton
Download Presentation

Tree

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. Tree • A recursive data structure that simulates a hierarchical tree • A tree has zero or one root node • A node has links to its sub-trees • A sub-tree has only one incoming link. • A tree is a graph with no cycle (i.e., no loop) • Related terms • A parent has a directed link to each of its children • There is a path of links from an ancestor to each of its descendants • A leaf node is a node with no child • The depth of a node is the number of links in the path from the root to that node • The height of a node is the number of links in the longest path from that node to a leaf node

  2. R Tree Example A O N U P C M D G • Which one is the root? • Which ones are leaves? • What is P’s depth/height? • What is the tree height? • i.e., maximum height B E Y class TreeNode<AnyType> {AnyType element; TreeNode[] children;} class Tree<AnyType> {TreeNode<AnyType> root;}

  3. R Tree Example (2) A O N U P C M D G • How to change ‘E’ to ‘T’? root.children[0].children[1]. children[1].children[2].data = ‘T’; • How to find tree’s height? public intcomputeHeight(TreeNode<AnyType> tree) { if (tree== null || tree.children.length== 0) return 0; else {intmax_height = 0; for (inti=0; i<tree.children.length; ++i) {intchild_height = computeHeight(tree.children[i]);max_height = Math.max(max_height, child_height); } return 1 + max_height;} • What about tree size? B E Y

  4. Binary Tree A B C P Y K Z E • For a binary tree of size n, what are the possible heights? • Minimum = ___________________________ • Maximum = ___________________________ D class BinaryNode<AnyType> {AnyType element; BinaryNode<AnyType> left; BinaryNode<AnyType> right;}class BinaryTree<AnyType> {BinaryNode<AnyType> root;} • public static <AnyType> int height(BinaryNode<AnyType> t) { if (t == null) return 0; if (t.left == null && t.right == null) return 0; return 1 + Math.max(height(t.left), height(t.right));}

  5. Binary Search Tree (BST) • A BST is a binary tree where each node stores an element with the following representation invariants • If r is the root, every element in the left sub-tree is r.element • … and every element in the right sub-tree is r.element • … and the left and the right sub-trees of the r are BSTs P F S W R H B M A

  6. BST insert protected BinaryNode<AnyType> insert( AnyType x, BinaryNode<AnyType> t ) {if( t == null)t = newBinaryNode<AnyType>( x );elseif( x.compareTo( t.element ) < 0 )t.left = insert( x, t.left);// recursiveelseif( x.compareTo( t.element ) > 0 )t.right = insert( x, t.right);// recursiveelsethrownewDuplicateItemException( x.toString( ) ); // Duplicatereturn t; } What’s Big-O? P F S W R H B M A

  7. BST find privateBinaryNode<AnyType> find( AnyType x, BinaryNode<AnyType> t ) {while( t != null) {if( x.compareTo( t.element ) < 0 )t = t.left;elseif( x.compareTo( t.element ) > 0 )t = t.right;elsereturn t; // Match }returnnull; // Not found } What’s Big-O? P F S W R H B M A

  8. BST remove • remove() has 3 cases • Remove leaf node ---- easy! • Remove node with one child ---- still quite easy ! • Remove node with two children ---- many possibilities! • Suggested solution • Step 1: find the smallest element in the right sub-tree • Step 2: replace the element of this node with that smallest element • Step 3: Make sure we remove that smallest element from the right sub-tree recursive!

  9. BST remove protectedBinaryNode<AnyType> remove( AnyType x, BinaryNode<AnyType> t ) {if( t == null) // case 1thrownewNoSuchElementException( x.toString( ) );if( x.compareTo( t.element ) < 0 ) t.left = remove( x, t.left); // recursiveelseif( x.compareTo( t.element ) > 0 ) t.right = remove( x, t.right); // recursiveelseif( t.left != null && t.right != null ) // case 3 {t.element = findMin( t.right ).element;t.right = remove( t.element, t.right ); }elset = ( t.left != null ) ? t.left : t.right; // case 2return t; } What’s Big-O? P F S W R H B M A

  10. Cost Analysis

  11. Balanced Tree • Balanced tree is a tree data structure that tries to minimize its height through balancing its left and right sub-tree • Why? • Many many implementations of balanced tree • Splay tree • http://www.link.cs.cmu.edu/splay/ • Always use balanced tree! • Wikipedia helps 

  12. Common kinds of trees* means balanced tree • Binary search tree (BST) • Simple BST • *Splay tree • *AVL tree • *Red-black tree • *B-Tree • Trie • Other hierarchical structures • File system • Huffman tree • Animated Cartoon Characters P F S W R H B M A Source: http://en.wikipedia.org/wiki/File:B-tree.svg Author: Cyhawk Source: http://en.wikipedia.org/wiki/File:Patricia_trie.svg Author: Claudio Rocchini

More Related