Tree - PowerPoint PPT Presentation

About This Presentation
Title:

Tree

Description:

preorder. Pick the root first. Then pick subtree. Within subtree, pick its root first, and so on... Inside, it is also preorder. Preorder result is 1,2,3,4, ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 43
Provided by: Toe
Category:
Tags: preorder | tree

less

Transcript and Presenter's Notes

Title: Tree


1
Tree
2
Basic characteristic
3
  • Top node root
  • Left and right subtree
  • Node 1 is a parent of node 2,5,6.
  • Node 2 is a parent of node 3,4
  • Node 3,4 are children of node 2.
  • Node 3 and 4 are siblings.
  • Node 1 is an ancestor of node 7.

4
  • Node that does not have any branch going out
    leaf. In the picture, the leaves are 3, 4, 5 and
    7.
  • Notice there is only 1 path from a root to a
    node.
  • depth of node n is the path length from root to
    n.
  • Example depth of node 7 is 2.
  • Height of n is the maximum path length from n to
    leaf.
  • Example height of node 7 is 0.
  • Height of node 1 is 2.

5
  • Height of a tree
  • If a tree only has root, we let its height be 0.
    For empty tree, we let its height be -1.
  • height of any tree height of its heighest
    subtree 1.
  • level of a node
  • A child of a node has 1 more level than its
    parent.
  • A root has its level 0.

6
If a node can have any number of branches?
  • We can never be sure of the number of branches?
  • Preparing many branches in advance is a waste of
    space.
  • One solution! -gt

7
How to arrange nodes for printing
  • preorder
  • Pick the root first
  • Then pick subtree
  • Within subtree, pick its root first, and so on.

8
Preorder result is 1,2,3,4,5,6,7
First to pick
4th, this subtree
Second, this subtree
Inside, it is also preorder.
Inside, we also view it with preorder.
Third
9
  • postorder
  • View all subtrees, then the root.
  • Inside each subtree, use postorder.
  • The postorder result of our example is
    3,4,2,5,7,6,1.
  • inorder
  • Left subtree comes first, then root, then right
    subtree.
  • Inside each subtree, use inorder.
  • The inorder result of our example is (we do not
    count a subtree that has 5 because we do not know
    which side it will be on) 3,2,4,1,6,7

10
breadth-first search
  • The first 3 methods we have seen are
    tree-searching using depth-first search
  • When looking at each subtree, we must finish with
    the whole of that subtree before we can carry on.
  • breadth-first inspects a tree level by level,
    starting from root.
  • Our example tree then gives the following result.
    -gt 1,2,5,6,3,4,7

11
breadth-first implementation
  • When we look at a node, we record the children of
    that node into a queue created for storing next
    level nodes (next level queue).
  • Example if we have

12
breadth-first implementation(2)
  • When we look at the root, we record itsildren
    into next level queue. Therefore, at this stage,
    next level queue has and .
  • this level queue next level queue clear next
    level queue
  • Inspect this level queue.
  • Find Now we insert the children of into next
    level queue
  • At this stage, next level queue a,
  • Continue inspecting this level queue,
  • we find Now we insert the children of - into
    next level queue
  • At this stage, next level queue a,,d,e
  • No more member in this level queue, therefore let
    this level queue next level queue.
  • At this stage, this level queue a,,d,e
  • Now starting with this level queue again. And so
    on.

13
Binary Tree
  • Each node can have at most 2 branches.
  • It is possible to have one branch at every level,
    meaning it is a linked list (skewed tree).

14
full binary tree or perfectly balanced tree
  • A complete triangle, no missing leaf.

15
complete tree
  • Full up to the level before the highest level.
  • At the highest level, leaves must fill from left
    to right.

Full is complete, but complete is not full.
16
complete tree(2)
  • If we put members of a complete binary tree into
    an array, breadth-first
  • Member at the i_th position will have its left
    child at the 2i1 th position.
  • And its right child will be at 2i2 th position.
  • A parent of i is at the (i-1)/2 th position (no
    decimal)

A N E T O X
Example left child of E must have index
221 5. It is X. The parent of E must have
index (2-1)/2 0
17
two-tree
  • Must be empty tree or
  • All non-leaf must have 2 branches.

