BINARY AND OTHER TREES - PowerPoint PPT Presentation

1 / 110
About This Presentation
Title:

BINARY AND OTHER TREES

Description:

BINARY AND OTHER TREES Chapter 8 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees. 8.1 Trees Parts of a Tree Parts of a ... – PowerPoint PPT presentation

Number of Views:180
Avg rating:3.0/5.0
Slides: 111
Provided by: LCh88
Category:
Tags: and | binary | other | trees | binary | search

less

Transcript and Presenter's Notes

Title: BINARY AND OTHER TREES


1
Chapter 8
  • BINARY AND OTHER TREES

2
Overview
  • Trees.
  • Terminology.
  • Traversal of Binary Trees.
  • Expression Trees.
  • Binary Search Trees.

3
8.1 Trees
Definition A trees t is a finite nonempty
set of elements. One of these elements is called
the root, and the remaining elements (if any) are
partitioned into trees which are called the
subtrees of t.
4
(No Transcript)
5
(No Transcript)
6
Parts of a Tree
7
Parts of a Tree
nodes
8
Parts of a Tree
parent node
9
Parts of a Tree
child nodes
parent node
10
Parts of a Tree
child nodes
parent node
11
Parts of a Tree
root node
12
Parts of a Tree
leaf nodes
13
Parts of a Tree
sub-tree
14
Parts of a Tree
sub-tree
15
Parts of a Tree
sub-tree
16
Parts of a Tree
sub-tree
17
height depth number of levels
18
Node Degree Number Of Children
3
2
1
1
0
0
1
0
0
19
Tree Degree Max Node Degree
Degree of tree3.
20
8.2 Binary Trees
  • Finite (possibly empty) collection of elements.
  • A nonempty binary tree has a root element.
  • The remaining elements (if any) are partitioned
    into two binary trees.
  • These are called the left and right subtrees of
    the binary tree.

21
Differences Between A Tree A Binary Tree
  • No node in a binary tree may have a degree more
    than 2, whereas there is no limit on the degree
    of a node in a tree.
  • A binary tree may be empty a tree cannot be
    empty.

22
Differences Between A Tree A Binary Tree
  • The subtrees of a binary tree are ordered those
    of a tree are not ordered.
  • Are different when viewed as binary trees.
  • Are the same when viewed as trees.

23
Examples of Binary Trees
A
nodes 9 height of root 4
B
C
D
E
F
G
H
I
A
A
B
B
empty
empty
24
Binary Expression Trees
  • Use binary trees to represent arithmetic
    expressions
  • e.g.,

/
a

e
-
c
d
25
Arithmetic Expressions
  • (a b) (c d) e f/gh 3.25
  • Expressions comprise three kinds of entities.
  • Operators (, -, /, ).
  • Operands
  • (a, b, c, d, e, f, g, h, 3.25, (a b), (c d),
    etc.).
  • Delimiters ((, )).

26
Operator Degree
  • Number of operands that the operator requires.
  • Binary operator requires two operands.
  • a b
  • c / d
  • e - f
  • Unary operator requires one operand.
  • g
  • - h

27
Infix Form
  • Normal way to write an expression.
  • Binary operators come in between their left and
    right operands.
  • a b
  • a b c
  • a b / c
  • (a b) (c d) e f/gh 3.25

28
Operator Priorities
  • How do you figure out the operands of an
    operator?
  • a b c
  • a b c / d
  • This is done by assigning operator priorities.
  • priority() priority(/) gt priority()
    priority(-)
  • When an operand lies between two operators, the
    operand associates with the operator that has
    higher priority.

29
Tie Breaker
  • When an operand lies between two operators that
    have the same priority, the operand associates
    with the operator on the left.
  • a b - c
  • a b / c / d

30
Delimiters
  • Subexpression within delimiters is treated as a
    single operand, independent from the remainder of
    the expression.
  • (a b) (c d) / (e f)

