140 likes | 482 Views
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
E N D
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
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;}
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
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));}
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
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
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
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!
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
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
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