Buet Admission About Tree
Buet Admission About Tree
Buet Admission About Tree
The first node from where the tree originates is called as a root node.
In any tree, there must be only one root node.
We can never have multiple root nodes in a tree data structure.
Example-
2. Edge-
Example-
3. Parent-
The node which has a branch from it to any other node is called as a parent
node.
In other words, the node which has one or more children is called as a
parent node.
In a tree, a parent node can have any number of child nodes.
Example-
Here,
Node A is the parent of nodes B and C
Node B is the parent of nodes D, E and F
Node C is the parent of nodes G and H
Node E is the parent of nodes I and J
Node G is the parent of node K
4. Child-
Example-
Here,
Nodes B and C are the children of node A
Nodes D, E and F are the children of node B
Nodes G and H are the children of node C
Nodes I and J are the children of node E
Node K is the child of node G
5. Siblings-
Example-
Here,
Nodes B and C are siblings
Nodes D, E and F are siblings
Nodes G and H are siblings
Nodes I and J are siblings
6. Degree-
Example-
Here,
Degree of node A = 2
Degree of node B = 3
Degree of node C = 2
Degree of node D = 0
Degree of node E = 2
Degree of node F = 0
Degree of node G = 1
Degree of node H = 0
Degree of node I = 0
Degree of node J = 0
Degree of node K = 0
7. Internal Node-
The node which has at least one child is called as an internal node.
Internal nodes are also called as non-terminal nodes.
Every non-leaf node is an internal node.
Example-
8. Leaf Node-
The node which does not have any child is called as a leaf node.
Leaf nodes are also called as external nodes or terminal nodes.
Example-
Example-
10. Height-
Total number of edges that lies on the longest path from any leaf node to
a particular node is called as height of that node.
Height of a tree is the height of root node.
Height of all leaf nodes = 0
Example-
11. Depth-
Total number of edges from root node to a particular node is called as depth
of that node.
Depth of a tree is the total number of edges from root node to a leaf node
in the longest path.
Depth of the root node = 0
The terms “level” and “depth” are used interchangeably.
Example-
Here,
Depth of node A = 0
Depth of node B = 1
Depth of node C = 1
Depth of node D = 2
Depth of node E = 2
Depth of node F = 2
Depth of node G = 2
Depth of node H = 2
Depth of node I = 3
Depth of node J = 3
Depth of node K = 3
12. Subtree-
Example-
Binary Tree:- - Binary tree is a data structure in which each node can have at most two
children.
Binary Search Tree:- A Binary Search Tree (BST) is a tree in which all the nodes follow
the below-mentioned properties:
All nodes of left subtree are less than or equal to it’s root node.
All nodes of right subtree are greater than to it’s root node.
The left and right subtree each must also be a binary search tree.
Complete Binary Tree:- A complete binary tree is a binary tree that satisfies the
following 2 properties-
Every internal node has exactly 2 children.
All the leaf nodes are at the same level.
Example-
Here,
First binary tree is not a complete binary tree.
This is because all the leaf nodes are not at the same level.
Almost Complete Binary Tree:- An almost complete binary tree is a binary tree that
satisfies the following 2 properties-
All the levels are completely filled except possibly the last level.
The last level must be strictly filled from left to right.
Example-
Here,
First binary tree is not an almost complete binary tree.
This is because the last level is not filled from left to right.
Full Binary Tree:- A binary tree is full if every node has 0 or 2 children. Full binary tree
is also called as Strictly binary tree.
AVL Tree:- AVL tree is a self-balancing Binary Search Tree (BST) where the difference
between heights of left and right subtrees cannot be more than one for all nodes.
Spanning Tree:- A spanning tree is a subset of Graph G, which has all the vertices
covered with minimum possible number of edges. Hence, a spanning tree does not have
cycles and it cannot be disconnected.
We found three spanning trees off one complete graph. A complete undirected graph
can have maximum nn-2 number of spanning trees, where n is the number of nodes. In
the above addressed example, n is 3, hence 3 3−2 = 3 spanning trees are possible.
Minimum Spanning Tree (MST):- A minimum spanning tree (MST) or minimum weight
spanning tree for a weighted, connected and undirected graph is a spanning tree with
weight less than or equal to the weight of every other spanning tree.
Skewed Binary Tree:- A skewed binary tree is a binary tree that satisfies the following
2 properties-
All the nodes except one node has one and only one child.
The remaining node has no child.
Example-
In - Order Traversal
Pre - Order Traversal
Post - Order Traversal
Heap:- A Heap is a special tree-based data structure in which the tree is a complete
binary tree. Heap is a binary tree with special characteristics. A heap data structure
some times also called as Binary Heap.
There are two types of heap data structures and they are as follows...
Max Heap
Min Heap
Max Heap:- Where the value of the root node is greater than or equal to either of its
children.
Min Heap:- Where the value of the root node is less than or equal to either of its
children.
Child node ≥ Root node
Consider the following list of unsorted numbers which are to be sort using Heap
Sort 45, 28, 52, 25, 60, 70
Build a Binary Search Tree from a preorder sequence { 15, 10, 8, 12, 20, 16, 25 }.
Solution:
Build a Binary Search Tree from a postorder sequence {8, 12, 10, 16, 25, 20, 15}
Solution:
Postorder(left, right, root ): 8, 12, 10, 16, 25, 20, 15
Inorder( left, root, right ): 8, 10, 12, 15, 16, 20, 25
Hashing
Hashing:- Hashing is a technique to convert a range of key values into a range of indexes
of an array.
Hash function is a function which takes a piece of data (i.e. key) as input and produces
an integer (i.e. hash value) as output which maps the data to a particular index in the
hash table.
Problem:- Using the hash function ‘key mod 7’, insert the following sequence of keys in
the hash table 50, 700, 76, 85, 92, 73 and 101. Use separate chaining technique for
collision resolution.
Solution:
Solution:
Double hashing:- Double hashing is similar to linear probing and the only difference is
the interval between successive probes. Here, the interval between probes is computed
by using two hash functions.
Quadratic Probing- In quadratic probing,
Problem:- Let us consider a simple hash function as “key mod 7” and sequence of keys
as 50, 700, 76, 85, 92, 73, 101.
Solution:
Quadratic
Criteria Linear Probing Double Hashing
Probing
Secondary
Yes Yes No
Clustering
Number of Probe
Sequence M m m2
(m = size of table)
Convert the given binary tree to binary search tree(BST) without changing the spatial
structure of given binary tree. For example, in the below diagram tree on the left
hand side is converted to BST on right hand side while maintaining the original spatial
structure of binary tree.
Answer:
Inorder 3 6 8 1 4 0 5 7 2
Sorted 0 1 2 3 4 5 6 7 8
inorder