31
Infix Expression Is Hard To Parse
  • Need operator priorities, tie breaker, and
    delimiters.
  • This makes computer evaluation more difficult
    than is necessary.
  • Postfix and prefix expression forms do not rely
    on operator priorities, a tie breaker, or
    delimiters.
  • So it is easier for a computer to evaluate
    expressions that are in these forms.

32
Postfix Form
  • The postfix form of a variable or constant is the
    same as its infix form.
  • a, b, 3.25
  • The relative order of operands is the same in
    infix and postfix forms.
  • Operators come immediately after the postfix form
    of their operands.
  • Infix a b
  • Postfix ab

33
Postfix Examples
  • Infix a b c
  • Postfix

a
b
c

  • Infix a b c
  • Postfix

a
b

c
  • Infix (a b) (c d) / (e f)
  • Postfix

a
b

c
d
-

e
f

/
34
Unary Operators
  • Replace with new symbols.
  • a gt a _at_
  • a b gt a _at_ b
  • - a gt a ?
  • - a-b gt a ? b -

35
Postfix Evaluation
  • Scan postfix expression from left to right
    pushing operands on to a stack.
  • When an operator is encountered, pop as many
    operands as this operator needs evaluate the
    operator push the result on to the stack.
  • This works because, in postfix, operators come
    immediately after their operands.

36
Postfix Evaluation
  • (a b) (c d) / (e f)
  • a b c d - e f /
  • a b c d - e f /
  • a b c d - e f /
  • a b c d - e f /

b
a
37
Postfix Evaluation
  • (a b) (c d) / (e f)
  • a b c d - e f /
  • a b c d - e f /
  • a b c d - e f /

d
  • a b c d - e f /

c
  • a b c d - e f /

(a b)
  • a b c d - e f /
  • a b c d - e f /

38
Postfix Evaluation
  • (a b) (c d) / (e f)
  • a b c d - e f /
  • a b c d - e f /

(c d)
(a b)
39
Postfix Evaluation
  • (a b) (c d) / (e f)
  • a b c d - e f /
  • a b c d - e f /
  • a b c d - e f /
  • a b c d - e f /

f
e
  • a b c d - e f /

(a b)(c d)
stack
40
Postfix Evaluation
  • (a b) (c d) / (e f)
  • a b c d - e f /
  • a b c d - e f /
  • a b c d - e f /
  • a b c d - e f /

(e f)
  • a b c d - e f /

(a b)(c d)
  • a b c d - e f /

stack
41
Prefix Form
  • The prefix form of a variable or constant is the
    same as its infix form.
  • a, b, 3.25
  • The relative order of operands is the same in
    infix and prefix forms.
  • Operators come immediately before the prefix form
    of their operands.
  • Infix a b
  • Postfix ab
  • Prefix ab

42
Binary Tree Form
  • a b
  • - a

43
Binary Tree Form
  • (a b) (c d) / (e f)

44
Merits Of Binary Tree Form
  • Left and right operands are easy to visualize.
  • Code optimization algorithms work with the binary
    tree form of an expression.
  • Simple recursive evaluation of expression.

45
8.3 Properties of Binary Trees
  • 1. A binary tree with n nodes has n-1 edges
  • 2. A binary tree of height h, h?0, has at least h
    and at most 2h-1 elements in it
  • 3. The height of a binary tree that contains n
    elements is at most n and at least ?log2(n1)?

46
(No Transcript)
47
(No Transcript)
48
Minimum Number Of Nodes
  • Minimum number of nodes in a binary tree whose
    height is h.
  • At least one node at each of first h levels.

minimum number of nodes is h
49
Maximum Number Of Nodes
  • All possible nodes at first h levels are present.

Maximum number of nodes 1 2 4 8
2h-1 2h - 1
50
Number Of Nodes Height
  • Let n be the number of nodes in a binary tree
    whose height is h.
  • h lt n lt 2h 1
  • log2(n1) lt h lt n

51
Full Binary Tree
  • A full binary tree of a given height h has 2h1
    nodes.

52
Numbering Nodes In A Full Binary Tree
  • Number the nodes 1 through 2h 1.
  • Number by levels from top to bottom.
  • Within a level number from left to right.

