Week 3 - Tree Structure Flashcards

1
Q

How is insertion done efficiently in a tree structure (e.g., Binary Search Tree)?

A

To insert an element:

  1. Start from the root.
  2. Recursively compare the new value with the current node.
  3. Move left if smaller, right if larger.
  4. Insert the new node at the appropriate null (leaf) position.

Time Complexity: O(log n) for a balanced tree, O(n) in the worst case (unbalanced tree).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is deletion handled efficiently in a Binary Search Tree?

A

To delete a node:

  1. Find the node to be deleted.
  2. Three cases:
  • No children: Simply remove the node.
  • One child: Replace the node with its child.
  • Two children: Replace the node with its in-order successor or predecessor, then delete the successor/predecessor.

Time Complexity: O(log n) for balanced trees, O(n) in the worst case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Tree in data structures?

A

A Tree is a non-linear data structure that simulates a hierarchical structure with a set of connected nodes. It has:

A single root node.

No loops (i.e., it’s acyclic).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the basic components (terminologies) of a tree?

A

Nodes: Individual elements of the tree.

Edges: Connections between nodes.

Leaves: Nodes with no children.

Height/Depth: The longest path from the root to any leaf node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the root in a tree data structure?

A

The root is the topmost node in a tree. It is the starting point from which all other nodes branch out.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the height of a tree?

A

The height of a tree is the number of edges on the longest path from the root to any leaf node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Binary Tree?

A

A Binary Tree is a tree data structure where each node has at most two children, commonly referred to as the **left

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a Binary Search Tree (BST)?

A

A Binary Search Tree is a binary tree where:

The left child of a node contains only nodes with values less than the node’s value.

The right child contains only nodes with values greater than the node’s value.

This ordering property makes search operations efficient.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a Balanced Tree?

A

A Balanced Tree is a binary tree in which the height difference between the left and right subtrees of any node is no more than one. This ensures performance remains optimal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why is balancing important in binary search trees?

A

Balancing avoids skewed structures (like linked lists), maintaining O(log n) efficiency for operations like search, insert, and delete.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens when a Binary Search Tree is not balanced?

A

If a BST becomes unbalanced (e.g., all nodes inserted in sorted order), it can degrade to a linked list, causing operations to take O(n) time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the representation of an empty binary tree?

A

An empty tree is represented as either:

Empty or a blank tree ()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you build a binary tree using the expression Fork(X, L, R)?

A

You create a binary tree with:

X as the current node (label)

L as the left subtree

R as the right subtree

Example: Fork(3, Empty, Empty) represents a single-node tree with value 3.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the only two components needed to build all binary trees?

A

All binary trees can be built using only:

Empty (to represent no node)

Fork (to combine nodes and subtrees)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly