0% found this document useful (0 votes)
9 views

CS 05

- A binary tree is either a leaf node or an internal node connected to two binary subtrees (left and right). - Tree traversals like preorder and postorder recursively visit nodes in a specified order - preorder visits a node before its children, postorder after. - Evaluating an arithmetic expression tree uses postorder traversal - evaluating children then applying the operator at each node. - Inorder traversal visits the left subtree, then the node, then the right subtree.

Uploaded by

rohitsingh884882
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

CS 05

- A binary tree is either a leaf node or an internal node connected to two binary subtrees (left and right). - Tree traversals like preorder and postorder recursively visit nodes in a specified order - preorder visits a node before its children, postorder after. - Evaluating an arithmetic expression tree uses postorder traversal - evaluating children then applying the operator at each node. - Inorder traversal visits the left subtree, then the node, then the right subtree.

Uploaded by

rohitsingh884882
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Recursive Definition of Binary Tree

• A binary tree is either a


• leaf or
A
• An internal node (the
root) and one/two
B C
binary trees (left subtree
and/or right subtree).
D F H

7
An arithmetic express tree
• ((x + 5) % 3) − (3x + 8) —

% +

+ 3 3x 8

x 5

8
Decision Tree
Stipend at least
• Should I accept an internship offer 10K a month

Commute more Decline Offer


than 1 hour

Decline Offer
Offers free coffee

Accept Offer Decline Offer

9
Complete Binary Tree
i
• Level i has 2 nodes

• In a tree of height h

• Leaves are at level h


h
• Total # leaves is 2
2 h−1
• Total # internal nodes = 1 + 2 + 2 + … + 2 = 2h − 1
• Total # internal nodes = # leaves −1

( 2 )
h h h+1 n+1
• Total # nodes n = 2 + 2 − 1 = 2 −1 height = log2

n+1
• Total # leaves = 2 10
Binary Tree
• A binary tree can be obtained from an appropriate complete binary tree
by pruning

11
Minimum height of a binary tree
• A binary tree of height h has
i
• At most 2 nodes at level i
2 h h+1
• At most 1 + 2 + 2 + … + 2 = 2 − 1 nodes

12
Minimum height of a binary tree
• A binary tree of height h has
i
• At most 2 nodes at level i
2 h h+1
• At most 1 + 2 + 2 + … + 2 = 2 − 1 nodes

• If the tree has n nodes then,


h+1
• n≤2 −1

• h ≥ log2(n + 1)/2

13
Maximum height of a binary tree
• A binary tree of n nodes has height at most n − 1

14
Number of leaves in a binary tree
• Number of leaves is ≤ 1+ number of internal nodes
• Proof is by induction on number of internal nodes
• Tree with 1 node has a leaf but no internal node
• Assume the statement is true for a tree with at most k − 1 internal
nodes

• A tree with k internal nodes has k1 internal nodes in left subtree and
(k − k1 − 1) internal nodes in right subtree.

• Number of leaves ≤ (k1 + 1) + (k − k1) = k + 1

15
Leaves in a binary tree
• For a binary tree on n nodes
• # leaves + # internal nodes = n

• # leaves ≤ 1+ # internal nodes


(n + 1)
• Hence, #leaves ≤ 2

• Minimum #leaves = 1

16
Abstract Data Types (ADTs) for Trees
• size() • positions ()
• isEmpty() • swapElements (p, q)
• elements() • replaceElement (p, e)

• isRoot (p)
• root ()
• isInternal (p, q)
• parent (p)
• replaceElement (p)
• children (p)
• isExternal(p)
17
Abstract Data Types (ADTs) for Binary Trees

• root ()
• parent (p)
• children (p)
• leftChild (p)
• rightChild (p)
• sibling(p)

18
The Node Structure for a Binary Tree p(x)
Key Element
• Each node in the tree contains
left(x) right(x)
• key(x) ⟶ key
• element(x) ⟶ element p(x)
• left(x) ⟶ pointer to the left child Key Element
• right(x) ⟶ pointer to the right child left(x) right(x)

• p(x) ⟶ pointer to the parent node


p(x) p(x)
Key Element Key Element
left(x)
19
right(x) left(x) right(x)
Unbounded Branching
• parent →
UnboundedTree

• firstChild →
UnboundedTree

• nextSibling →
UnboundedTree

20
Tree Walks/Traversals
• A tree walk is a way of visiting all the nodes in a tree in a specified order.
• A visit to a node means we are doing some processing on the data of
that node.

• A preorder tree walk processes each node before processing its children.
• A postorder tree walk processes each node after processing its children.

21
Preorder walk A

• Preorder
Traversal
B C D E F G

H I J K L M N

22
Preorder walk A

• Preorder
Traversal
B C D E F G

H I J K L M N

Algorithm preOrder(v)
visit node v
for each child w of v
recursively perform preOrder (w)

23
Postorder walk
• Postorder
traversal

24
Postorder walk

Algorithm postOrder(v)
for each child w of v
recursively perform postOrder (w)
visit node v

25
Traversals of Binary Trees

• preorder (v) • Postorder (v)


if (v = null) then return if (v = null) then return
else else
visit (v) preorder (v.leftchild())
preorder (v.leftchild()) preorder (v.rightchild())
preorder (v.rightchild()) visit (v)

26
Examples
A
• Preorder traversal
A, B, C, D, F, G, E
B E

• Postorder traversal C D

C, F, G, D, B, E, A

F G

27
Evaluating Mathematical Expression
• ((x + 5) % 3) − (3x + 8) —

% +

+ 3 3x 8

x 5

28
Evaluating Mathematical Expression

• Algorithm evaluate (v)


if v is a leaf
return the variable stored at v
else
let ⊚ be the operator stored at v
x ← evaluate v.leftchild()
y ← evaluate v.rightchild()
return x ⊚ y
29
Inorder Traversal
• Algorithm inOrder (v) A

if (v = null) then return


B E
else
inOrder (v . leftChild())
C D
visit (v)
inOrder (v . rightChild())
F G

• Inorder traversal
C, B, F, D, G, A, E
30
Euler Tour Traversal
• Generic Traversal of a binary tree

• Walk around the tree and visit each node 3 times
• On the left
• From below
% +
• On the right

+ 3 3x 8

x 5
31
Printing an arithmetic expression ((x + 5) % 3) − (3x + 8)
• Use Euler Walk

• Print ′(′ before traversing the left subtree.
• Traverse the left subtree
• Print the element at the node
% +
• Traverse the right subtree

• Print ′)′ 3 8
+ 3x

x 5
32

You might also like