1
2
3
4
6
5
7
8
9
10
11
12
13
14
15
53
Node Number Properties
  • Parent of node i is node i / 2, unless i 1.
  • Node 1 is the root and has no parent.

54
Node Number Properties
  • Left child of node i is node 2i, unless
  • 2i gt n, where n is the number of nodes.
  • If 2i gt n, node i has no left child.

55
Node Number Properties
  • Right child of node i is node 2i1, unless 2i1 gt
    n, where n is the number of nodes.
  • If 2i1 gt n, node i has no right child.

56
Full Binary Tree
1
2
3
4
5
6
7
12
14
15
13
8
10
11
9
  • Definition
  • Full binary tree of height h has exactly 2h-1
    elements
  • Example
  • height h 4
  • 24-1 15 elements

57
Complete Binary Tree
1
2
3
4
5
6
  • Definition
  • binary tree of height h
  • all levels except possibly level h-1 are full
  • full binary tree is a special case of a complete
    binary tree

58
Complete Binary Tree With n Nodes
  • Start with a full binary tree that has at least n
    nodes.
  • Number the nodes as described earlier.
  • The binary tree defined by the nodes numbered 1
    through n is the unique n node complete binary
    tree.

59
Example
  • Complete binary tree with 10 nodes.

60
More Properties
  • Let i, 1 ? i ?n, be the number assigned to an
    element of a complete binary tree of height h as
    follows
  • Begin at level 1 and go down to level h,
    numbering left to right (see next slide).
  • If i1, then element is root of binary tree. If
    igt1, then its parent has number ?i/2?
  • If 2igtn, then element has no left child. O.w.,
    its left child has been assigned the number 2i
  • If 2i1gtn, then this element has no right child.
    O.w., its right child has been assigned the
    number 2i1

61
Intuition
1
2
3
4
5
6
7
12
14
15
13
8
10
11
9
Rigorous proof by induction on i
62
Review
A
B
C
D
E
F
G
H
I
J
K
L
  • What is the depth of node C?
  • What is the height of node C/the tree?
  • How many different paths of length 3 are there?
  • Which nodes are leaves?
  • Which node is the root?
  • What is the parent of C?
  • Which nodes are ancestors of E?

63
8.4 Representation of Binary Trees
  • Formula-Based Representation
  • Linked Representation

64
(No Transcript)
65
(No Transcript)
66
Array Representation
  • Number the nodes using the numbering scheme for a
    full binary tree. The node that is numbered i is
    stored in treei.

a
b
c
d
e
f
g
h
i
j
67
Right-Skewed Binary Tree
  • An n node binary tree needs an array whose length
    is between n1 and 2n.

68
Linked Representation
  • Each binary tree node is represented as an object
    whose data type is BinaryTreeNode.
  • The space required by an n node binary tree is n
    (space required by one node).

69
Linked Representation Example
70
Formula-Based Representation
  • Use array to store elements
  • Element numbers are used as array indices
  • Assume complete binary tree (with missing
    elements)
  • Leave empty cells in the array
  • Use properties of complete b. t. to calculate
    array indices (see previous slide)

1
A
?
A
B
C
2
3
B
0
1
2
3
4
5
6
7
4
5
7
6
C
71
Contd
  • Let r be index of element, then
  • Parent(r) ?(r-1)/2?, 0ltrltn
  • Left child(r) 2r1, if 2r1 lt n
  • Right child(r) 2r2 if 2r2ltn
  • Left sibling(r) r-1 if even and 0ltrltn
  • Right sibling(r) r1 if r odd and r1ltn
  • Compact representation
  • However, only useful when number of missing
    elements is small

72
Worst Case Scenario
  • Right-skewed binary tree
  • With n elements, requires array of size up to
    2n-1

A
3
B
7
C
7
D
A
B
C
D
73
Linked Representation
  • Each element is represented by by a node (chain
    node) that has two link fields (leftChild and
    rightChild)
  • each node has element field to hold data
  • edge in tree is represented by a pointer from
    parent node to child node

