binary trees - imran ihsanimranihsan.com/upload/lecture/dsaf1713.pdf · data structures and...

28
DATA STRUCTURES AND ALGORITHMS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD WWW.IMRANIHSAN.COM LECTURES ADAPTED FROM: DANIEL KANE, NEIL RHODES DEPARTMENT OF CS & ENGINEERING UNIVERSITY OF CALIFORNIA, SAN DIEGO 13 BINARY TREES INORDER, PREORDER, POSTORDER TRAVERSALS

Upload: hoangkhanh

Post on 14-Apr-2018

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

DATA STRUCTURES AND ALGORITHMS

IMRAN IHSANASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABADWWW.IMRANIHSAN.COM

LECTURES ADAPTED FROM:DANIEL KANE, NEIL RHODESDEPARTMENT OF CS & ENGINEERINGUNIVERSITY OF CALIFORNIA, SAN DIEGO

13BINARY TREESINORDER, PREORDER, POSTORDER TRAVERSALS

Page 2: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

DEFINITION

2

• The arbitrary number of children in general trees is often unnecessary—many real-life trees are restricted to two branches

• Expression trees using binary operators

• An ancestral tree of an individual, parents, grandparents, etc.

• Phylogenetic trees

• Lossless encoding algorithms

• There are also issues with general trees:

• There is no natural order between a node and its children

Page 3: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

DEFINITION

3

• A binary tree is a restriction where each node has exactly two children:

• Each child is either empty or another binary tree

• This restriction allows us to label the children as left and right subtrees

We will also refer to the two sub-trees as

• The left-hand sub-tree, and

• The right-hand sub-tree

Page 4: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

DEFINITION

4

• Sample variations on binary trees with five nodes:

Page 5: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

DEFINITION

5

• A full node is a node where both the left and right sub-trees are non-empty trees

Legend:

full nodes neither leaf nodes

Page 6: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

DEFINITION

6

• An empty node or a null sub-tree is any location where a new leaf node could be appended

Page 7: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

DEFINITION

7

• A full binary tree is where each node is:• A full node, or• A leaf node

• These have applications in• Expression trees• Huffman encoding

Page 8: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

BINARY NODE CLASS

8

• The binary node class is similar to the single node class:

#include <algorithm>

template <typename Type>

class Binary_node {

protected:

Type element;

Binary_node *left_tree;

Binary_node *right_tree;

public:

Binary_node( Type const & );

Type retrieve() const;

Binary_node *left() const;

Binary_node *right() const;

bool empty() const;

bool is_leaf() const;

int size() const;

int height() const;

void clear();

}

Page 9: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

BINARY NODE CLASS

9

CONSTRUCTOR

• We will usually only construct new leaf nodes

template <typename Type>

Binary_node<Type>::Binary_node( Type const &obj ):

element( obj ),

left_tree( nullptr ),

right_tree( nullptr ) {

// Empty constructor

}

Page 10: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

BINARY NODE CLASS

10

ACCESSORS

• The accessors are similar to that of Single_list

template <typename Type>

Type Binary_node<Type>::retrieve() const {

return element;

}

template <typename Type>

Binary_node<Type> *Binary_node<Type>::left() const {

return left_tree;

}

template <typename Type>

Binary_node<Type> *Binary_node<Type>::right() const {

return right_tree;

}

Page 11: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

BINARY NODE CLASS

11

BASIC FUNCTIONALITY

• Much of the basic functionality is very similar to Simple_tree

template <typename Type>

bool Binary_node<Type>::empty() const {

return ( this == nullptr );

}

template <typename Type>

bool Binary_node<Type>::is_leaf() const {

return !empty() && left()->empty() && right()->empty();

}

Page 12: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

BINARY NODE CLASS

12

SIZE

template <typename Type>

int Binary_node<Type>::size() const {

return empty() ? 0 :

1 + left()->size() + right()->size();

}

Page 13: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

BINARY NODE CLASS

13

HEIGHT

template <typename Type>

int Binary_node<Type>::height() const {

return empty() ? -1 :

1 + std::max( left()->height(), right()->height() );

}

Page 14: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

BINARY NODE CLASS

14

CLEAR

• Removing all the nodes in a tree is similarly recursive:

template <typename Type>

void Binary_node<Type>::clear( Binary_node *&ptr_to_this ) {

if ( empty() ) {

return;

}

left()->clear( left_node );

right()->clear( right_node );

delete this;

ptr_to_this = nullptr;

}

Page 15: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

APPLICATION:

15

EXPRESSION TREES

Any basic mathematical expression containing binary operators may be represented using a binary tree

For example, 3(4a + b + c) + d/5 + (6 – e)

Page 16: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

APPLICATION:

16

EXPRESSION TREES

Observations:

• Internal nodes store operators

• Leaf nodes store literals or variables

• No nodes have just one sub tree

• The order is not relevant for• Addition and multiplication (commutative)

• Order is relevant for• Subtraction and division (non-commutative)

