Binary%20Trees%20Terminology - PowerPoint PPT Presentation

About This Presentation
Title:

Binary%20Trees%20Terminology

Description:

Binary Trees. Terminology. A graph G = V,E is a collection of nodes and edges. ... v1,v2,...,vn where and (vi,vi 1) is an edge of G. A path v1,v2,...,vn,v1 is a ... – PowerPoint PPT presentation

Number of Views:104
Avg rating:3.0/5.0
Slides: 8
Provided by: Harv55
Learn more at: http://cs.sou.edu
Category:

less

Transcript and Presenter's Notes

Title: Binary%20Trees%20Terminology


1
Binary TreesTerminology
  • A graph G ltV,Egt is a collection of nodes and
    edges. An edge (v1,v2) is a pair of vertices that
    are directly connected.
  • A path, P, is a sequence of vertices ltv1,v2,,vngt
    where and (vi,vi1) is an edge of G. A path
    ltv1,v2,,vn,v1gt is a cycle.
  • A tree is a graph without cycles. The Root is a
    distinguished node of the tree. The Parent is the
    adjacent node that is closer to the root. A Leaf
    is a node without children. A child is an
    adjacent node that is further from the root. The
    level of a vertex is the length of the path from
    the root.
  • A binary tree is a tree with at most two children

2
Advantage of TreesAssuming Deletions exclude
time for Find
  • Ordered Array
  • Insert O(N), Delete O(N), Find O(Lg N).
  • Unordered Array
  • Insert O(1), Delete O(1), Find O(N).
  • Linked List
  • Insert O(1), Delete O(1), Find O(N).
  • Binary Trees
  • Insert O(1), Delete O(1), Find O(lg N) in most
    cases.
  • Question When would Find not take place in O(Lg
    N)?

Note the above insert and removed complexities
dont include the find
3
Tree StructureQuestion Can a Tree be
implemented using an array?
  • typedef struct TreeNode
  • Key key
  • Data data
  • struct TreeNode left
  • struct TreeNode right
  • Tree
  • Top of the Tree Tree root
  • Basic Tree Abstract Data Type Methods
  • Node find(Key key)
  • Item insert(Key key, Data data )
  • Item delete(Key key)

4
Binary Tree Find
  • Node find( Key key)
  • Node current root, previous
  • previous null
  • if (current null) return null
  • while(!equal(current-gtkey, key))
  • previous current if
    (greater(current-gtkey, key)) current
    current-gtleft
  • else current current-gtright
  • if (current null) return previous
  • return previous

Notes -gt in C is equivalent to . in Java Java
has no equivalent to . in C
5
Binary Tree Insertion/Deletionpseudo code
  • Insert node
  • Perform Find
  • If node was found, Return false
  • If tree was empty, Set root node
  • Else If node lt leaf node returned by Find
  • Link node to left child of
    leaf, Return true
  • Else Link node to right child of leaf,
    Return true
  • Delete node
  • Perform Find
  • If node was not found, Return false
  • If node was a leaf, Unlink parent left or right
    pointer, Return true
  • If node had one child, Link child to appropriate
    parent pointer, Return true
  • Find and Remove successor
  • Replace node with successor in the tree, Return
    true

6
Other Search Tree Operations
  • Find Minimum
  • Go left until the leaf is found
  • Find Maximum
  • Go right until the leaf is found
  • Predecessor
  • Go left, and the proceed right until the leaf is
    found
  • Successor
  • Go right, and then proceed left until the leaf
    is found

7
Binary Tree TraversalSee Tree Example in Text
  • Preorder Traversal
  • Recursively Call until leaf is found
  • Recursive step
  • Visit, Traversal(Left), Traversal(Right)
  • Inorder (Traversal(left), Visit,
    Traversal(right))
  • Postorder (Traversal(left), Traversal(right),
    Visit)
Write a Comment
User Comments (0)
About PowerShow.com