Lab 11: Implementation of The BINARY SEARCH TREE Data Structure With The Help of Algorithms
Lab 11: Implementation of The BINARY SEARCH TREE Data Structure With The Help of Algorithms
Lab 11: Implementation of The BINARY SEARCH TREE Data Structure With The Help of Algorithms
Alogrithms
Semester BS CS – 03
Introduction to :
1. BINARY SEARCH TREE structure representation
2. Creation of Binary Search Tree
3. Traversing over Binary Search Tree
Binary Tree
We extend the concept of linked data structures to structure containing nodes with more than one
self-referenced field. A binary tree is made of nodes, where each node contains a "left" reference,
a "right" reference, and a data element. The topmost node in the tree is called the root.
Every node (excluding a root) in a tree is connected by a directed edge from exactly one other
node. This node is called a parent. On the other hand, each node can be connected to arbitrary
number of nodes, called children. Nodes with no children are called leaves, or external nodes.
Nodes which are not leaves are called internal nodes. Nodes with the same parent are called
siblings.
First of all, binary search tree (BST) is a dynamic data structure, which means, that its size is only
limited by amount of free memory in the operating system and number of elements may vary
during the program run. Main advantage of binary search trees is rapid search, while addition is
quite cheap. Let us see more formal definition of BST.
Binary search tree is a data structure, which meets the following requirements:
it is a binary tree;
each node contains a value;
a total order is defined on these values (every two values can be compared with each
other);
Department of Computer Sciences 1/4 Semester BSCS 3
CSC-221 Data Structures & Algorithms Lab 11: Implementation of the BINARY SEARCH TREE
data structure with the help of algorithms
left subtree of a node contains only values lesser, than the node's value;
right subtree of a node contains only values greater, than the node's value.
Notice, that definition above doesn't allow duplicates.
Depending on the size of user data, memory overhead may vary, but in general it is quite
reasonable. In some implementations, node may store a link to the parent, but it depends on
algorithm, programmer want to apply to BST. For basic operations, like addition, removal and
search a link to the parent is not necessary. It is needed in order to implement iterators.
With a view to internal representation, the sample from the overview changes:
Leaf nodes have links to the children, but they don't have children. In a programming language it
Tree Traversal:
A traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data
structure, there is no unique traversal. We will consider several traversal algorithms with we
group in the following two kinds
depth-first traversal
breadth-first traversal
There are three different types of depth-first traversals, :
PreOrder traversal - visit the parent first and then left and right children;
InOrder traversal - visit the left child, then the parent and the right child;
PostOrder traversal - visit left child, then the right child and then the parent;
There is only one kind of breadth-first traversal--the level order traversal. This traversal visits
nodes by levels from top to bottom and from left to right.
As an example consider the following tree and its four traversals:
60
25
20 35 59 65