SlideShare a Scribd company logo
TREES
• A tree is a hiearchical structure . 
• Collection of nodes or Finite set of nodes This 
collection can be empty Nodes and Edges.
TREE TERMINOLOGY 
• NODE:A node is the key component of the tree 
data structure that stores the data element and 
may contain zero,one or more link to other 
sucessor node for connectivity. A tree consist of 
finite set of nodes. 
• EDGE or LINK: A directed line from one node to the 
other successor node is called edge of the tree.i 
• It is also known as link or arc or branch of a tree. 
• A tree consists of a finite set of edges that connects 
node.
• PARENT: The immediate predecessor of a node is 
called its parent.All the nodes except the root node 
have exactly one parent. 
• CHILD: All the immediate successors of a node are 
known as its child. If a node has two child then one 
on the left is called left child and other on the right 
is called right child. 
• SIBLING: Two or more nodes with same parents are 
called siblings. 
• ROOT: The topmost node of the tree is called the 
root of the tree.One can reach any node of the tree 
from the root node by following the edges.
• LEAF: The node that does not have any child node 
is called leaf node. 
• PATH: A path is a result in sequence of nodes when 
we traverse from a node to a node along the edges 
that connect them. There is always a unique path 
from the root to any other node in a tree. 
• LEVEL: The level of the node is an integer value that 
measures the distance of a node from the root.As 
the root is at zero distance from itself so it is at 
level 0. 
• DEGREE: The maximum number of children that are 
possible for a node is known as degree ,like in 
binary tree max 2 children can be there so degree is 
2
• DEPTH: The depth of a node is the length(number 
of edges) of the path from the root to the node of 
the tree. 
• Depth of the node is sometimes also referred to the level of a node. 
• HEIGHT: The height of a tree is the length (number 
of edges) of the path from the node to its furthest 
leaf .
BINARY TREE 
• A binary tree is a special form of tree in which a 
node can have atmost two children 
• A node in a binary tree may not necessarily have 
the maximum of two children i.e either 1,2 or 0 
child. 
• A tree which does not contain any node is called 
empty binary node.
Binary Tree 
Full binary tree Complete binary tree
Full Binary Tree 
A binary tree is said to be full binary tree 
if each node has exactly zero or two 
children. 
Also known as proper binary tree
Complete Binary Tree 
A complete binary tree is either a full 
binary tree or one in which every level is 
fully occupied except possibly for the 
bottomost level where all the nodes 
must be as far left as possible.
PROPERTIES OF BINARY TREE 
• 1.The maximum number of nodes in a binary tree 
on a given level (say L) is 2L ,where L>=0 . 
• 2.The maximum number of nodes in an binary tree 
with height h is 2h+1-1 
• 3.the minimum number of nodes possible in a 
binary tree of height h is h+1
• 4.if n is the number of nodes and e is the number of 
edges in an non-empty binary tree then n=e+1. 
• 5.if n0 is the number of leaf nodes (no child) and n2 is 
the number of nodes with two children in a non-empty 
binary tree then n0= n2+1 
• 6.for a complete binary tree T with n nodes ,the 
height is floor[log2(n+1) -1]
Binary Tree Traversal Techniques 
• Three recursive techniques for binary tree 
traversal 
• In each technique, the left subtree is traversed 
recursively, the right subtree is traversed 
recursively, and the root is visited 
• What distinguishes the techniques from one 
another is the order of those 3 tasks 
13
Preoder, Inorder, Postorder 
• In Preorder, the root 
is visited before (pre) 
the subtrees traversals 
• In Inorder, the root is 
visited in-between left 
and right subtree traversal 
• In Preorder, the root 
is visited after (pre) 
the subtrees traversals 
Preorder Traversal: 
1. Visit the root 
2. Traverse left subtree 
3. Traverse right subtree 
Inorder Traversal: 
1. Traverse left subtree 
2. Visit the root 
3. Traverse right subtree 
Postorder Traversal: 
1. Traverse left subtree 
2. Traverse right subtree 
3. Visit the root 14
Illustrations for Traversals 
1 
3 
8 9 
11 
5 
4 6 
7 
10 
12 
15
Illustrations for Traversals 
• Assume: visiting a node 
is printing its label 
• Preorder: 
1 3 5 4 6 7 8 9 10 11 12 
• Inorder: 
4 5 6 3 1 8 7 9 11 10 12 
• Postorder: 
4 6 5 3 8 11 12 10 9 7 1 
1 
3 
8 9 
11 
5 
4 6 
7 
10 
12 
16
Illustrations for Traversals (Contd.) 
6 
15 
8 
2 
3 7 
11 
10 
14 
12 
20 
27 
22 30 
17
Illustrations for Traversals (Contd.) 
• Assume: visiting a node 
is printing its data 
• Preorder: 15 8 2 6 3 7 
11 10 12 14 20 27 22 30 
• Inorder: 3 6 7 2 8 10 11 
12 14 15 20 22 27 30 
• Postorder: 3 7 6 2 10 14 
12 11 8 22 30 27 20 15 
6 
15 
8 
2 
3 7 
11 
10 
14 
12 
20 
27 
22 30 
18
• Assume: visiting a node 
is printing its label 
• Preorder: 
1 3 5 4 6 7 8 9 10 11 12 
• Inorder: 
4 5 6 3 1 8 7 9 11 10 12 
• Postorder: 
4 6 5 3 8 11 12 10 8 9 7 1
Assume: visiting a node 
is printing its data 
Preorder: 15 8 2 6 3 7 
11 10 12 14 20 27 22 30 
Inorder: 3 6 7 2 8 10 11 
12 14 15 20 22 27 30 
Postorder: 3 7 6 2 10 14 
12 11 8 22 30 27 20 15
Tree Traversals 
Pre-Order(NLR) 
1, 3, 5, 9, 6, 8
Tree Traversals 
In-Order(LNR) 
5, 3, 9, 1, 8, 6
Tree Traversals 
Post-Order(LRN) 
5, 9, 3, 8, 6, 1
PRE-ORDER USING STACK 
• PREORD(INFO,LEFT,RIGHT,ROOT): A binary tree 
T is in memory .The algorithm does a 
preorder traversal of T, applying an 
operation PROCESS to each of its node. An 
array STACK is used to temporarily hold the 
addresses of nodes.
• 1.[Initiaaly push NULL onto STACK and initialize 
PTR] 
Set TOP1.STACK[1]NULL and PTRROOT 
2.Repeat steps 3 to 5 while PTR!=NULL 
3.Apply PROCESS to INFO[PTR] 
4.[Right Child?] 
If RIGHT[PTR]!=NULL [Push on STACK] 
TOPTOP+1 
STACK[TOP]RIGHT[PTR] 
[End of if structure]
• 5.[Left Child ?] 
If LEFT[PTR]!=NULL, 
Set PTRLEFT[PTR] 
Else [pop from stack] 
PTRSTACK[TOP] 
TOPTOP-1 
[End of if strucutre] 
[End of step 2 loop] 
6.Exit
IN-ORDER 
• INORDER(INFO,LEFT,RIGHT,ROOT):A binary tree is 
in memory. This algorithm does an inorder traversal 
.applying PROCESS to each of its node.An array 
STACK is used to temporarily hold the addresses of 
node.
1.[Push NULL onto STACK and initialize PTR] 
TOP1,STACK[1]NULL and PTRROOT 
2.Repeat while PTR!=NULL [Push left most path onto 
stack] 
a)TOPTOP+1 and STACK[TOP]PTR [Saves 
nodes] 
b)PTRLEFT[PTR] [updates PTR] 
[End of loop] 
3.PTRSTACK[TOP] 
TOPTOP-1 [pops node from STACK]
4.Repeat steps 5 to 7 while PTR!=NULL [Backtracking] 
5.Apply PROCESS to INFO[PTR] 
6.[Right child?] 
If RIGHT[PTR]!=NULL 
a)PTRRIGHT[PTR] 
b)GOTO step 2 
7.PTRSTACK[TOP] 
TOP=TOP-1 [Pops node] 
[end of step 4 loop] 
8.Exit
POSTORDER 
• POSTORD(INFO,LEFT,RIGHT,ROOT):A binary 
tree T is in memory.This algorithm does a 
postorder traversal of T,applying an 
operation PROCESS to each of its node.An 
array STACK is used to temporarily hold the 
address of nodes
1.[push NULL onto STACK and initialize PTR] 
Set TOP1,STACK[1]NULL,PTRROOT 
2.[Push left-most path onto STACK] 
Repeat steps 3 to 5 while(PTR!=NULL) 
3.TOPTOP+1 
STACK[TOP]PTR [ Pushes PTR on STACK] 
4.if RIGHT[PTR]!=NULL, then [Push on STACK] 
TOPTOP+1 and STACK[TOP]=-RIGHT[PTR]. 
[end of If strucutre]
Set PTRLEFT[PTR] [Updates pointer PTR] 
[End of Step 2 loop] 
6.Set PTRSTACK[TOP] and TOPTOP-1 [Pops node from 
STACK] 
7.Repeat while PTR>0. 
A)Apply PROCESS to INFO[PTR] 
B)Set PTRSTACK[TOP] and TOPTOP-1 [pops node 
from STACK] 
8.If PTR<0 ,then: 
a)PTR-PTR 
b)Goto step 2. 
[End of If structure] 
9.EXIT
Binary Search Tree
INTRODUCTION 
• Binary Search Tree abbreviated as BST is a special 
form of binary tree whose nodes are arranged in 
such a way that for every node N,the values 
contained in all nodes in its left sub tree are less the 
value contained in N and values contained in the 
right sub tree are larger than the node N
SEARCHING 
• Searching an item is the key operation performed on a BST. 
When we want to search given item(ITEM) in BST ,we begin by 
first comparing the ITEM with the value in root. 
• A)if they are equal then the location of the root node is 
returned. 
• B)If the ITEM is less than the value of the root then we need to 
search the left sub tree of the root. The right sub tree is 
eliminated . 
• C)if the ITEM is greater than the value of the root then we 
need to search the right sub tree of the root.
Searching in BST 
• BSTSEARCH(ROOT,ITEM): Given linked BST whose root 
node is pointed to by pointer ROOT. A variable PTR is 
used which is a pointer that points to the current node 
being processed. Another local variable LOC returns the 
location of a given ITEM. The local variable FLAG contains 
Boolean value which is TRUE(i.e. 1) if search is successful 
otherwise FALSE(i.e. 0) . This algo searches for a given 
ITEM from BST.
1. PTRROOT [Stores address of ROOT into PTR] 
2. FLAG0 [Assume search is unsuccessful] 
3. LOCNULL 
4. Repeat steps while PTR!=NULL and FLAG=0 
5. If ITEM =INFO(PTR) then [Item found] 
FLAG1, LOCPTR 
Else if ITEM < INFO(PTR) then [Item in left subtree] 
PTRLEFT(PTR) 
Else 
PTRRIGHT(PTR) [Item in right subtree] 
[End if structure] 
[End of step 4 loop]
INSERTION IN BST 
• Insertion of new node into binary search tree is 
very simple operation based on the search 
operation. 
• In order to insert a node with given item into 
BST ,we begin searching at the root and descend 
level by level moving left or right subtree as 
appropriate comparing the value of the current 
node and node being inserted. 
• When we reach a node with empty subtree(i.e leaf) 
then we insert the new node as its left or right child 
depending on ITEM.
• While searching if the ITEM to be inserted is 
already in the tree then do nothing as we know 
binary tree cannot hold the duplicate values. 
• Also note that as insertion take place only at leaf 
node so we only need ITEM to be inserted and not 
the location of the insertion
• BST_INSERT(ROOT,ITEM,AVAIL) –Given a linked 
list BST whose root node is pointed to by a pointer 
ROOT.A local variable PTR is used which points to the 
current node being processed. The variable PPTR is a 
pointer that points to the parent of the new node 
containing ITEM in its INFO part. The pointer 
variable NEW points to the new node and the AVAIL 
pointer points to the first node in the availability 
list.This algorithm inserts the given ITEM into BST.
1. If AVAIL=NULL then [No space for new node] 
Write “ Overflow”. 
return 
[End of if structure] 
2. [Get new node from availability list] 
a) NEWAVIAL 
b) AVAILLEFT(AVAIL) 
c) INFO(NEW)ITEM [Copy ITEM into INFO part of new node] 
3.[New node has no children] 
a) LEFT(NEW)NULL 
b) RIGHT(NEW)NULL 
4.If ROOT=NULL then [Tree is empty] 
ROOTNEW [Insert new node as root] 
return 
[End of if structure]
[Remaining steps insert new node at appropriate location in non empty tree] 
5.PTRROOT, PTRNULL 
6. Repeat steps 7 and 8 while PTR!=NULL [Finding parent of new node] 
7.PPTRPTR [ Make current node as parent] 
8. If ITEM> INFO(PTR) then [Item>current node’s INFO part] 
PTRRIGHT(PTR) [Move towards right subtree] 
ELSE 
PTRLEFT(PTR) [Move towards left subtree] 
[End of If structure] 
[End of step 6 loop] 
9.If ITEM<INFO(PPTR) then 
LEFT(PPTR)NEW [Insert new node as left child] 
Else 
RIGHT(PPTR)NEW [Insert new as right child] 
10. Return
DELETION IN BST 
BSTDELETE(ROOT,ITEM ,AVAIL)-Given a linked binary 
search tree whose root is pointed to by pointer ROOT. A local variable 
PTR is used which is a pointer that points to the current node being 
processed. The variable PPTR is a pointer that points to the parent of 
ITEM. The local variable LOC returns the location of given ITEM that 
is to be deleted. The FLAG variable contains a Boolean value which is 
TRUE if search is successful , other wise FALSE. This algo deletes the 
given ITEM from BST.
1.PTRROOT, FLAG0 [Initialize PTR, assume search successful] 
2. Repeat steps 3 while PTR!=NULL [All nodes served ] and FLAG=0 [Search 
Unsuccessful] 
3. If ITEM=INFO(PTR) then [Node to be deleted found] 
FLAG1, LOCPTR 
Else If ITEM < INFO(PTR) then [ITEM in left subtree] 
PPTRPTR [Make current node as a parent] 
PTRLEFT(PTR) [Make a left child as current node] 
Else [ITEM in right subtree] 
PPTRPTR 
PTRRIGHT(PTR) 
[End of If structure] 
[End of step 3 loop] 
4. If LOC=NULL then 
Write “ITEM does not exist” 
return
[End of If structure] 
5. If RIGHT(LOC)!=NULL and LEFT(LOC)!=NULL then [ Case 3] 
Call DELNODE_TWOCH(ROOT, LOC, PPTR) 
Else[Case 1 and Case 2] 
Call DELNODE_ZRONECH(ROOT,LOC,PPTR) 
[End of If structure] 
6.[Returning deleted node to availability list] 
LEFT(LOC)AVAIL 
AVAILLOC 
7. Return
DELNODE_ZRONECH 
• DELNODE_ZRONECH (ROOT,LOC,PPTR): 
This algorithm deletes a node with zero or one 
child from BST. A local variable CHILDNODE 
will be set to NULL if the deleted node does 
not have any child or will point to left or right 
child of deleted node otherwise.
1.[Initialize childnode] 
If LEFT(LOC)=NULL and RIGHT(LOC)=NULL then [No Child] 
CHILDNODENULL 
Else If LEFT(LOC)!=NULL then 
CHILDNODELEFT(LOC) 
Else 
CHILDNODERIGHT(LOC) 
[End of If structure] 
2.If PPTR=NULL then [No parent] 
ROOTCHILDNODE 
Else If LOC=LEFT(PPTR) then [Deleted node to the left of parent node] 
LEFT(PPTR)CHILDNODE 
ELSE [Deleted Node to the right of parent node] 
RIGHT(PPTR)CHILDNODE 
[End of If structure] [End of structure] 3. Return
DELNODE_TWOCH 
DELNODE_TWOCH(ROOT,LOC,PPTR): This algorithm 
deletes a node with two children from BST . LOC points to 
the node to be deleted and PPTR points to its parent. A 
local variable SUCC points to the inorder successor of 
node to be deleted. The local variable PARSUCC points to 
the parent of inorder successor.
[Find inorder successor node and parent of successor node[Step 1 and 2]] 
1.SUCCRIGHT(LOC), PARSUCCLOC 
2. Repeat while LEFT(SUCC) !=NULL 
PARSUCCSUCC [ Make successor as parent] 
SUCCLEFT(SUCC) [ Make left child of successor as successor node] 
[End of Step 2 loop] 
3. Call DELNODE_ZRONECH(ROOT,SUCC,PARSUCC) [Delete inorder successor] 
[Steps 4 and 5 replace node N by its inorder successor] 
4. If PARSUCC=NULL then [No Parent] 
ROOTSUCC 
Else if LOC=LEFT(PPTR) then [deleted node to left of its parent] 
LEFT(PPTR)SUCC 
Else [deleted node to right of its parent] 
RIGHT(PPTR)SUCC
[End of If structure] 
5. a) LEFT(SUCC)LEFT(LOC) 
b) RIGHT(SUCC)RIGHT(LOC) 
6. Return

