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

Binary Search Tree

Stack and Queue in Data Structure

Uploaded by

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

Binary Search Tree

Stack and Queue in Data Structure

Uploaded by

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

Trees

• A tree is a data structure that is defined as a set of one or more nodes


which allow us to associate a parents child relationship.
• In trees one node is designated as the root node or parent node, and
all the remaining nodes can be partitioned into non-empty sets ,each
of which is a subtree of the root.
• A root node can only have child nodes. On the contrary, leave nodes
or leaves are those that have no children. When there are no nodes in
the tree then the tree is known as a null tree or empty tree.
Definitions
• Node-A node is a main component of the tree data structure. It stores
the actual data along with the links to the other notes.
• Root- The root node is the topmost node of the tree. It does not have
a parent node. If the root node is empty then the tree is empty
• Parent- The parent of a node is the immediate predecessor of that
node. In the following figure X is the parent of the Y and Z nodes.
• Child- The child nodes are the immediate successors of a node. They
must have a parent node. A child node placed at the left side is called
the left child and similarly a child node placed at the right side is
called the right child.
• Leaf nodes- A leaf node is one which does not have any child nodes.
• Sub trees-The nodes B, X and Y from the left subtree of root A.
similarly the nodes C and Z from the right subtree of A.
Binary search tree
• A binary search tree (BST) is a variant of a binary tree. The special
property of a binary search tree is that all the nodes in the left
subtree have a value less than that of root node. Similarly, all the
nodes in the rights of tree have a value more than that of a root
node.
Operations on binary search tree
• Searching node
• inserting a node
• deleting a node
• determining the height of the binary search tree
• finding the largest node in the binary search tree
• finding the smallest node in the binary search tree
Algorithm for searching a node in the binary
search tree
• Search(Root, Value)
• Step 1: Start
• Step 2: If (Root==Null)
• Return Null
• Print “empty tree”
• Else if(Root—> info==Value)
• Return root
• Else if (root—>info>value)
• Search(root —>Lchild ,value)
• Else if(root—>info<value)
• Search(Root—>Rchild, value)
• Else
• Print “value not found”
• [end of if]———4
• Step 3: end
Example
Inserting a node in the binary search tree
• Insert (Root, Value)
• Step 1:start
• Step 2: if (root==null)
• Allocate memory for root node
• Set root—> info=value
• Set root—>Lchild=Root—>Rchild =null
• [end of if]
• Step 3: if(root—>info >value)
• Insert (root—>Lchild, value)
• Else
• Insert(root—>Rchild,value)
• [end of if]
• Step 4:end
Example
• Insert a new node with the value 7 in the binary search tree.
Deleting a node from the binary search tree
Deleting or not having one child
• Delete a node with the value 10
Deleting a note having two children
• For example delete a note with the value 42 from the binary search
tree
Algorithm for deleting node
• Delete_Node(Root,Value)
• Step 1: start
• Step 2: if(root== null)
• Print “error”
• [end of if]
• Step 3: if (root —>info>value)
• Delete_node(root—>Lchild, value)
• Else if (root—>info<value)
• Delete_node(root—>Rchild,value)
• Else
• If(root—> Lchild=null& Root —>Rchild=Null)
• Free(root)
• Else if( root—>Lchild &Root—>Rchild)
• Temp=find_largest(root—>Lchild)
• Or
• Temp=Find_smallest(root—>Rchild)
• Set root—>info=temp—>info
• Free(temp)
• Else
• If(root—>Lchild!=null)
• Set temp=root—>Lchild
• Set root—>info=temp—>info
• Free(temp)
• Set temp=root—>Rchild
• Set root—>info=temp—info
Deletion in BST
Delete a leaf node (no children)
Delete a node
Binary tree traversal methods
• Pre-order traversal
• In order traversal
• Post order
Pre-order traversal
• In pre order traversal ,the following operations are performed
recursively at each node:
• Visit the root node
• Traverse the left subtree
• Traverse the right subtree
Function for pre-order traversal
• Void pre-order (struct BST * root)
•{
• if root!= Null)
• { printf(“%d”,root->info);
• Pre-order(root->Lchild);
• Pre-order(root->Rchild);
•}
•}
In-order traversal
• In in-order traversal, the following operations are performed
recursively at each node:
• 1. Traverse the left subtree.
• 2. Visit the root node.
• 3. Traverse the right subtree
• Example
Function for an in order traversal
• Void in-order(struct BST*root)
• {if (root !=Null)
•{
• In-order(root->lchild);
• Printf(“%d”, root-info);
• In-order(root->Rchild);
•}
•}
Post order traversal
• In a post order traversal the following operations were performed
recursively at each node:
• 1. Traverse the left subtree
• 2. Traverse the right subtree
• 3. Visit the root node
• Example
Function for post order traversal
• Void post-order(struct BST*root)
•{
• If (root !=Null)
•{
• Post-order(root->lchild)
• Post-order(root->Rchild)
• Printf(“%d”,root->info);
•}
•}
Construct binary tree from in order and post
order traversal
• In order H,D,P,L,A,Z,C,E
• Postorder. H,P,L,D,E,C,Z,A
• In order H,D,P,L,A,Z,C,E
• Preorder A,D,H,L,P,Z,C,E
AVL Tree introduction
• In binary search tree
How to calculate balance factor
How to create a AVL tree
• 55,25,65,9,8,15
• BST 8,9,10
• 10, 9,8
• 10,12,11
• 10,8,9
AVL
• 5,7,19,12,10,15,18,20,25,23
AVL

You might also like