2. Binary Tree Data Structure
• A tree whose
elements have at
most 2 children is
called a binary
tree. Since each
element in a binary
tree can have only
2 children, we
typically name
them the left and
right child.
3. Binary Search Tree Data Structure
• Binary Search Tree is a node-based binary tree data structure which
has the following properties:
• The left sub-tree of a node contains only nodes with keys lesser
than the node’s key.
• The right sub-tree of a node contains only nodes with keys greater
than the node’s key.
• The left and right sub-tree each must also be a binary search tree.
• There must be no duplicate nodes.
5. Binary Search Tree
• Draw a binary search tree by inserting the
following numbers from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• Binary tree mean a node having at most 2
children. Either 0, 1 or 2.
• The left subtree is less then its parent
node and right subtree is grater then its
parent.
6. Binary Search Tree
• Draw a binary search tree by inserting the
following numbers from left to right.
• 9 is less then 11 and 9 is greater then 7.
7. Binary Search Tree
• Draw a binary search tree by inserting the
following numbers from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• 6 is less then 11 and 8 is also less then 11,
so put it in left but 8 is greater then 6 so
put it in right.
8. Binary Search Tree
• Draw a binary search tree by inserting the
following numbers from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• Now 19 is greater then 11. place it in right.
9. Binary Search Tree
• Draw a binary search tree by inserting the
following numbers from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• 10 is less then 11 so go on left side.
10. Binary Search Tree
• Draw a binary search tree by inserting the
following numbers from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• 5 is less then 11, it could be in left of the
tree.
• 17 is greater then 11, it will be placed in
right.
11. Binary Search Tree
• Draw a binary search tree by
inserting the following numbers
from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• 43 on right, 31 is left of 43 and 49
will be in right of 43.
12. Binary Search Tree
• Draw a binary search tree by
inserting the following numbers
from left to right.
• If insert 60 in BST.
14. Binary Search Tree
• O(h) time on a tree of height h.
• The binary search tree is a balanced binary search tree.
• Height of the binary search tree becomes log(n).
• So, Time complexity of BST Operations = O(logn).
• The binary search tree is a skewed binary search tree.
• Height of the binary search tree becomes n.
• So, Time complexity of BST Operations = O(n).
15. Binary Search Tree (Deletion)
• Have 3 possibilities
• Node have 0 child
• Have 1 child
• Have 2 child
18. Binary Search Tree (Deletion)
• Node have 2 children
• In order predecessor
• In order successor
19. Binary Search Tree (Deletion)
• In order predecessor
• 10 is the largest element in left of the tree
20. Binary Search Tree (Deletion)
• In order successor
• The smallest element in right of the tree
21. Difference between Binary Tree and Binary
Search Tree:
• BINARY TREE is a non linear data structure where each node can
have almost two child nodes.
• BINARY SEARCH TREE is a node based binary tree which further has
right and left subtree that too are binary search tree.
• BINARY TREE is unordered hence slower in process of insertion,
deletion and searching.
• Insertion, deletion, searching of an element is faster in BINARY
SEARCH TREE than BINARY TREE due to the ordered characteristics
22. Difference between Binary Tree and Binary
Search Tree:
• IN BINARY TREE there is no ordering in terms of how the nodes are
arranged.
• IN BINARY SEARCH TREE the left subtree has elements less than the
nodes element and the right subtree has elements greater than the
nodes element.
24. Problems with BST:
• Height is not under control
• It depends on that how the elements we are going to insert.
• Sometimes its logn and sometimes its n.
• It’s a big issue in BST.
26. AVL Tree
Can we
arrange the
key elements
in BST?
First 4 orders
height is 3
and last two
elements its
2.
27. AVL Tree
• If we are arranging the elements in a order we get the
minimum height is the best order.
• We can reduce the time complexity of searching in entire
binary tree.
• That is drawback of a binary tree that we can make different
trees in different shapes with different heights.
• Whether BST can be improved or not?
• Yes, we should have a procedure which can convert the best
order tree.
29. AVL Tree
• In computer science, an AVL tree (named after inventors Adelson-Velsky and
Landis) is a self-balancing binary search tree. It was the first such data
structure to be invented.[2] In an AVL tree, the heights of the two child
subtrees of any node differ by at most one; if at any time they differ by more
than one, rebalancing is done to restore this property. Lookup, insertion,
and deletion all take O(log n) time in both the average and worst cases,
where n {displaystyle n} n is the number of nodes in the tree prior to the
operation. Insertions and deletions may require the tree to be rebalanced
by one or more tree rotations.
• The AVL tree is named after its two Soviet inventors, Georgy Adelson-Velsky
and Evgenii Landis, who published it in their 1962 paper "An algorithm for
the organization of information".[
•AVL is a height balanced binary search tree.
30. AVL Tree
• Balance factor = Height of left sub tree – height of right sub tree.
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
31. AVL Tree
• Balance factor = Height of left sub tree – height of right sub tree.
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• So the height of first tree is balanced and others are not balanced.
32. AVL Tree
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• Right of right
imbalanced
• 2 step rotation.