18
Binary tree Theory
  • Let
  • leaves(t) be the number of leaves in the tree t.
  • n(t) be the number of nodes of tree t.
  • height(t) be the height of t.
  • leftsubtree(t) be the left subtree of t.
  • rightsubtree(t) be the right subtree of t.
  • max(a,b) be a maximum value from a and b.

19
For a non empty binary tree, we have the
following relations
If t is a two-tree, then
Then t is definitely a two-tree.
If
If t is a full tree,
20
If
, then t is a full tree
21
Proving
base case is when our tree has only its root.

Which is true.
inductive case is when our tree has height h. Let
We must prove that when t has height h1, then
22
We know that
We can substitute n(t) into our equation.
23
Fact about External Path length
  • Let t be a binary tree that has root. External
    Path length, E(t), is the sum of the depth of all
    leaves.
  • If t is a binary tree that has leaves(t) k
    (more than 0), then

24
Binary Search Tree
  • Is an empty tree or a tree that
  • Every member in leftsubtree(t) has smaller value
    than the member at the root.
  • Every member in rightsubtree(t) has larger value
    than the member at the root.
  • Both leftsubtree(t) and rightsubtree(t) are also
    binary search trees.

25
  • Finding a member is easy. Let us try to find 4.
    When we look at each node, we know immediately
    which side of the tree that we should carry on
    looking.
  • Therefore the searching time only depends on the
    height of our tree. The height is a log of n(t).

26
  1. class BinaryNode
  2. // Constructors
  3. BinaryNode( Comparable theElement )
  4. this( theElement, null, null )
  5. BinaryNode( Comparable theElement,
    BinaryNode lt, BinaryNode rt )
  6. element theElement
  7. left lt
  8. right rt
  9. // Friendly data accessible by other
    package routines
  10. Comparable element // The data in
    the node
  11. BinaryNode left // Left child
  12. BinaryNode right // Right child

27
  • // BinarySearchTree class
  • //
  • // CONSTRUCTION with no initializer
  • //
  • // PUBLIC
    OPERATIONS
  • // void insert( x ) --gt Insert x
  • // void remove( x ) --gt Remove x
  • // Comparable find( x ) --gt Return item
    that matches x
  • // Comparable findMin( ) --gt Return smallest
    item
  • // Comparable findMax( ) --gt Return largest
    item
  • // boolean isEmpty( ) --gt Return true if
    empty else false
  • // void makeEmpty( ) --gt Remove all
    items
  • // void printTree( ) --gt Print tree in
    sorted order

private BinaryNode root
28
  1. public class BinarySearchTree
  2. private BinaryNode root
  3. /
  4. Construct the tree.
  5. /
  6. public BinarySearchTree( )
  7. root null
  8. /
  9. Insert into the tree duplicates are
    ignored.
  10. _at_param x the item to insert.
  11. /
  12. public void insert( Comparable x )
  13. root insert( x, root )

29
  • /
  • Remove from the tree. Nothing is done
    if x is not found.
  • _at_param x the item to remove.
  • /
  • public void remove( Comparable x )
  • root remove( x, root )
  • /
  • Find the smallest item in the tree.
  • _at_return smallest item or null if
    empty.
  • /
  • public Comparable findMin( )
  • return elementAt( findMin( root ) )

30
  • /
  • Find the largest item in the tree.
  • _at_return the largest item of null if
    empty.
  • /
  • public Comparable findMax( )
  • return elementAt( findMax( root ) )
  • /
  • Find an item in the tree.
  • _at_param x the item to search for.
  • _at_return the matching item or null if
    not found.
  • /
  • public Comparable find( Comparable x )
  • return elementAt( find( x, root ) )

31
  1. /
  2. Make the tree logically empty.
  3. /
  4. public void makeEmpty( )
  5. root null
  6. /
  7. Test if the tree is logically empty.
  8. _at_return true if empty, false
    otherwise.
  9. /
  10. public boolean isEmpty( )
  11. return root null

32
  1. /
  2. Print the tree contents in sorted
    order.
  3. /
  4. public void printTree( )
  5. if( isEmpty( ) )
  6. System.out.println( "Empty tree"
    )
  7. else
  8. printTree( root )
  9. /
  10. Internal method to get element field.
  11. _at_param t the node.
  12. _at_return the element field or null if t
    is null.
  13. /
  14. private Comparable elementAt( BinaryNode
    t )
  15. return t null ? null t.element

