Binary Trees - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Binary Trees

Description:

BST Property: All elements stored in the left subtree of a node with value K ... BST Search. Algorithm. search(K,T) for key K and tree T. if currnode is NULL, ... – PowerPoint PPT presentation

Number of Views:133
Avg rating:3.0/5.0
Slides: 37
Provided by: igr9
Category:
Tags: binary | bst | trees

less

Transcript and Presenter's Notes

Title: Binary Trees


1
Binary Trees
  • A binary tree is made up of a finite set of nodes
    that is either empty or consists of a node called
    the root together with two binary trees, called
    the left and right subtrees, which are disjoint
    from each other and from the root.

2
Binary Tree Example
  • Definitions
  • Node element of the tree
  • Root top node
  • Subtree binary tree associated with the root
    node (left and right)
  • Children the root node of a nodes subtrees
  • Edge a link from a node to its children
  • Parent the node to which the current node is a
    child
  • Ancestor, descendant

3
Binary Tree Example
  • Path a sequence of nodes n1,n2,,nk s.t. ni is
    a parent of ni1 for 1 lt i lt k
  • Length of a path k -1
  • Depth length of path from root to a node
  • Height depth of the deepest node 1
  • Level all nodes of depth d
  • Leaf node with two empty children
  • Internal node node with at least one non-empty
    child

No standard definitions
4
Full and Complete Binary Trees
  • Full binary tree Each node is either a leaf or
    internal node with exactly two non-empty
    children.
  • Complete binary tree If the height of the tree
    is d, then all leaves except possibly level d are
    completely full. The bottom level has all nodes
    to the left side.

5
Full Binary Tree Theorem (1)
  • Theorem The number of leaves in a non-empty full
    binary tree is one more than the number of
    internal nodes.
  • Proof (by Mathematical Induction)
  • Base cases A full, non-empty binary tree with 0
    internal nodes must have 1 leaf node. A full
    binary tree with 1 internal node must have two
    leaf nodes.
  • Induction Hypothesis Assume any full binary tree
    T containing n internal nodes has n1 leaves.

6
Full Binary Tree Theorem (2)
  • Induction Step Given tree T with n internal
    nodes, pick leaf node L and make it internal (by
    adding two children). T is still full. How many
    internal leaf nodes does it contain?
  • We added two new leaf nodes, but lost one
  • n 1 2 1 n 2
  • We created one new internal node
  • n 1
  • T has n1 internal nodes, and n2 leaf nodes, or
    n internal nodes and n1 leaf nodes
  • By induction, the theorem holds for n gt 0

7
Traversals (1)
  • Any process for visiting the nodes in some order
    is called a traversal.
  • Any traversal that lists every node in the tree
    exactly once is called an enumeration of the
    trees nodes.

8
Traversals (2)
  • Preorder traversal Visit each node before
    visiting its children.
  • Postorder traversal Visit each node after
    visiting its children.
  • Inorder traversal Visit the left subtree, then
    the node, then the right subtree.
  • Level-order traversal Visit all nodes in level
    0, then level 1, then level 2, etc.
  • Always visiting the left child first

9
Recursive Implementation
  • Pre-, post-, and in-order traversals are all
    easily written as recursive functions (or with a
    stack)
  • Level-order requires an additional data structure
    (a queue)

10
What are the traversals?
Give the pre-, post-, in-, and level order
traversal of the tree.
11
Counting Nodes
  • We can count the number of nodes in a tree using
    a traversal.
  • Key point the number of nodes in a tree with
    root node r is the number of nodes in rs left
    subtree plus the number of nodes in rs right
    subtree plus 1 (for r)

12
Array Implementation
13
Array Implementation
  • Parent(r) (r-1)/2 if r ltgt 0 and r lt n.
  • Leftchild(r) 2r 1 if 2r 1 lt n.
  • Rightchild(r) 2r 2 if 2r 2 lt n.
  • Leftsibling(r) r - 1 if r is even, r gt 0, and r
    lt n.
  • Rightsibling(r) r 1 if r is odd and r 1 lt
    n.