t
A binary tree with n nodes has 2n - (n-1) n1
NULL pointers
A
B
C
D
E
74
Contd
  • Popular way to represent trees
  • large fraction of space is devoted to tree
    structure overhead (not to storing data)
  • Tree traversal through pointer chasing
  • need methods such as getLeftChild(),
    getRightChild(), etc.

75
template ltclass Tgtclass BinaryTreeNode public
BinaryTreeNode() LeftChild RightChild 0
BinaryTreeNode(const T e) data
e LeftChild RightChild 0
BinaryTreeNode(const T e,
BinaryTreeNode l,BinaryTreeNode r)
data e LeftChild l RightChild r
private T data BinaryTreeNodeltTgt
LeftChild, // left subtree
RightChild // right subtree Program 8.1
Node class for linked binary trees
76
(No Transcript)
77
8.5 Common Binary Tree Operations
  • The Operations
  • Determine its height.
  • Determine the number of elements in it.
  • Make a Copy.
  • Display the binary tree on a screen or on paper.
  • Determine whether two binary trees are identical.
  • Delete the tree.
  • If it is an expression tree, evaluate the
    expression.
  • If it is an expression tree, obtain the
    parenthesized form of the expression.

78
8.6 Traversal
  • Systematic way of visiting all the nodes.
  • Methods Preorder, Inorder, and Postorder
  • They all traverse the left subtree before the
    right subtree.
  • The name of the traversal method depends on when
    the node is visited.

79
Binary Tree Traversal
  • Many binary tree operations are done by
    performing a traversal of the binary tree.
  • In a traversal, each element of the binary tree
    is visited exactly once.
  • During the visit of an element, all action (make
    a clone, display, evaluate the operator, etc.)
    with respect to this element is taken.

80
Binary Tree Traversal Methods
  • Preorder
  • Inorder
  • Postorder
  • Level order

81
Preorder Traversal
  • Visit the node.
  • Traverse the left subtree.
  • Traverse the right subtree.

82
Example Preorder
43
31
64
20
40
56
89
28
33
47
59
83
Inorder Traversal
  • Traverse the left subtree.
  • Visit the node.
  • Traverse the right subtree.

84
Example Inorder
43
31
64
20
40
56
89
28
33
47
59
85
Postorder Traversal
  • Traverse the left subtree.
  • Traverse the right subtree.
  • Visit the node.

86
Example Postorder
43
31
64
20
40
56
89
28
33
47
59
87
Expression Tree
  • A Binary Tree built with operands and operators.
  • Also known as a parse tree.
  • Used in compilers.

88
Example Expression Tree

/
/
1
3

4
6
7
1/3 67 / 4
89
Notation
  • Preorder
  • Prefix Notation
  • Inorder
  • Infix Notation
  • Postorder
  • Postfix Notation

90
Example Infix

/
/
1
3

4
6
7
91
Example Postfix

/
/
1
3

4
7
6
Recall Reverse Polish Notation
92
Example Prefix

/
/
1
3

4
6
7

/
1
3
/

