Binary%20Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Binary%20Trees

Description:

... Inorder Traversal In an inorder traversal a node is visited after its left subtree and before its right subtree Application: draw a binary tree x(v) = inorder ... – PowerPoint PPT presentation

Number of Views:215
Avg rating:3.0/5.0
Slides: 17
Provided by: YUE84
Learn more at: http://dcm.uhcl.edu
Category:

less

Transcript and Presenter's Notes

Title: Binary%20Trees


1
Binary Trees
CSCI 3333 Data Structures
byDr. Bun YueProfessor of Computer
Scienceyue_at_uhcl.eduhttp//sce.uhcl.edu/yue/201
3
2
Acknowledgement
  • Mr. Charles Moen
  • Dr. Wei Ding
  • Ms. Krishani Abeysekera
  • Dr. Michael Goodrich

3
Binary Trees
  • A binary tree is a tree with the following
    properties
  • Each internal node has at most two children
    (exactly two for proper binary trees)
  • The children of a node are an ordered pair
  • We call the children of an internal (interior)
    node left child and right child

A
B
C
G
D
F
E
H
I
4
Binary Tree
  • Alternative recursive definition a binary tree
    is either
  • a tree consisting of a single node, or
  • a tree whose root has an ordered pair of
    children, each of which is a binary tree.
  • Some Applications
  • arithmetic expressions
  • decision processes
  • searching

5
Arithmetic Expression Tree
  • Binary tree associated with an arithmetic
    expression
  • interior nodes operators
  • leaf nodes operands
  • Example arithmetic expression tree for the
    expression (2 ? (a - 1) (3 ? b))

6
Decision Tree
  • Binary tree associated with a decision process
  • interior nodes questions with yes/no answer
  • leaf nodes decisions
  • Example dining decision

Want a fast meal?
Yes
No
How about coffee?
On expense account?
Yes
No
Yes
No
Starbucks
Spikes
Al Forno
Café Paragon
7
Properties of Proper Binary Trees
  • Notation
  • n number of nodes
  • e number of external (leaf) nodes
  • i number of internal nodes
  • h height
  • Properties
  • e i 1
  • n 2e - 1
  • h ? i
  • h ? (n - 1)/2
  • e ? 2h
  • h ? log2 e
  • h ? log2 (n 1) - 1

8
BinaryTree ADT
  • The BinaryTree ADT extends the Tree ADT, i.e., it
    inherits all the methods of the Tree ADT
  • Additional methods
  • position left(p)
  • position right(p)
  • boolean hasLeft(p)
  • boolean hasRight(p)
  • Update methods may be defined by data structures
    implementing the BinaryTree ADT

9
Inorder Traversal
  • In an inorder traversal a node is visited after
    its left subtree and before its right subtree
  • Application draw a binary tree
  • x(v) inorder rank of v
  • y(v) depth of v

Algorithm inOrder(v) if hasLeft (v) inOrder (left
(v)) visit(v) if hasRight (v) inOrder (right (v))
6
2
8
1
7
9
4
3
5
10
Print Arithmetic Expressions
Algorithm printExpression(v) if hasLeft
(v) print(() inOrder (left(v)) print(v.element
()) if hasRight (v) inOrder (right(v)) print
())
  • Specialization of an inorder traversal
  • print operand or operator when visiting node
  • print ( before traversing left subtree
  • print ) after traversing right subtree

((2 ? (a - 1)) (3 ? b))
11
Evaluate Arithmetic Expressions
  • Specialization of a postorder traversal
  • recursive method returning the value of a subtree
  • when visiting an internal node, combine the
    values of the subtrees

12
Linked Structure for Trees
  • A node is represented by an object storing
  • Element
  • Parent node
  • Sequence of children nodes
  • Node objects implement the Position ADT

B
?
?
A
D
F
B
D
A
F
?
?
C
E
C
E
13
Linked Structure for Binary Trees
  • A node is represented by an object storing
  • Element
  • Parent node
  • Left child node
  • Right child node
  • Node objects implement the Position ADT

?
?
?
B
A
D
?
?
?
?
C
E
14
Array-Based Representation of Binary Trees
  • nodes are stored in an array

1
2
3
  • let rank(node) be defined as follows
  • rank(root) 1
  • if node is the left child of parent(node),
    rank(node) 2rank(parent(node))
  • if node is the right child of parent(node),
    rank(node) 2rank(parent(node))1

6
4
5
7
10
11
15
Space Complexity
  • Big-Oh for Memory Requirement.
  • Linked Structures worst case O(n), where n is
    the number of nodes.
  • Array Representation Worst case O(2n) gt not
    acceptable for many general binary tree
    applications.

16
Questions and Comments?
Write a Comment
User Comments (0)
About PowerShow.com