Trees - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Trees

Description:

else height(n) = 1 max(height of children) Trees: ancestry. Green nodes are descendants of red node; red node is ancestor of green nodes. ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 17
Provided by: CSBR
Category:
Tags: ancestry | trees

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
root
internal node
leaf
2
Trees depth
depth 0
depth 1
depth 3
depth 4
depth(root) 0 else depth(n) 1
depth(n.parent())
3
Trees height
height 4
height 0
height 1
height 1
height 0
height 0
height(leaf) 0. else height(n) 1 max(height
of children)
4
Trees ancestry
Green nodes are descendants of red node red node
is ancestor of green nodes. Sometimes we say a
node is an ancestor of itself (e.g. LCA problem
on HW2), but generally not.
5
Trees full
Every node has 0 or two children
6
Trees complete
All levels except possibly the last, are full
last level is left-full (i.e., all nodes as far
left as possible.
7
Animal
  • C Is your animal smaller than a breadbox?
  • U n
  • C Is your animal a cat?
  • U n
  • C What is your animal?
  • U albatross
  • C Please enter a question that would be true for
    an albatross, but false for a cat
  • U Does your animal have wings?

8
Animal (round 2)
  • C Is your animal smaller than a breadbox?
  • U n
  • C Does your animal have wings?
  • U y
  • C Is your animal an albatross?
  • U n
  • C What is your animal?
  • U penguin
  • C Please enter a question that would be true for
    a penguin, but false for a albatross
  • U is your animal earthbound?

9
Initial state
smaller than breadbox?
cat?
10
After round 1
smaller than breadbox?
wings?
cat?
albatross?
11
Another use representing expressions
((1 4) (2 - 5)) 3

3


-
1
4
2
5
12
  • class EmptyBinTreeException(Exception)
  • class MissingChildException(Exception)
  • class Node
  • parent(self) // Return parent Node (not
    position)
  • left(self) // Return left child Node (not
    position)
  • right(self)
  • addRight(self, value) //Add right child to this
    node
  • // Error if theres one
    already
  • addLeft(self, value)
  • hasLeft(self) // Return whether the node has a
    left child.
  • hasRight(self)
  • element(self) // Return the element stored at
    this node
  • depth(self) // Return the depth of this node
    in the tree

13
  • class Position
  • def __init__(self, node)
  • self._node node
  • def __str__(self)
  • return repr(self._node.element())
  • def element(self) // PUBLIC part of Position
  • return self._node.element()

14
  • class BinTree
  • root(self)
  • parent(self, p) // Return position of parent of
    this position
  • children(self, p)// Return posns of children of
    this posn
  • isEmpty(self) // Is the tree empty?
  • size(self) // Return the size of the tree
  • height(self) // Return the height of the tree
  • ...

15
  • isInternal(self, p) // Return whether posn p
    is internal
  • isExternal(self, p)
  • isRoot(self, p)
  • replace(self, p, e) // replace element at
    position p with e
  • left(self, p) // Get posn of left child of
    node at posn p
  • // error if no left child
    exists
  • right(self, p)
  • hasLeft(self, p) // Return whether node at
    posn p has left child.
  • hasRight(self, p)
  • addRoot(self, e) // Add root to tree Error
    if theres one already
  • addLeft(self, p, e) // Add a left child to the
    node at position p // error if theres
    one already
  • addRight(self, p, e)

16
Other methods not implemented in HW
iterator(self) return iterator over all
elements stored in tree positions(self)
return iterator over all positions in
tree remove(p) remove position p and replace
it with child, if any. If p has two kids, its
an error attach(p, T1, T2) Attach T1 and T2
as left and right subtrees of leaf at position p.
Error if p is not external
Write a Comment
User Comments (0)
About PowerShow.com