19

Binary tree

Embed Size (px)

Citation preview

Page 1: Binary tree
Page 2: Binary tree

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 3: Binary tree

Binary Trees

ARUN KUMAR K [email protected]/

arunkumar3040twitter.com/arunkumar3040in.linkedin.com/in/

arunkumar3040+919496349799

Page 4: Binary tree

Tree Terminology

• A tree consists of a collection of elements or nodes, with each node linked to its successors

• The node at the top of a tree is called its root• The links from a node to its successors are

called branches• The successors of a node are called its children• The predecessor of a node is called its parent

Page 5: Binary tree

Tree Terminology (continued)

• Each node in a tree has exactly one parent except for the root node, which has no parent

• Nodes that have the same parent are siblings• A node that has no children is called a leaf

node

Page 6: Binary tree

Binary Trees

•A tree in which no node can have more than two children.

Page 7: Binary tree

Example: Expression Trees

• Leaves are operands (constants or variables)• The other nodes (internal nodes) contain

operators• Will not be a binary tree if some operators are

not binary

Page 8: Binary tree

Example: Expression Trees

Expression tree for ( a + b * c ) + ( ( d * e + f ) * g

Page 9: Binary tree

Binary Tree Traversal

• Traversal is the process of visiting every node once

• 3 types of traversals*Inorder*Preorder*Postorder

Page 10: Binary tree

Preorder traversal

Expression tree for ( a + b * c ) + (( d * e + f ) * g

• root, left, right• prefix expression

++a*bc*+*defg

Page 11: Binary tree

Inorder traversal

Expression tree for ( a + b * c ) +( ( d * e + f ) * g

• left, root, right• infix expression

a+b*c+d*e+f*g

Page 12: Binary tree

Postorder traversal

Expression tree for ( a + b * c ) + (( d * e + f ) * g

• left, right, root• postfix expression

abc*+de*f+g*+

Page 13: Binary tree

InsertProceed down the tree as you would find a close matchIf X is found, do nothing (or update something)Otherwise, insert X at the last spot on the path traversed

Page 14: Binary tree

DeleteConsider children of deleted nodeProperty of the search tree should be maintained.Three cases:(1) the node is a leaf

– Delete it immediately(2) the node has one child

– Adjust a pointer from the parent to bypass that node

Page 15: Binary tree

Delete(3) the node has 2 children

– replace the key of that node with the minimum element at the right subtree

– delete the minimum element • Has either no child or only right child because if it has a left child, that

left child would be smaller and would have been chosen. So invoke case 1 or 2.

Page 16: Binary tree

Questions…..

Page 17: Binary tree
Page 18: Binary tree

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 19: Binary tree

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]