• It is possible to replace non-commutative operators using the unary negation and inversion:

(a/b) = a b-1 (a – b) = a + (–b)

Page 17: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

APPLICATION:

17

EXPRESSION TREES

• A post-order depth-first traversal converts such a tree to the reverse-Polish format

3 4 a × b c + + × d 5 ÷ 6 e – + +

Page 18: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

APPLICATION:

18

EXPRESSION TREES

• Humans think in in-order

• Computers think in post-order:

• Both operands must be loaded into registers

• The operation is then called on those registers

• Most use in-order notation (C, C++, Java, C#, etc.)

• Necessary to translate in-order into post-order

Page 19: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

FULL VS COMPLETE BINARY TREE

19

• A full BT of depth k is a binary tree of depth k having 2k -1 nodes, k>=0.

• A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k.

Full Binary Tree Complete Binary Tree

B C

A

D FE

J

G

KH I J KJ K

B C

A

D FE G

H I

Page 20: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

LABELING NODES

20

• Label the nodes 1 through 2k+1 – 1.

• Label by levels from top to bottom.

• Within a level, label from left to right.

2 3

1

4 65

14

7

158 9 12 1310 11

Page 21: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

NODE NUMBER PROPERTIES

21

• Parent of node i is node i / 2, unless i = 1.

• Node 1 is the root and has no parent.

• Left child of node i is node 2i, unless 2i > n,

where n is the number of nodes.

• If 2i > n, node i has no left child.

• Right child of node i is node 2i+1, unless 2i+1 > n,

where n is the number of nodes.

• If 2i+1 > n, node i has no right child. 2 3

1

4 65

14

7

158 9 12 1310 11

Page 22: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

ARITHMETIC EXPRESSION USING BT

22

• inorder traversal

• A / B * C * D + E

• infix expression

• preorder traversal

• + * * / A B C D E

• prefix expression

• postorder traversal

• A B / C * D * E +

• postfix expression

• level order traversal

• + * E * D / C A B

+

*

A

*

/

E

D

C

B

Page 23: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

INORDER TRAVERSAL (RECURSIVE VERSION)

10 – Binary Trees | Data Strutures & Algorithms

Imran Ihsan - www.imranihsan.com |23

void inorder(ptnode ptr)

/* inorder tree traversal */

{

if (ptr) {

inorder(ptr->left);

cout<<ptr->data;

indorder(ptr->right);

}

} A / B * C * D + E

Page 24: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

PREORDER TRAVERSAL (RECURSIVE VERSION)

10 – Binary Trees | Data Strutures & Algorithms

Imran Ihsan - www.imranihsan.com |24

void preorder(ptnode ptr)

/* preorder tree traversal */

{

if (ptr) {

cout<<ptr->data;

preorder(ptr->left);

predorder(ptr->right);

}

} + * * / A B C D E

Page 25: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

POSTORDER TRAVERSAL (RECURSIVE VERSION)

10 – Binary Trees | Data Strutures & Algorithms

Imran Ihsan - www.imranihsan.com |25

void postorder(ptnode ptr)

/* postorder tree traversal */

{

if (ptr) {

postorder(ptr->left);

postdorder(ptr->right);

cout<<ptr->data;

}

} A B / C * D * E +

Page 26: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

LEVELORDER TRAVERSAL (USING QUEUE)

10 – Binary Trees | Data Strutures & Algorithms

Imran Ihsan - www.imranihsan.com |26

void levelOrder(ptnode ptr)

/* level order tree traversal */

{

int front = rear = 0;

ptnode queue[MAX_QUEUE_SIZE];

if (!ptr) return; /* empty queue */

enqueue(front, &rear, ptr);

for (;;) {

ptr = dequeue(&front, rear);

if (ptr) {

cout<<ptr->data;

if (ptr->left)

enqueue(front, &rear, ptr->left);

if (ptr->right)

enqueue(front, &rear, ptr->right);

}

else break;

+ * E * D / C A B

Page 27: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

EULER TOUR TRAVERSAL

10 – Binary Trees | Data Strutures & Algorithms

Imran Ihsan - www.imranihsan.com |27

• Generic traversal of a binary tree

• Includes a special cases the preorder, postorder and inorder traversals

• Walk around the tree and visit each node three times:• on the left (preorder)

• from below (inorder)

• on the right (postorder) +

-2

5 1

3 2

L

B

R

Page 28: BINARY TREES - Imran Ihsanimranihsan.com/upload/lecture/DSAF1713.pdf · data structures and algorithms imran ihsan assistant professor, air university, islamabad lectures adapted

EULER TOUR TRAVERSAL

10 – Binary Trees | Data Strutures & Algorithms

Imran Ihsan - www.imranihsan.com |28

eulerTour(node v) {

//perform action for visiting node on the left;

if v is internal then

eulerTour(v->left);

//perform action for visiting node from below;

if v is internal then

eulerTour(v->right);

//perform action for visiting node on the right;

}