Binary Trees 2 - PowerPoint PPT Presentation

Loading...

PPT – Binary Trees 2 PowerPoint presentation | free to download - id: 4cdd24-OTgyN



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Binary Trees 2

Description:

Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science Postorder Template void Postorder ( BinaryTreeNode *t) { // Postorder traversal of *t. – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 46
Provided by: Lee144
Learn more at: http://www.cs.sjsu.edu
Category:
Tags: binary | trees

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Binary Trees 2


1
Binary Trees 2
CS146 Lecture 8
  • Prof. Sin-Min Lee
  • Department of Computer Science

2
(No Transcript)
3
(No Transcript)
4
(No Transcript)
5
(No Transcript)
6
For each number of nodes, n, there is a certain
number of possible binary tree configurations.
These numbers form a sequence of integers with
respect to n. A useful way to describe an integer
sequence is to construct a generating function
whose coefficients bi are the sequence. B(x) is
power series over a purely formal variable x.
7
Apparently, b1 is 1, and b2 is 2. The b0
coefficient is somewhat artificial, its the
no-nodes tree which is, I guess, the only one.
Further analysis gives the following idea if a
binary tree has n nodes, then there must be one
node as the root and two subtrees. The total
number of nodes in the subtrees must be n-1, and
either of them may be empty. Assuming k nodes in
the left subtree, the right subtree then has
n-k-1 nodes, k going from 0 to n-1. The root node
is always there, and therefore the tree
configurations differ only by the configuration
of subtrees.
8
The total number of possible trees on n nodes can
then be expressed as  
9
(No Transcript)
10
(No Transcript)
11
(No Transcript)
12
Postorder
  • Template ltclass Tgt
  • void Postorder ( BinaryTreeNodeltTgt t)
  • // Postorder traversal of t.
  • If (t)
  • Postorder ( t-gtLeftChild)//do left
    subtree
  • Postorder(t-gtRightChild)//do right
    subtree
  • visit(t)
    // visit tree root
  • Each root is visited after its left and right
    subtrees have been traversed.

13
(No Transcript)
14
(No Transcript)
15
Example
  • Preorder ab/cd
  • Inorder abc/d
  • Postorder abcd/
  • Elements of a binary tree listed in pre-,in-,and
    postorder.


/
a
b
c
d
16
About level order
  • While loop only if the tree is not empty
  • Following the addition of the children of t to
    the queue,we attempt to delete an element from
    the queue.
  • If the queue is empty,Delete throws an
    OutOfBounds exception.
  • If the queue is not empty,then Delete returns
    the deleted element in t.

17
(No Transcript)
18
(No Transcript)
19
(No Transcript)
20
(No Transcript)
21
Sample tree to illustrate tree traversal
1
2
3
5
4
6
7
10
8
9
11
12
22
Tree after four nodes visited in preorder
traversal
23
Tree after left subtree of root visited using
preorder traversal
24
Tree after completed preorder traversal
1
1
8
2
2
3
9
12
6
3
5
4
6
7
11
7
4
5
10
8
9
11
12
25
Tree visited using inorder traversal
26
Tree visited using postorder traversal
12
1
6
11
2
3
3
5
9
10
5
4
6
7
1
2
4
7
8
10
8
9
11
12
27
(No Transcript)
28
(No Transcript)
29
Here we will work through an example showing how
a preorder traversal can be obtained by explicit
use of a stack. The logic follows (see the text
for the code) 1.Stack the
root node.
2.Exit with completion if the stack is empty.
3.Pop the
stack and visit the node obtained.
4.Push the right
child, if it exists, on the stack.
5.Push the left
child, if it exists, on the stack. 6.Go to 2.

30
(No Transcript)
31
(No Transcript)
32
Using the runtime stack via recursion, we can
write simple definitions of the three depth-first
traversals without use of an explicit stack (in
these definitions I leave out the templates for
brevity) void Preorder(TreeNode p) if (p)
Visit(p) Preorder(p-gtleft)
Preorder(p-gtright)
33
(No Transcript)
34
(No Transcript)
35
(No Transcript)
36
(No Transcript)
37
(No Transcript)
38
(No Transcript)
39
void Inorder(TreeNode p) if (p)
Inorder(p-gtleft) Visit(p)
Inorder(p-gtright) void Postorder(TreeNode
p) if (p) Postorder(p-gtleft)
Postorder(p-gtright) Visit(p)
40
(No Transcript)
41
(No Transcript)
42
(No Transcript)
43
(No Transcript)
44
TREE SORT Combines tree initialization,
insertion, and inorder traversal to generate and
print each node in a "sorted" binary tree. 1.
Initialize read first item, create root node. 2.
For each item after the first read item and
insert in tree. 3. Traverse in order, printing
each node. More formally, in pseudocode def
treesort () read item root makebt(null,
item, null) while not end of data read
item insert(root, item) end while
inorder(root) end
45
Using tree sort, sort the letters M, A, R, J, K,
S, V. Insertion produces M
/ \ A R
\ \
J S \
\ K
V Traverse in order gt A J K M R S V
About PowerShow.com