More Related Content

What's hot

Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)
amna izzat
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
Sadaf Ismail
 
Linked lists
Linked listsLinked lists
Linked lists
Eleonora Ciceri
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
Zaid Shabbir
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
Dharita Chokshi
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
varagilavanya
 
Binary trees
Binary treesBinary trees
Binary trees
Simratpreet Singh
 
Trees
TreesTrees
Trees
9590133127
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
Katang Isip
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
E2
E2E2
E2
lksoo
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
Ashim Lamichhane
 
Sorting techniques
Sorting techniquesSorting techniques
Sorting techniques
Lovely Professional University
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
Celine George
 
Standard data-types-in-py
Standard data-types-in-pyStandard data-types-in-py
Standard data-types-in-py
Priyanshu Sengar
 
Set Similarity Search using a Distributed Prefix Tree Index
Set Similarity Search using a Distributed Prefix Tree IndexSet Similarity Search using a Distributed Prefix Tree Index
Set Similarity Search using a Distributed Prefix Tree Index
HPCC Systems
 
Tree representation in map reduce world
Tree representation  in map reduce worldTree representation  in map reduce world
Tree representation in map reduce world
Yu Liu
 

What's hot (19)

Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
Linked lists
Linked listsLinked lists
Linked lists
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
 
Binary trees
Binary treesBinary trees
Binary trees
 
