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

Data Structure: Tree

Trees are hierarchical data structures with a root node and child nodes. Binary trees restrict nodes to having at most two children. Binary search trees organize nodes so that all left descendants of a node are less than the node and all right descendants are greater. Heaps are complete binary trees where the root node has the greatest (max-heap) or least (min-heap) value among its children.

Uploaded by

Mahomuda Akter
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)
44 views

Data Structure: Tree

Trees are hierarchical data structures with a root node and child nodes. Binary trees restrict nodes to having at most two children. Binary search trees organize nodes so that all left descendants of a node are less than the node and all right descendants are greater. Heaps are complete binary trees where the root node has the greatest (max-heap) or least (min-heap) value among its children.

Uploaded by

Mahomuda Akter
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/ 20

Data Structure:

Tree
Trees
• Trees are hierarchical data structures
• The topmost node is called root of the tree
• The elements that are directly under an element are called its
children

2
Trees
• The element directly above something is called its parent
• Information stored in tree naturally forms a hierarchy
• Level is number of nodes on a path from root to the node, including
root and node where level of root is 0

3
Trees
• The depth of a node is the number of edges from the node to the
tree's root node where a root node will have a depth of 0
• The height of a node is the number of edges
on the longest path from the node to a leaf where
leaf node will have a height of 0
• The diameter (or width) of a tree is the number
of nodes on the longest path between any two
leaf nodes
• The tree here has a diameter of 6 nodes

4
Binary Tree
• A tree with at most 2 children is called a binary tree
• We typically name the children the left and right child

5
Types of Binary Tree
• Full Binary Tree: A Binary Tree is a full binary tree if every
node has 0 or 2 children
• Complete Binary Tree: A Binary Tree is a complete Binary
Tree if all the levels are completely filled except possibly
the last level and the last level has all keys as left as
possible
• Perfect Binary Tree: A Binary tree is a Perfect Binary Tree
in which all the internal nodes have two children and all
leaf nodes are at the same level

6
Binary Search Tree
• Binary Search Tree is a node-based binary tree data structure which
has the following properties:
• The left subtree of a node contains only nodes with keys lesser than the
node’s key.
• The right subtree of a node contains only nodes with keys greater than the
node’s key.
• The left and right subtree each must also be a binary search tree.
• There must be no duplicate nodes.

7
Binary Search Tree: Predecessor(পূর্র্র্তী
ব ) and
Successor (উত্তরাধিকারী)
• Predecessor is the node that lies behind of given node and
Successor is the node that lies ahead of given node

Successor

Predecessor

8
Binary Search Tree: Predecessor(পূর্র্র্তী
ব ) and
Successor (উত্তরাধিকারী)
• If X has two children, its predecessor is the maximum value in its left
subtree

9
Binary Search Tree: Predecessor(পূর্র্র্তী
ব ) and
Successor (উত্তরাধিকারী)
• If X has two children, its successor is the minimum value in its right
subtree

10
Binary Search Tree: Insertion
• Insert 21 in the following tree:

11
Binary Search Tree: Insertion
if insertion point is found
insert the value
if value to be inserted < this key
go left
else go right

12
Binary Search Tree: Deletion
• Delete 32 from the following tree:

13
Binary Search Tree: Deletion
search for v
if v is a leaf
delete leaf v
else if v has 1 child
bypass v
else replace v with successor

14
Binary Search Tree: Deletion
• Delete 29 from the following tree:

15
Binary Search Tree: Deletion
search for v
if v is a leaf
delete leaf v
else if v has 1 child
bypass v
else replace v with successor

16
Binary Search Tree: Deletion
• Delete 65 from the following tree:

17
Binary Search Tree: Deletion
search for v
if v is a leaf
delete leaf v
else if v has 1 child
bypass v
else replace v with successor

18
Binary Heap
• A Heap is a special Tree-based data structure in which the tree is a
complete binary tree. Generally, Heaps can be of two types:
• Max-Heap: In a Max-Heap the key present at the root node must be greatest
among the keys present at all of it’s children. The same property must be
recursively true for all sub-trees in that Binary Tree
• Min-Heap: In a Min-Heap the key present at the root node must be minimum
among the keys present at all of it’s children. The same property must be
recursively true for all sub-trees in that Binary Tree

19
Creating Binary Heap
• Create a binary heap using following elements:
2,7,26,9,17,49,3,36,72,5

20

You might also like