Binary Tree Tahsin

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Binary Tree

Presented By
Tahsin Akhter
183120018
What is Binary tree?

 A binary tree is a special type of tree in which every node or vertex has either no
child node or one child node or two child nodes. They are used to store and
organize data.

 A binary tree is a tree data structure composed of nodes, each of which has at
most, two children, referred to as left and right nodes. The tree starts off with a
single node known as the root.
Working Process:
 Each node in the tree contains the following:
 Data, pointer of left child and pointer of right child
 Here are the common operation are:
 Insertion : Elements may be inserted into a binary tree in any order. The very first insertion operation
creates the root node. Each insertion that follows iteratively searches for an empty location at each
level of the tree.
 Upon finding an empty left or right child, the new element is inserted. By convention, the insertion
always begins from the left child node.
 Deletion : An element may also be removed from the binary tree. Since there is no particular order
among the elements, upon deletion of a particular node, it is replaced with the right-most element.
 Let’s look at an example to get a better idea of how the deletion process works.
 3. Tree traversal
 Another frequently used tree operation is traversal. Tree traversal is the process of visiting each node
present in a tree. There are three methods of tree traversal:
 In-order traversal: Traverses a tree in a in-order manner.
 Pre order traversal: Traverses a tree in a pre-order manner.
 Post order traversal: Traverses a tree in a post-order manner.
Advantage of Binary tree
 we can always keep the cost of insert(), delete(), lookup() to O(logN) where N is the
number of nodes in the tree - so the benefit really is that lookups can be done in
logarithmic time which matters a lot when N is large.

 We have an ordering of keys stored in the tree. Any time we need to traverse the
increasing (or decreasing) order of keys, we just need to do the in-order (and
reverse in-order) traversal on the tree.

 BST can also be used in the design of memory allocators to speed up the search of
free blocks (chunks of memory), and to implement best fit algorithms where we are
interested in finding the smallest free chunk with size greater than or equal to size
specified in allocation request.
Limitations of Binary tree:

 The main disadvantage is that we should always implement a balanced binary


search tree - AVL tree, Red-Black tree, Splay tree. Otherwise the cost of operations
may not be logarithmic and degenerate into a linear search on an array.
Pseudo code:
 Insert operation: search operation:
Code implantation in c:
 Insert operation:
 Search operation:
Complexity analysis of BsT

 Time complexity of all BST Operations = O(n)

 Worst case : o(n)

 Best case: o(log n)


Application of BsT
 used to efficiently store data in sorted form in order to access and search stored
elements quickly.

 They can be used to represent arithmetic expressions (Refer here for more info )

 BST used in Unix kernels for managing a set of virtual memory areas (VMAs).

You might also like