14
Binary Tree Implementation
15
Array Implementation
16
Array Implementation
  • Parent(r) (r-1)/2 if r ltgt 0 and r lt n.
  • Leftchild(r) 2r 1 if 2r 1 lt n.
  • Rightchild(r) 2r 2 if 2r 2 lt n.
  • Leftsibling(r) r - 1 if r is even, r gt 0, and r
    lt n.
  • Rightsibling(r) r 1 if r is odd and r 1 lt
    n.

17
Binary Search Trees
  • BST Property All elements stored in the left
    subtree of a node with value K have values lt K.
    All elements stored in the right subtree of a
    node with value K have values gt K.

18
BST Search
  • Algorithm
  • search(K,T) for key K and tree T
  • if currnode is NULL, return false
  • if K lt currnode-gtvalue
  • return search(K,currnode-gtleft)
  • else if K gt currnode-gtvalue
  • return search(K,currnode-gtright)
  • else // must equal currnode
  • return true

19
BST Insert (1)
20
BST Insert (2)
  • Insert algorithm (no duplicates)
  • insert(K,T) insert key K into Tree T
  • Find leaf node L where search(K,T) fails
  • If K lt L-gtvalue
  • add K as Ls left child
  • else
  • add K as Ls right child

21
BST Remove (1)
22
BST Remove
  • Remove algorithm
  • Remove(K,T) remove key K from tree T
  • find node N with K using search(K,T)
  • if N is a leaf, remove and exit
  • else
  • find the smallest node in the right subtree
    of N, called n N
  • exchange values for N and N
  • remove N

23
Cost of BST Operations
  • Find
  • Insert
  • Delete
  • All cost depth of the node in question.
  • Worst case ?(n).
  • Average case ?(log n).

24
Heaps
  • Heap Complete binary tree with the heap
    property
  • Min-heap All values less than child values.
  • Max-heap All values greater than child values.
  • The values are partially ordered.
  • Heap representation Normally the array-based
    complete binary tree representation.

25
Building the Heap
  • (a) (4-2) (4-1) (2-1) (5-2) (5-4) (6-3) (6-5)
    (7-5) (7-6)
  • (b) (5-2), (7-3), (7-1), (6-1)

26
Siftdown (1)
  • For fast heap construction
  • Work from high end of array to low end.
  • Call siftdown for each item.
  • Dont need to call siftdown on leaf nodes.
  • Siftdown(H)
  • if(H-gtvalue gt H-gtleft-gtvalue and
  • H gt H-gtright-gtvalue) then return
  • else
  • H max(H-gtleft, H-gtright)
  • swap values(H,H)
  • siftdown(H)

27
Siftdown (2)
28
Buildheap Cost
  • Cost for heap construction
  • log n
  • ? (i - 1) n/2i ? n.
  • i1

29
Remove Max Value
  • RemoveMax(H) -- get the max element off heap H
  • maxelem H-gtvalue // Max is at the top
  • H-gtvalue last elem-gtvalue
  • shiftdown(H)
  • return maxelem

30
Priority Queues (1)
  • A priority queue stores objects, and on request
    releases the object with greatest value.
  • Example Scheduling jobs in a multi-tasking
    operating system.
  • The priority of a job may change, requiring some
    reordering of the jobs.
  • Implementation Use a heap to store the priority
    queue.

31
Priority Queues (2)
  • To support priority reordering, delete and
    re-insert. Need to know index for the object in
    question.

32
Huffman Coding Trees
  • ASCII codes 8 bits per character.
  • Fixed-length coding.
  • Can take advantage of relative frequency of
    letters to save space.
  • Variable-length coding
  • Build the tree with minimum external path weight.

33
Huffman Tree Construction (1)
34
Huffman Tree Construction (2)
35
Assigning Codes
36
Coding and Decoding
  • A set of codes is said to meet the prefix
    property if no code in the set is the prefix of
    another.
  • Code for DEED
  • Decode 1011001110111101
  • Expected cost per letter
Write a Comment
User Comments (0)
About PowerShow.com