33
  1. /
  2. Internal method to insert into a
    subtree.
  3. _at_param x the item to insert.
  4. _at_param t the node that roots the tree.
  5. _at_return the new root.
  6. /
  7. private BinaryNode insert( Comparable x,
    BinaryNode t )
  8. / 1/ if( t null )
  9. / 2/ t new BinaryNode( x, null, null
    )
  10. / 3/ else if( x.compareTo( t.element ) lt 0
    )
  11. / 4/ t.left insert( x, t.left )
  12. / 5/ else if( x.compareTo( t.element ) gt 0
    )
  13. / 6/ t.right insert( x, t.right )
  14. / 7/ else
  15. / 8/ // Duplicate do nothing
  16. / 9/ return t

34
  1. /
  2. Internal method to remove from a
    subtree.
  3. _at_param x the item to remove.
  4. _at_param t the node that roots the tree.
  5. _at_return the new root.
  6. /
  7. private BinaryNode remove( Comparable x,
    BinaryNode t )
  8. if( t null )
  9. return t // Item not found do
    nothing
  10. if( x.compareTo( t.element ) lt 0 )
  11. t.left remove( x, t.left )
  12. else if( x.compareTo( t.element ) gt 0
    )
  13. t.right remove( x, t.right )
  14. else if( t.left ! null t.right !
    null ) // Two children
  15. t.element findMin( t.right
    ).element
  16. t.right remove( t.element,
    t.right )

35
Replacing a deleted element with the smallest
value of its right subtree.
36
Replacing a deleted node with its subtree. This
happens when the node only has one subtree.
37
  1. /
  2. Internal method to find the smallest
    item in a subtree.
  3. _at_param t the node that roots the tree.
  4. _at_return node containing the smallest
    item.
  5. /
  6. private BinaryNode findMin( BinaryNode t
    )
  7. if( t null )
  8. return null
  9. else if( t.left null )
  10. return t
  11. return findMin( t.left )

38
  • /
  • Internal method to find the largest
    item in a subtree.
  • _at_param t the node that roots the tree.
  • _at_return node containing the largest
    item.
  • /
  • private BinaryNode findMax( BinaryNode t
    )
  • if( t ! null )
  • while( t.right ! null )
  • t t.right
  • return t

39
  1. /
  2. Internal method to find an item in a
    subtree.
  3. _at_param x is item to search for.
  4. _at_param t the node that roots the tree.
  5. _at_return node containing the matched
    item.
  6. /
  7. private BinaryNode find( Comparable x,
    BinaryNode t )
  8. if( t null )
  9. return null
  10. if( x.compareTo( t.element ) lt 0 )
  11. return find( x, t.left )
  12. else if( x.compareTo( t.element ) gt 0
    )
  13. return find( x, t.right )
  14. else
  15. return t // Match

40
  1. /
  2. Internal method to print a subtree in
    sorted order.
  3. _at_param t the node that roots the tree.
  4. /
  5. private void printTree( BinaryNode t )
  6. if( t ! null )
  7. printTree( t.left )
  8. System.out.println( t.element )
  9. printTree( t.right )

41
  • // Test program
  • public static void main( String args
    )
  • BinarySearchTree t new
    BinarySearchTree( )
  • final int NUMS 4000
  • final int GAP 37
  • System.out.println( "Checking... (no
    more output means success)" )
  • for( int i GAP i ! 0 i ( i
    GAP ) NUMS )
  • t.insert( new MyInteger( i ) )
  • for( int i 1 i lt NUMS i 2 )
  • t.remove( new MyInteger( i ) )

42
  • if( NUMS lt 40 )
  • t.printTree( )
  • if( ((MyInteger)(t.findMin(
    ))).intValue( ) ! 2
  • ((MyInteger)(t.findMax(
    ))).intValue( ) ! NUMS - 2 )
  • System.out.println( "FindMin or
    FindMax error!" )
  • for( int i 2 i lt NUMS i2 )
  • if( ((MyInteger)(t.find( new
    MyInteger( i ) ))).intValue( ) ! i )
  • System.out.println( "Find
    error1!" )
  • for( int i 1 i lt NUMS i2 )
  • if( t.find( new MyInteger( i ) )
    ! null )
  • System.out.println( "Find
    error2!" )
Write a Comment
User Comments (0)
About PowerShow.com