0% found this document useful (0 votes)
4 views

Binary Search Tree

A Binary Search Tree (BST) is a hierarchical structure where each node's left subtree contains keys less than or equal to the node's key, and the right subtree contains keys greater than or equal to the node's key. There are two primary representations of binary trees: linked node representation, which allows for dynamic memory usage and straightforward node manipulation, and array representation, which is efficient for complete trees but can waste space in non-complete trees. Each representation has its advantages and disadvantages regarding memory usage and ease of navigation.

Uploaded by

madhulikathrasu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Binary Search Tree

A Binary Search Tree (BST) is a hierarchical structure where each node's left subtree contains keys less than or equal to the node's key, and the right subtree contains keys greater than or equal to the node's key. There are two primary representations of binary trees: linked node representation, which allows for dynamic memory usage and straightforward node manipulation, and array representation, which is efficient for complete trees but can waste space in non-complete trees. Each representation has its advantages and disadvantages regarding memory usage and ease of navigation.

Uploaded by

madhulikathrasu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Binary Search Tree

• A Binary Search Tree (BST) is a tree in which all the


nodes follow the below-mentioned properties −
• The left sub-tree of a node has a key less than or
equal to its parent node's key.
• The right sub-tree of a node has a key greater than
or equal to its parent node's key.
• Thus, BST divides all its sub-trees into two
segments; the left sub-tree and the right sub-tree
and can be defined as −
• left_subtree (keys) ≤ node (key) ≤ right_subtree
(keys)
Binary Tree Representation

• BST is a collection of nodes arranged in a way


where they maintain BST properties. Each
node has a key and an associated value. While
searching, the desired key is compared to the
keys in BST and if found, the associated value
is retrieved.
Following is a pictorial representation of BST −

• We observe that the root node key (27) has all less-valued keys
on the left sub-tree and the higher valued keys on the right sub-
tree.
• The properties that separate a binary search
tree from a regular binary tree is
– All nodes of left subtree are less than the root
node
– All nodes of right subtree are more than the root
node
– Both subtrees of each node are also BSTs i.e. they
have the above two properties
Binary Tree Representation

• Binary tree is a tree data structure (non-


linear) in which each node can have at most
two children which are referred to as
the left child and the right child. The topmost
node in a binary tree is called the root, and
the bottom-most nodes are called leaves. A
binary tree can be visualized as a hierarchical
structure with the root at the top and the
leaves at the bottom.
Representation of Binary Trees

• There are two primary ways to represent


binary trees:
• Linked Node Representation
• Array Representation
Linked Node Representation
• This is the simplest way to represent a binary
tree. Each node contains data and pointers to
its left and right children.
This representation is mostly used to
represent binary tree with multiple
advantages. The most common advantages
are given below.
• Advantages:
• It can easily grow or shrink as needed, so it uses only the
memory it needs.
• Adding or removing nodes is straightforward and requires
only pointer adjustments.
• Only uses memory for the nodes that exist, making it
efficient for sparse trees.
• Disadvantages:
• Needs extra memory for pointers.
• Finding a node can take longer because you have to start
from the root and follow pointers.
Array Representation

• Array Representation is another way to represent


binary trees, especially useful when the tree is
complete (all levels are fully filled except possibly
the last, which is filled from left to right). In this
method:
• The tree is stored in an array.
• For any node at index i:
– Left Child: Located at 2 * i + 1
– Right Child: Located at 2 * i + 2
• Root Node: Stored at index 0
• Advantages:
• Easy to navigate parent and child nodes using index
calculations, which is fast
• Easier to implement, especially for complete binary trees.
• Disadvantages:
• You have to set a size in advance, which can lead to
wasted space.
• If the tree is not complete binary tree then then many
slots in the array might be empty, this will result in
wasting memory
• Not as flexible as linked representations for dynamic
trees.

You might also like