0% found this document useful (0 votes)
65 views5 pages

DA - 3.1 - Binary Search Tree: Terminology

This document discusses binary search trees, which can search as quickly as an ordered array and insert/delete items as quickly as a linked list. It defines key terminology like root, leaves, height. It explains that a binary search tree maintains the property that all children to the left of a node have values smaller than the node, and all children to the right have larger values. This allows searching in logarithmic time, though an unbalanced tree can degrade to linear time. The document discusses insertion, deletion, and different traversal orders of binary search trees.

Uploaded by

B S Praveen Bsp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views5 pages

DA - 3.1 - Binary Search Tree: Terminology

This document discusses binary search trees, which can search as quickly as an ordered array and insert/delete items as quickly as a linked list. It defines key terminology like root, leaves, height. It explains that a binary search tree maintains the property that all children to the left of a node have values smaller than the node, and all children to the right have larger values. This allows searching in logarithmic time, though an unbalanced tree can degrade to linear time. The document discusses insertion, deletion, and different traversal orders of binary search trees.

Uploaded by

B S Praveen Bsp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DA_3.

1 - Binary Search Tree


In previous semester, you have learnt about ordered arrays and linked lists. Ordered
arrays are good at searching or finding an element by using binary search algorithm but
poor in inserting and deleting an element. Similarly, Linked lists are good at inserting and
deleting a node but poor at searching an element. By using anyone of these data structures,
we will one of their unique advantages.
If we want a data structure which can search as quickly as an ordered array and can insert
and delete items as quickly as linked list, then we have to choose binary search trees.
Terminology:

Theory
A binary search tree is a tree where each node has a left and right child. Either child, or
both children, may be missing. The below figure illustrates a binary search tree. Assuming k
represents the value of a given node, and then a binary search tree also has the following
property: all children to the left of the node have values smaller than k, and all children to
the right of the node have values larger than k. The top of a tree is known as the root, and
the exposed nodes at the bottom are known as leaves. In Figure 3-2, the root is node 20 and
the leaves are nodes 4, 16, 37, and 43.
The height of a tree is the length of the longest path from root to leaf. For this example the
tree height is 2.

20
7
4

38
16

37

43

Copy right @ RGUKT

To search a tree for a given value, we


start at the root and work down. For
4
example, to search for 16, we first note
7
that 16 < 20 and we traverse to the left
16
child. The second comparison finds that
16 > 7, so we traverse to the right child.
20
On the third comparison, we succeed.
37
Each comparison results in reducing the
38
number of items to inspect by one-half.
43
In this respect, the algorithm is similar to
a binary search on an array. However,
this is true only if the tree is balanced. For example, see below picture shows another tree
containing the same values. While it is a binary search tree, its behavior is more like that of
a linked list, with search time increasing proportional to the number of elements stored.
The below picture is example for an unbalanced binary search tree

Insertion and Deletion


Let us examine insertions in a binary search tree to determine the conditions that can
cause an unbalanced tree. To insert an 18 in the tree in Figure 3-2, we first search for that
number. This causes us to arrive at node 16 with nowhere to go. Since 18 > 16, we simply
add node 18 to the right child of node 16 (Figure).
Now we can see how an unbalanced tree can occur. If the data is presented in an ascending
sequence, each node will be added to the right of the previous node. This will create one
long chain, or linked list. However, if data is presented for insertion in a random order, then
a more balanced tree is possible.
Deletions are similar, but require that the binary search tree property be maintained. For
example, if node 20 in Figure 3-4 is removed, it must be replaced by node 18 or node 37.

Copy right @ RGUKT

Assuming we replace a node by its successor, the resulting tree is shown in Figure 3-5. The
rationale for this choice is as follows. The successor for node 20 must be chosen such that
all nodes to the right are larger. Therefore we need to select the smallest valued node to the
right of node 20. To make the selection, chain once to the right (node 38), and then chain to
the left until the last node is found (node 37). This is the successor for node 20.

20
37
7

38

16

37

7
43

38
16

43
18

18

Binary Tree after Adding Node 18

Binary Tree
after deleting Node 20

Unbalanced binary search trees are not desirable because search time increases.
Worst case running time is O(N)
o What happens when you Insert elements in ascending order?
Insert: 2, 4, 6, 8, 10, 12 into an empty BST
o Problem: Lack of balance:
compare depths of left and right subtree
o Unbalanced degenerate tree
So, best case running time of BST operations is O(log N)
Traversal:
We can traverse binary search tree in three different ways. They are

Copy right @ RGUKT

Preorder traversal
o Visit Root node, Left Node and Right node
o If traverse the above binary search tree by using preorder, you will get this
sequence F, B, A, D, C, E, G, I, H (root, left, right).
Inorder traversal:
o Visit Left Node, Root Node and Right Node
o A, B, C, D, E, F, G, H, I (left, root, right); note how this produces a sorted
sequence
Postorder traversal:
o Visit left node, right node and root
o A, C, E, D, B, H, I, G, F (left, right, root)

Example2:

Web links:
http://en.wikipedia.org/wiki/Binary_search_tree
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/binarySearchTree.
htm

Copy right @ RGUKT

Multiple choice Questions


1. Does binary search tree take much time than the sorted array for searching an
element?
a. No
b. Yes
2. Does binary search tree take much time than the list to delete or insert an element?
a. Yes
b. No
3. Does binary tree and binary tree same?
a. yes
b. No
4. In which order of traversing a tree we get sorted order of sequence?
a. Inorder
b. Preorder
c. Postorder
d. None

Problem Set:
1. Write a program to traverse above example2 using Inorder, preorder and Postorder.

Copy right @ RGUKT

You might also like