Trees
TreesTrees
Trees
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Lecture7 data structure(tree)
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
E2
E2E2
E2
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Sorting techniques
Sorting techniquesSorting techniques
Sorting techniques
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Standard data-types-in-py
Standard data-types-in-pyStandard data-types-in-py
Standard data-types-in-py
 
Set Similarity Search using a Distributed Prefix Tree Index
Set Similarity Search using a Distributed Prefix Tree IndexSet Similarity Search using a Distributed Prefix Tree Index
Set Similarity Search using a Distributed Prefix Tree Index
 
Tree representation in map reduce world
Tree representation  in map reduce worldTree representation  in map reduce world
Tree representation in map reduce world
 

Similar to Trees

binary tree
binary treebinary tree
binary tree
Shankar Bishnoi
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
MouDhara1
 
Trees second part in data structures with examples
Trees second part in data structures with examplesTrees second part in data structures with examples
Trees second part in data structures with examples
rupanaveen24
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
KamranAli649587
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptx
RaaviKapoor
 
Binary tree
Binary treeBinary tree
Binary tree
Afaq Mansoor Khan
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
kalyanineve
 
Tree chapter
Tree chapterTree chapter
Tree chapter
LJ Projects
 
trees
trees trees
Binary tree
Binary treeBinary tree
Binary tree
MKalpanaDevi
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
AdityaK92
 
