Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
33 views4 pages

Dsa Assignment4

The document defines a tree data structure and its key properties, including root nodes, leaf nodes, and traversal methods. It differentiates between various types of trees such as binary trees, binary search trees, and heap trees, explaining their unique characteristics and operations like insertion and deletion. Additionally, it covers concepts like full vs. complete binary trees and provides examples for better understanding.

Uploaded by

Shubham Chaubey
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)
33 views4 pages

Dsa Assignment4

The document defines a tree data structure and its key properties, including root nodes, leaf nodes, and traversal methods. It differentiates between various types of trees such as binary trees, binary search trees, and heap trees, explaining their unique characteristics and operations like insertion and deletion. Additionally, it covers concepts like full vs. complete binary trees and provides examples for better understanding.

Uploaded by

Shubham Chaubey
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/ 4

1. Define a tree data structure. List its key properties.

A tree is a hierarchical data structure consisting of nodes where each node contains a value and
references to its child nodes. Trees have a parent-child relationship, starting with a root node.

Key properties:

• Root node

• Parent and child nodes

• Edges

• Depth and height

• Leaf nodes

• Subtrees

• Degree of nodes

2. Differentiate between binary tree, binary search tree, and heap tree.

A binary tree is a hierarchical structure where each node has at most two children, referred to
as the left and right child. A binary search tree (BST), on the other hand, is a type of binary tree
where the left subtree contains nodes with smaller values than the parent node, and the right
subtree contains larger values. A heap tree is a specialized tree-based data structure satisfying
the heap property, where the parent node is either greater than (max-heap) or less than
(min-heap) its children.

3. Explain the following terms with examples:

1. Root node: The topmost node (e.g., in a tree A → B, C, A is root).

2. Leaf node: Nodes without children (e.g., B and C are leaves).

3. Internal node: Nodes with children (e.g., A is internal).

4. Degree: Number of children (e.g., degree of A is 2).

5. Depth: Edges from root to a node (e.g., depth of C is 1).

6. Height: Longest path to a leaf (e.g., height of root A is 1).

4. Full (strict) binary tree vs. complete binary tree:


• Full binary tree: Each node has either 0 or 2 children.

/\

B C

• Complete binary tree: All levels are filled except possibly the last, filled
left-to-right.

/\

B C

5. General tree vs. binary tree:

General trees can have any number of children per node, while binary trees restrict nodes to
have at most two children. General trees are more flexible but less efficient for specific tasks
compared to binary trees.

6. Tree traversal:

Tree traversal refers to visiting all nodes systematically. Types:

1. Inorder: Left → Root → Right.

2. Preorder: Root → Left → Right.

3. Postorder: Left → Right → Root.

4. Level-order: Visits nodes level by level.

7. Differentiate between traversals:

Traversal Type Visiting Order Example (Tree: A → B, C)

Inorder Left → Root → Right B, A, C

Preorder Root → Left → Right A, B, C


Postorder Left → Right → Root B, C, A

8. What is a binary search tree (BST)? List its properties.

A BST is a binary tree where the left subtree contains values smaller than the root, and the right
subtree contains values larger.

Properties:

1. Left < Root < Right.

2. Left and right subtrees are BSTs.

3. Inorder traversal results in sorted values.

9. Insertion and deletion in a BST:

• Insertion: Navigate left/right until a free position is found and insert.

• Deletion:

1. Node has no children → Delete it.

2. Node has one child → Replace it with the child.

3. Node has two children → Replace with inorder successor.

Example: Insert 20 in [10, 15, 25] → Result: [10, 15, 20, 25].

10. Heap tree: Max-heap vs. Min-heap:

A heap tree is a specialized binary tree satisfying the heap property, where parent nodes
dominate their children (max-heap) or are dominated (min-heap). Max-heaps store the largest
value at the root, while min-heaps store the smallest.

11. Heap tree properties vs. BST:

Heap trees are complete binary trees, with all levels filled except the last. The heap property
ensures efficient insertion, deletion, and access operations, differing from binary search trees,
which provide ordered traversal.

12. Construct max-heap [10, 20, 15, 30, 40, 50, 100]:

1. Insert 10 → [10]
2. Insert 20 → [20, 10]

3. Insert 15 → [20, 10, 15]

4. Insert 30 → [30, 20, 15, 10]

5. Insert 40 → [40, 30, 15, 10, 20]

6. Insert 50 → [50, 40, 15, 10, 20, 30]

7. Insert 100 → [100, 50, 40, 10, 20, 30, 15].

13. Min-heapify [3, 9, 2, 1, 4, 5]:

Start adjusting from the last non-leaf node:

• Final heap: [1, 3, 2, 9, 4, 5].

14. Insert 35 into max-heap [40, 30, 20, 15, 10, 8]:

1. Add 35 → [40, 30, 20, 15, 10, 8, 35].

2. Adjust upwards → [40, 35, 20, 15, 10, 8, 30].

You might also like