0% found this document useful (0 votes)
2 views18 pages

12 CS253 CH8 Tree Part 2 BinaryTree

Uploaded by

hadibak2019
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
2 views18 pages

12 CS253 CH8 Tree Part 2 BinaryTree

Uploaded by

hadibak2019
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 18

Chapter 8

Binary Tree
Dr. Ameera Jaradat
ameera@yu.edu.jo

Data Structures and Algorithms in


Python
© 2013 Goodrich, Tamassia,
Goldwasser
binary tree
• A binary tree is a tree such that
– every node has at most 2 children
– each node is labeled as being either a left child or
a right child
binary tree Examples
A Recursive Binary Tree Definition
a binary tree is either empty or
consists of:
– A node r, called the root of T, that stores an element
– A binary tree (possibly empty), called the left subtree of T
– A binary tree (possibly empty), called the right subtree of T
A Recursive Binary Tree Definition

Root node

Left subtree Right subtree


A

B C

D F H

I I J I J
Binary Tree application: An arithmetic expression

• leaves are associated with variables or constants.


• internal nodes are associated with one of the
operators +, −, ×, and /.

This tree represents the expression ((((3 +1)×3)/((9 −5)+2))−


Figure 8.8:
((3×(7−4)) +6)).
Binary Tree application: decision trees

• Represent a number
of different
outcomes that can
result from
answering a series of
yes-or-no questions.
• Each internal node is
associated with a
question.

A decision tree providing


Figure 8.7:
investment advice
Perfect & Full binary tree
A Full binary tree is a binary tree Every internal node has
exactly 2 children
A perfect binary tree is a binary tree that satisfies the following
2 properties:
– Every internal node has exactly 2 children.
– All the leaf nodes are at the same level.

Full , not perfect Full , and perfect


Complete Binary Tree
A complete binary tree is a binary tree that satisfies the
following 2 properties:
– All the levels are completely filled except possibly the last level.
– The last level must be strictly filled from left to right.
Binary tree Samples

Perfect - Binary tree

Complete , full

Full
A B

Perfect binary tree

C D
Skewed Binary Tree
• Each node has only one left child
• Each node has only one right child
• skewed binary tree of n nodes has a depth of (n-1).
Properties of Binary Trees
• In general, level d has at most 2d nodes

Maximum number of nodes in the levels of a


Figure 8.9:
binary tree.
Perfect binary tree
level number of nodes in level
A 0  20 = 1

B C 1  21 = 2

D F G H 2  22 = 4

I J K L M N O P 3  23 = 8

Total number of nodes = 20 + 21 + 22 + 23 = 15

h
Maximum number of nodes in a binary tree with height = h is
∑ 2𝑘=2 h+1 − 1
𝑘=0
Perfect Binary Tree has Maximum number of nodes
Properties of Binary Trees
• In perfect binary tree
– Number of nodes at depth d is 2d.
– Total number of nodes of tree with height h = 2h+1 -1.
– Number of leaf nodes in a tree of height h = 2h
– Number of internal nodes in a tree of height h = 2h − 1
– The height of a tree with n nodes = log(n+1) -1.
• In general
– The minimum number of nodes in a binary tree of height h = h + 1
– The maximum number of nodes in a binary tree of height h = 2h+1 − 1
– The minimum height of a binary tree with n nodes = log(n+1) -1.
– The maximum height of a binary tree with n nodes = n-1.

log ( 𝑁 +1 ) − 1 ≤ 𝒉 ≤ 𝑁 − 1
h +1
h+ 1≤ 𝑵 ≤ 2 −1
Perfect BT has the max
number of nodes and the min
height

Skewed BT (path BT) has the


min number of nodes and
max height
Remember ....
0
2 =1 log 2 1 =0
2
1
=2 log 2 2 =1
22 =4 log 2 4 =2
23 =8 log 2 8 = 3
2 4 =16 log 2 16 = 4
5
2 =32 log 2 32 =5
26 =64 log 2 64 = 6
27 =128 log 2 128=7
8
2 =256 log 2 256= 8
2 9 =512 log 2 512 =9
210 = 1024 log 2 1024=10
Thanks for listening

: Next
IMPLEMENTING TREES
TREE TRAVERSAL

You might also like