6
7
4
93
Program 8.2 Preorder traversal
template ltclass Tgt void PreOrder(BinaryTreeNodeltTgt
t) if (t) Visit(t) // visit tree root
PreOrder(t-gtLeftChild) // do left subtree
PreOrder(t-gtRightChild) // do right subtree
94
Program 8.3 Inorder traversal
template ltclass Tgt void InOrder(BinaryTreeNodeltTgt
t) if (t) InOrder(t-gtLeftChild) // do left
subtree Visit(t) // visit tree root
InOrder(t-gtRightChild) // do right
subtree
95
Program 8.4 Postorder traversal
template ltclass Tgt void PostOrder(BinaryTreeNodeltT
gt t) if (t) PostOrder(t-gtLeftChild) //
do left subtree PostOrder(t-gtRightChild) //
do right subtree Visit(t) // visit tree
root
96
The Class BinaryTree
97
AbstractDataType BinaryTree instances
collection elements if not empty, the collection
is partitioned into a root, left subtree,
and right subtree each subtree is also a binary
tree operations Create() Create an empty
binary tree IsEmpty Return true if empty,
return false otherwise Root(x) x is set to root
element return false if the operation
fails, return true otherwise
98
MakeTree(root,left,right) create a binary
tree element, left (right) as the left (right)
subtree. BreakTree(root,left,right) inverse of
create PreOrder preorder traversal of binary
tree InOrder inorder traversal of binary
tree PostOrder postorder traversal of binary
tree LevelOrder level-order traversal of binary
tree ADT 5.1 The abstract data type binary tree
99
8.9 ADT and Class Extensions
100
Program 8.7 Binary tree class template ltclass Tgt
class BinaryTree public BinaryTree() root
0 BinaryTree() bool IsEmpty()
constreturn ((root) ? falsetrue) bool
Root(T x) const void MakeTree(const T
element, BinaryTreeltTgt
left, BinaryTreeltTgt right) void BreakTree(T
element, BinaryTreeltTgt left,
BinaryTreeltTgt right)
101
void PreOrder(void (Visit) (BinaryTreeNodeltTgt
u)) PreOrder(Visit, root) void
InOrder(void (Visit) (BinaryTreeNodeltTgt u))
InOrder(Visit, root) void PostOrder (void
(Visit) (BinaryTreeNodeltTgt u))
PostOrder(Visit, root) void LevelOrder (void
(Visit) (BinaryTreeNodeltTgt u))
102
private BinaryTreeNodeltTgt root // pointer to
root void PreOrder(void (Visit)
(BinaryTreeNodeltTgtu),BinaryTreeNodeltTgtt)
void InOrder(void (Visit)
(BinaryTreeNodeltTgtu),BinaryTreeNodeltTgtt)
void PostOrder(void (Visit)
(BinaryTreeNodeltTgtu),BinaryTreeNodeltTgtt)
Program 8.7 Binary tree class
103
Program 8.8 Implementation of public
members template ltclass Tgt bool
BinaryTreeltTgtRoot(T x) const // Set x to root
data. // Return false if no root. if (root) x
root-gtdata return true else return false
// no root
104
template ltclass Tgt void BinaryTreeltTgtMakeTree(co
nst T element, BinaryTreeltTgt left,
BinaryTreeltTgt right) root new
BinaryTreeNodeltTgt (element,
left.root, right.root) left.root right.root
0
105
template ltclass Tgt void BinaryTreeltTgtBreakTree
( T element, BinaryTreeltTgt left,
BinaryTreeltTgt right) if (!root) throw
BadInput() // tree empty element
root-gtdata left.root root-gtLeftChild
right.root root-gtRightChild delete root
root 0 Program 8.8 Implementation of public
members
106
Program 8.9 Pre-, in-, and postorder template
ltclass Tgt void BinaryTreeltTgtPreOrder(void
(Visit) (BinaryTreeNodeltTgt u),BinaryTreeNodeltT
gt t) // Preorder traversal. if (t)
Visit(t) PreOrder(Visit,
t-gtLeftChild) PreOrder(Visit,
t-gtRightChild)
107
template ltclass Tgt void BinaryTreeltTgtInOrder(voi
d (Visit) (BinaryTreeNodeltTgt u),BinaryTreeNodeltT
gt t) // Inorder traversal. if (t)
InOrder(Visit, t-gtLeftChild)
Visit(t) InOrder(Visit,
t-gtRightChild)
108
template ltclass Tgt void BinaryTreeltTgtPostOrder(v
oid (Visit) (BinaryTreeNodeltTgt
u),BinaryTreeNodeltTgt t) // Postorder
traversal. if (t) PostOrder(Visit,
t-gtLeftChild) PostOrder(Visit,
t-gtRightChild) Visit(t)
Program 8.9 Pre-, in-, and postorder
109
8.10 Applications
110
Exercises
Chapter Ex8 Ex16 Ex22
Write a Comment
User Comments (0)
About PowerShow.com