Tree
TreeTree
Tree
sbkbca
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
Data structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxData structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptx
AhmedEldesoky24
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
sumitbardhan
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
Rai University
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
Rai University
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
Getachew Ganfur
 

Similar to Trees (20)

binary tree
binary treebinary tree
binary tree
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Trees second part in data structures with examples
Trees second part in data structures with examplesTrees second part in data structures with examples
Trees second part in data structures with examples
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
Binary Search Tree.pptx
Binary Search Tree.pptxBinary Search Tree.pptx
Binary Search Tree.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Tree chapter
Tree chapterTree chapter
Tree chapter
 
trees
trees trees
trees
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Tree
TreeTree
Tree
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Data structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxData structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptx
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 

Recently uploaded

The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 

Recently uploaded (20)

The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 

Trees

  • 2. • A tree is a hiearchical structure . • Collection of nodes or Finite set of nodes This collection can be empty Nodes and Edges.
  • 3. TREE TERMINOLOGY • NODE:A node is the key component of the tree data structure that stores the data element and may contain zero,one or more link to other sucessor node for connectivity. A tree consist of finite set of nodes. • EDGE or LINK: A directed line from one node to the other successor node is called edge of the tree.i • It is also known as link or arc or branch of a tree. • A tree consists of a finite set of edges that connects node.
  • 4. • PARENT: The immediate predecessor of a node is called its parent.All the nodes except the root node have exactly one parent. • CHILD: All the immediate successors of a node are known as its child. If a node has two child then one on the left is called left child and other on the right is called right child. • SIBLING: Two or more nodes with same parents are called siblings. • ROOT: The topmost node of the tree is called the root of the tree.One can reach any node of the tree from the root node by following the edges.
  • 5. • LEAF: The node that does not have any child node is called leaf node. • PATH: A path is a result in sequence of nodes when we traverse from a node to a node along the edges that connect them. There is always a unique path from the root to any other node in a tree. • LEVEL: The level of the node is an integer value that measures the distance of a node from the root.As the root is at zero distance from itself so it is at level 0. • DEGREE: The maximum number of children that are possible for a node is known as degree ,like in binary tree max 2 children can be there so degree is 2
  • 6. • DEPTH: The depth of a node is the length(number of edges) of the path from the root to the node of the tree. • Depth of the node is sometimes also referred to the level of a node. • HEIGHT: The height of a tree is the length (number of edges) of the path from the node to its furthest leaf .
  • 7. BINARY TREE • A binary tree is a special form of tree in which a node can have atmost two children • A node in a binary tree may not necessarily have the maximum of two children i.e either 1,2 or 0 child. • A tree which does not contain any node is called empty binary node.
  • 8. Binary Tree Full binary tree Complete binary tree
  • 9. Full Binary Tree A binary tree is said to be full binary tree if each node has exactly zero or two children. Also known as proper binary tree
  • 10. Complete Binary Tree A complete binary tree is either a full binary tree or one in which every level is fully occupied except possibly for the bottomost level where all the nodes must be as far left as possible.
  • 11. PROPERTIES OF BINARY TREE • 1.The maximum number of nodes in a binary tree on a given level (say L) is 2L ,where L>=0 . • 2.The maximum number of nodes in an binary tree with height h is 2h+1-1 • 3.the minimum number of nodes possible in a binary tree of height h is h+1
  • 12. • 4.if n is the number of nodes and e is the number of edges in an non-empty binary tree then n=e+1. • 5.if n0 is the number of leaf nodes (no child) and n2 is the number of nodes with two children in a non-empty binary tree then n0= n2+1 • 6.for a complete binary tree T with n nodes ,the height is floor[log2(n+1) -1]
  • 13. Binary Tree Traversal Techniques • Three recursive techniques for binary tree traversal • In each technique, the left subtree is traversed recursively, the right subtree is traversed recursively, and the root is visited • What distinguishes the techniques from one another is the order of those 3 tasks 13
  • 14. Preoder, Inorder, Postorder • In Preorder, the root is visited before (pre) the subtrees traversals • In Inorder, the root is visited in-between left and right subtree traversal • In Preorder, the root is visited after (pre) the subtrees traversals Preorder Traversal: 1. Visit the root 2. Traverse left subtree 3. Traverse right subtree Inorder Traversal: 1. Traverse left subtree 2. Visit the root 3. Traverse right subtree Postorder Traversal: 1. Traverse left subtree 2. Traverse right subtree 3. Visit the root 14
  • 15. Illustrations for Traversals 1 3 8 9 11 5 4 6 7 10 12 15
  • 16. Illustrations for Traversals • Assume: visiting a node is printing its label • Preorder: 1 3 5 4 6 7 8 9 10 11 12 • Inorder: 4 5 6 3 1 8 7 9 11 10 12 • Postorder: 4 6 5 3 8 11 12 10 9 7 1 1 3 8 9 11 5 4 6 7 10 12 16
  • 17. Illustrations for Traversals (Contd.) 6 15 8 2 3 7 11 10 14 12 20 27 22 30 17
  • 18. Illustrations for Traversals (Contd.) • Assume: visiting a node is printing its data • Preorder: 15 8 2 6 3 7 11 10 12 14 20 27 22 30 • Inorder: 3 6 7 2 8 10 11 12 14 15 20 22 27 30 • Postorder: 3 7 6 2 10 14 12 11 8 22 30 27 20 15 6 15 8 2 3 7 11 10 14 12 20 27 22 30 18
  • 19. • Assume: visiting a node is printing its label • Preorder: 1 3 5 4 6 7 8 9 10 11 12 • Inorder: 4 5 6 3 1 8 7 9 11 10 12 • Postorder: 4 6 5 3 8 11 12 10 8 9 7 1
  • 20. Assume: visiting a node is printing its data Preorder: 15 8 2 6 3 7 11 10 12 14 20 27 22 30 Inorder: 3 6 7 2 8 10 11 12 14 15 20 22 27 30 Postorder: 3 7 6 2 10 14 12 11 8 22 30 27 20 15
  • 21. Tree Traversals Pre-Order(NLR) 1, 3, 5, 9, 6, 8
  • 22. Tree Traversals In-Order(LNR) 5, 3, 9, 1, 8, 6
  • 24. PRE-ORDER USING STACK • PREORD(INFO,LEFT,RIGHT,ROOT): A binary tree T is in memory .The algorithm does a preorder traversal of T, applying an operation PROCESS to each of its node. An array STACK is used to temporarily hold the addresses of nodes.
  • 25. • 1.[Initiaaly push NULL onto STACK and initialize PTR] Set TOP1.STACK[1]NULL and PTRROOT 2.Repeat steps 3 to 5 while PTR!=NULL 3.Apply PROCESS to INFO[PTR] 4.[Right Child?] If RIGHT[PTR]!=NULL [Push on STACK] TOPTOP+1 STACK[TOP]RIGHT[PTR] [End of if structure]
  • 26. • 5.[Left Child ?] If LEFT[PTR]!=NULL, Set PTRLEFT[PTR] Else [pop from stack] PTRSTACK[TOP] TOPTOP-1 [End of if strucutre] [End of step 2 loop] 6.Exit
  • 27. IN-ORDER • INORDER(INFO,LEFT,RIGHT,ROOT):A binary tree is in memory. This algorithm does an inorder traversal .applying PROCESS to each of its node.An array STACK is used to temporarily hold the addresses of node.
  • 28. 1.[Push NULL onto STACK and initialize PTR] TOP1,STACK[1]NULL and PTRROOT 2.Repeat while PTR!=NULL [Push left most path onto stack] a)TOPTOP+1 and STACK[TOP]PTR [Saves nodes] b)PTRLEFT[PTR] [updates PTR] [End of loop] 3.PTRSTACK[TOP] TOPTOP-1 [pops node from STACK]
  • 29. 4.Repeat steps 5 to 7 while PTR!=NULL [Backtracking] 5.Apply PROCESS to INFO[PTR] 6.[Right child?] If RIGHT[PTR]!=NULL a)PTRRIGHT[PTR] b)GOTO step 2 7.PTRSTACK[TOP] TOP=TOP-1 [Pops node] [end of step 4 loop] 8.Exit
  • 30. POSTORDER • POSTORD(INFO,LEFT,RIGHT,ROOT):A binary tree T is in memory.This algorithm does a postorder traversal of T,applying an operation PROCESS to each of its node.An array STACK is used to temporarily hold the address of nodes
  • 31. 1.[push NULL onto STACK and initialize PTR] Set TOP1,STACK[1]NULL,PTRROOT 2.[Push left-most path onto STACK] Repeat steps 3 to 5 while(PTR!=NULL) 3.TOPTOP+1 STACK[TOP]PTR [ Pushes PTR on STACK] 4.if RIGHT[PTR]!=NULL, then [Push on STACK] TOPTOP+1 and STACK[TOP]=-RIGHT[PTR]. [end of If strucutre]
  • 32. Set PTRLEFT[PTR] [Updates pointer PTR] [End of Step 2 loop] 6.Set PTRSTACK[TOP] and TOPTOP-1 [Pops node from STACK] 7.Repeat while PTR>0. A)Apply PROCESS to INFO[PTR] B)Set PTRSTACK[TOP] and TOPTOP-1 [pops node from STACK] 8.If PTR<0 ,then: a)PTR-PTR b)Goto step 2. [End of If structure] 9.EXIT
  • 34. INTRODUCTION • Binary Search Tree abbreviated as BST is a special form of binary tree whose nodes are arranged in such a way that for every node N,the values contained in all nodes in its left sub tree are less the value contained in N and values contained in the right sub tree are larger than the node N
  • 35. SEARCHING • Searching an item is the key operation performed on a BST. When we want to search given item(ITEM) in BST ,we begin by first comparing the ITEM with the value in root. • A)if they are equal then the location of the root node is returned. • B)If the ITEM is less than the value of the root then we need to search the left sub tree of the root. The right sub tree is eliminated . • C)if the ITEM is greater than the value of the root then we need to search the right sub tree of the root.
  • 36. Searching in BST • BSTSEARCH(ROOT,ITEM): Given linked BST whose root node is pointed to by pointer ROOT. A variable PTR is used which is a pointer that points to the current node being processed. Another local variable LOC returns the location of a given ITEM. The local variable FLAG contains Boolean value which is TRUE(i.e. 1) if search is successful otherwise FALSE(i.e. 0) . This algo searches for a given ITEM from BST.
  • 37. 1. PTRROOT [Stores address of ROOT into PTR] 2. FLAG0 [Assume search is unsuccessful] 3. LOCNULL 4. Repeat steps while PTR!=NULL and FLAG=0 5. If ITEM =INFO(PTR) then [Item found] FLAG1, LOCPTR Else if ITEM < INFO(PTR) then [Item in left subtree] PTRLEFT(PTR) Else PTRRIGHT(PTR) [Item in right subtree] [End if structure] [End of step 4 loop]
  • 38. INSERTION IN BST • Insertion of new node into binary search tree is very simple operation based on the search operation. • In order to insert a node with given item into BST ,we begin searching at the root and descend level by level moving left or right subtree as appropriate comparing the value of the current node and node being inserted. • When we reach a node with empty subtree(i.e leaf) then we insert the new node as its left or right child depending on ITEM.
  • 39. • While searching if the ITEM to be inserted is already in the tree then do nothing as we know binary tree cannot hold the duplicate values. • Also note that as insertion take place only at leaf node so we only need ITEM to be inserted and not the location of the insertion
  • 40. • BST_INSERT(ROOT,ITEM,AVAIL) –Given a linked list BST whose root node is pointed to by a pointer ROOT.A local variable PTR is used which points to the current node being processed. The variable PPTR is a pointer that points to the parent of the new node containing ITEM in its INFO part. The pointer variable NEW points to the new node and the AVAIL pointer points to the first node in the availability list.This algorithm inserts the given ITEM into BST.
  • 41. 1. If AVAIL=NULL then [No space for new node] Write “ Overflow”. return [End of if structure] 2. [Get new node from availability list] a) NEWAVIAL b) AVAILLEFT(AVAIL) c) INFO(NEW)ITEM [Copy ITEM into INFO part of new node] 3.[New node has no children] a) LEFT(NEW)NULL b) RIGHT(NEW)NULL 4.If ROOT=NULL then [Tree is empty] ROOTNEW [Insert new node as root] return [End of if structure]
  • 42. [Remaining steps insert new node at appropriate location in non empty tree] 5.PTRROOT, PTRNULL 6. Repeat steps 7 and 8 while PTR!=NULL [Finding parent of new node] 7.PPTRPTR [ Make current node as parent] 8. If ITEM> INFO(PTR) then [Item>current node’s INFO part] PTRRIGHT(PTR) [Move towards right subtree] ELSE PTRLEFT(PTR) [Move towards left subtree] [End of If structure] [End of step 6 loop] 9.If ITEM<INFO(PPTR) then LEFT(PPTR)NEW [Insert new node as left child] Else RIGHT(PPTR)NEW [Insert new as right child] 10. Return
  • 43. DELETION IN BST BSTDELETE(ROOT,ITEM ,AVAIL)-Given a linked binary search tree whose root is pointed to by pointer ROOT. A local variable PTR is used which is a pointer that points to the current node being processed. The variable PPTR is a pointer that points to the parent of ITEM. The local variable LOC returns the location of given ITEM that is to be deleted. The FLAG variable contains a Boolean value which is TRUE if search is successful , other wise FALSE. This algo deletes the given ITEM from BST.
  • 44. 1.PTRROOT, FLAG0 [Initialize PTR, assume search successful] 2. Repeat steps 3 while PTR!=NULL [All nodes served ] and FLAG=0 [Search Unsuccessful] 3. If ITEM=INFO(PTR) then [Node to be deleted found] FLAG1, LOCPTR Else If ITEM < INFO(PTR) then [ITEM in left subtree] PPTRPTR [Make current node as a parent] PTRLEFT(PTR) [Make a left child as current node] Else [ITEM in right subtree] PPTRPTR PTRRIGHT(PTR) [End of If structure] [End of step 3 loop] 4. If LOC=NULL then Write “ITEM does not exist” return
  • 45. [End of If structure] 5. If RIGHT(LOC)!=NULL and LEFT(LOC)!=NULL then [ Case 3] Call DELNODE_TWOCH(ROOT, LOC, PPTR) Else[Case 1 and Case 2] Call DELNODE_ZRONECH(ROOT,LOC,PPTR) [End of If structure] 6.[Returning deleted node to availability list] LEFT(LOC)AVAIL AVAILLOC 7. Return
  • 46. DELNODE_ZRONECH • DELNODE_ZRONECH (ROOT,LOC,PPTR): This algorithm deletes a node with zero or one child from BST. A local variable CHILDNODE will be set to NULL if the deleted node does not have any child or will point to left or right child of deleted node otherwise.
  • 47. 1.[Initialize childnode] If LEFT(LOC)=NULL and RIGHT(LOC)=NULL then [No Child] CHILDNODENULL Else If LEFT(LOC)!=NULL then CHILDNODELEFT(LOC) Else CHILDNODERIGHT(LOC) [End of If structure] 2.If PPTR=NULL then [No parent] ROOTCHILDNODE Else If LOC=LEFT(PPTR) then [Deleted node to the left of parent node] LEFT(PPTR)CHILDNODE ELSE [Deleted Node to the right of parent node] RIGHT(PPTR)CHILDNODE [End of If structure] [End of structure] 3. Return
  • 48. DELNODE_TWOCH DELNODE_TWOCH(ROOT,LOC,PPTR): This algorithm deletes a node with two children from BST . LOC points to the node to be deleted and PPTR points to its parent. A local variable SUCC points to the inorder successor of node to be deleted. The local variable PARSUCC points to the parent of inorder successor.
  • 49. [Find inorder successor node and parent of successor node[Step 1 and 2]] 1.SUCCRIGHT(LOC), PARSUCCLOC 2. Repeat while LEFT(SUCC) !=NULL PARSUCCSUCC [ Make successor as parent] SUCCLEFT(SUCC) [ Make left child of successor as successor node] [End of Step 2 loop] 3. Call DELNODE_ZRONECH(ROOT,SUCC,PARSUCC) [Delete inorder successor] [Steps 4 and 5 replace node N by its inorder successor] 4. If PARSUCC=NULL then [No Parent] ROOTSUCC Else if LOC=LEFT(PPTR) then [deleted node to left of its parent] LEFT(PPTR)SUCC Else [deleted node to right of its parent] RIGHT(PPTR)SUCC
  • 50. [End of If structure] 5. a) LEFT(SUCC)LEFT(LOC) b) RIGHT(SUCC)RIGHT(LOC) 6. Return