Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Buet Admission About Tree

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Tree - Terminology

Tree is a non-linear data structure which organizes data in a hierarchical structure


and this is a recursive definition.

The important terms related to tree data structure are-


1. Root-

 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-

Here, node A is the only root node.

2. Edge-

 The connecting link between any two nodes is called as an edge.


 In a tree with n number of nodes, there are exactly (n-1) number of edges.

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-

 The node which is a descendant of some node is called as a child node.


 All the nodes except root node are child nodes.

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-

 Nodes which belong to the same parent are called as siblings.


 In other words, nodes with the same parent are sibling nodes.

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-

 Degree of a node is the total number of children of that node.


 Degree of a tree is the highest degree of a node among all the nodes in the
tree.

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-

Here, nodes A, B, C, E and G are internal nodes.

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-

Here, nodes D, I, J, F, K and H are leaf nodes.


9. Level-

 In a tree, each step from top to bottom is called as level of a tree.


 The level count starts with 0 and increments by 1 at each level or step.

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-

 In a tree, each child from a node forms a subtree recursively.


 Every child node forms a subtree on its parent node.

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.

Complete binary tree is also called as Perfect binary tree.

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.

Application of Spanning Tree:

 Civil Network Planning


 Computer Network Routing Protocol
 Cluster Analysis

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-

Binary Tree Traversals


There are three types of binary tree traversals.

 In - Order Traversal
 Pre - Order Traversal
 Post - Order Traversal

In-order (Left, Root, Right) : 4 2 5 1 3


Pre-order (Root, Left, Right) : 1 2 4 5 3
Post-order (Left, Right, Root) : 4 5 2 3 1
 Create the binary search tree using the following data elements 43, 10, 79, 90, 12,
54, 11, 9, 50.
 Draw a complete binary tree of height 4. What is the height of a complete binary
tree with N nodes?

Height of a complete binary tree with N is: ceil( log2(N+1) ) – 1


Heap Data structure

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.

Root node ≥ Child node

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:

Preorder( root, left, right ): 15, 10, 8, 12, 20, 16, 25


Inorder( left, root, right ): 8, 10, 12, 15, 16, 20, 25

 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.

Collision in Hashing:- A collision occurs whenever a key is mapped to an address that is


already occupied. Collision resolution technique provides an alternate place in hash
table where this key can be placed.
Open Hashing:- Open hashing is a collision avoidence method which uses array of
linked list to resolve the collision. It is also known as the separate chaining method (each
linked list is considered as a chain).

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:

Open addressing:- Open addressing is a method for handling collisions. In open


addressing, all elements are stored in the hash table itself. So at any point, the size of
the table must be greater than or equal to the total number of keys.

Linear Probing:- In linear probing,

 When collision occurs, we linearly probe for the next bucket.


 We keep probing until an empty bucket is found.
Problem:- Let us consider a simple hash function as “key mod 7” and a sequence of keys
as 50, 700, 76, 85, 92, 73, 101.

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,

 When collision occurs, we probe for i2th bucket in ith iteration.


 We keep probing until an empty bucket is found.

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

Primary Clustering Yes No No

Secondary
Yes Yes No
Clustering

Number of Probe
Sequence M m m2
(m = size of table)

Cache Lies between the


Best Poor
performance two

Balanced Binary Tree


Difference between the left and the right subtree for any node is not more than one.
Binary Tree (Properties)

 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:

Step 1: Inorder traversal in the given tree


Step 2: Sorted the inoder traversal
Step 3: Draw the tree using sorted inorder traversal

Inorder 3 6 8 1 4 0 5 7 2

Sorted 0 1 2 3 4 5 6 7 8
inorder

You might also like