Data structures binary search trees flashcards

This set of flashcards covers essential concepts and terminology related to binary search trees in computer science, focusing on their structure, operations, and applications.

NewtJames5·24 flashcards·24 questions
collegecomputer_sciencealgorithms
0
Known
1 / 24
0
Learning
Front

What is a binary search tree?

Tap to flip
Back

A binary search tree (BST) is a data structure that maintains sorted data. Each node has at most two children. Left child < Node < Right child.

Tap to flip
Got it
Still learning

Quiz(24 questions)

Question 1 of 24

1. What is the primary property of a binary search tree (BST)?

Terms in this Study Set(24)

Flashcards 1(12)

What is a binary search tree?

A binary search tree (BST) is a data structure that maintains sorted data. Each node has at most two children. Left child < Node < Right child.

True or False: BSTs can have duplicate values.

False. BSTs do not allow duplicate values to maintain unique paths.

How do you find a value in a BST?

Start at the root, compare the value. If less, go left; if more, go right. Repeat until found or null.

What is the time complexity for searching in a BST?

Average case: O(logn)\displaystyle O(log n), Worst case: O(n)\displaystyle O(n) when unbalanced.

Complete the sentence: The height of a balanced BST is...

...approximately log2(n)\displaystyle log_2(n), where n\displaystyle n is the number of nodes.

What are the benefits of using a BST?

- Fast search, insert, delete operations - Dynamic size - Ordered data access

Compare BST and arrays for searching.

BST: O(logn)\displaystyle O(log n) average search time. Array: O(n)\displaystyle O(n) average search time. BST requires more memory.

Fill in the blank: The in-order traversal of a BST outputs...

...the values in ascending order.

What is a leaf node in a BST?

A leaf node is a node with no children, meaning it is at the end of a branch.

True or False: All BSTs are height-balanced.

False. Some BSTs can become unbalanced, leading to poorer performance.

What is the purpose of rotations in BSTs?

To maintain balance during insertions and deletions, ensuring optimal search times.

Demonstrate inserting 5 into a BST with root 3:

1. Start at 3 (root). 2. 5 > 3, go right. 3. Right is null, insert 5 there.

Flashcards 2(12)

What is a balanced binary search tree?

A tree where the height is minimized to ensure efficient operations. Common types include AVL trees and Red-Black trees.

True or False: BSTs can have duplicate values.

False. In a standard binary search tree, each value must be unique to maintain order.

Insert 15, 10, 20 in order.

Resulting BST: 15 as root, 10 left of 15, and 20 right of 15.

What does the 'height' of a tree represent?

The number of edges in the longest path from the root to a leaf.

Fill in the blank: The time complexity for searching in a balanced BST is ____.

O(log n), where n is the number of nodes.

Comparison: BST vs. Array for search operations.

BST: O(log n) average time; Array: O(n) average time. BST is more efficient when data is dynamic.

What is the worst-case scenario for BST operations?

When the tree becomes skewed, resembling a linked list, leading to O(n) time complexity.

Cause → Effect: Unbalanced BST.

Causes inefficient operations, increasing time complexity to O(n) for search, insert, and delete.

What is a leaf node?

A node that has no children. It represents the end of a path in the binary search tree.

Delete node with one child: Example.

To delete node 10 with child 5: Replace 10 with 5. Tree remains valid.

True or False: All BSTs are binary trees.

True. Every binary search tree is a type of binary tree.

What are inorder traversal results?

The output of visiting nodes in ascending order. E.g. for 10, 15, 20: output is 10, 15, 20.

Questions in this Study Set(24)

1. What is the primary property of a binary search tree (BST)?

A.Each node has at most two children.
B.All nodes have the same value.
C.Each node must have three children.
D.Nodes can only contain positive values.

2. What is the primary purpose of a balanced binary search tree?

A.To ensure efficient operations by minimizing height
B.To allow duplicate values for easier data handling
C.To store data in a sorted array format
D.To eliminate the need for tree traversal

3. Which of the following is true about duplicate values in a BST?

A.They can be stored as multiple nodes.
B.They must be stored in the left subtree.
C.They are not allowed in a BST.
D.They can only be stored in the right subtree.

4. Which statement about duplicate values in a binary search tree is correct?

A.Duplicates are allowed if they are on different levels
B.Duplicates are not allowed in a standard BST
C.Duplicates can exist if they are equal to the root
D.Duplicates are allowed but must be stored as an array

5. If you are searching for a value in a BST and the value is less than the current node's value, what is the next step?

A.Go to the right child.
B.Go to the left child.
C.Return to the root.
D.Stop searching entirely.

6. If you insert 30, 20, and 40 in that order into a BST, what will the structure look like?

A.30 as root, 20 left, and 40 right
B.30 as root, 40 left, and 20 right
C.40 as root, 30 left, and 20 right
D.20 as root, 30 left, and 40 right

7. What is the worst-case time complexity for searching in an unbalanced BST?

A.O(log n)
B.O(n)
C.O(n log n)
D.O(1)

8. What does the height of a binary search tree indicate?

A.The number of nodes in the tree
B.The longest path from the root to a leaf node
C.The total number of edges in the tree
D.The number of leaf nodes present

9. How is the height of a balanced BST related to the number of nodes, n?

A.It is equal to n.
B.It is approximately log2(n).
C.It is double n.
D.It is n squared.

10. Fill in the blank: The time complexity for insertion in a balanced BST is ____.

A.O(1)
B.O(n)
C.O(log n)
D.O(n log n)

11. Which benefit is NOT associated with using a BST?

A.Ordered data access.
B.Dynamic size.
C.Fast hash table lookups.
D.Fast insert and delete operations.

12. When comparing search operations, which is the correct time complexity for a binary search tree versus an unsorted array?

A.BST: O(n), Array: O(log n)
B.BST: O(log n), Array: O(n)
C.BST: O(n log n), Array: O(n)
D.BST: O(log n), Array: O(log n)

13. When comparing average search times, how do BSTs and arrays differ?

A.Both have O(n) average time.
B.BSTs are faster with O(log n) and arrays are O(n).
C.Arrays are faster with O(log n) and BSTs are O(n).
D.Both structures require the same memory.

14. What is the worst-case scenario for operations in a binary search tree?

A.When the tree is perfectly balanced
B.When the tree is skewed to one side
C.When the tree has multiple duplicate values
D.When nodes are added randomly

15. In an in-order traversal of a BST, what is the resulting order of the node values?

A.Random order.
B.Descending order.
C.Ascending order.
D.Even order.

16. What effect does having an unbalanced BST have on operations?

A.Increases time complexity to O(log n)
B.Causes the tree to become a binary tree
C.Increases time complexity to O(n)
D.Allows for easier node deletion

17. What characterizes a leaf node in a binary search tree?

A.It has at least one child.
B.It has no children.
C.It is the root node.
D.It must have exactly two children.

18. Which of the following describes a leaf node in a binary search tree?

A.A node with at least one child
B.A node that can have multiple parents
C.A node that has no children
D.A node that contains a duplicate value

19. Which statement about height-balanced BSTs is false?

A.They optimize search times.
B.They must be perfectly balanced.
C.They can improve performance over unbalanced trees.
D.They can still have varying heights.

20. How do you delete a node with one child from a BST?

A.Simply remove the node from the tree
B.Replace the node with its child
C.Replace the node with its parent
D.Copy the subtree and then remove the node

21. What is the main purpose of performing rotations in a BST?

A.To sort the data.
B.To link nodes together.
C.To maintain tree balance during modifications.
D.To delete nodes.

22. True or False: All binary search trees are a type of binary tree.

A.True
B.False
C.Only if they are balanced
D.Only if they contain unique values

23. If you want to insert a value of 7 into a BST with a root of 4, where would you place it?

A.To the left of 4.
B.At the root.
C.To the right of 4.
D.It cannot be inserted.

24. What is the result of an inorder traversal of a BST?

A.Nodes in descending order
B.Nodes in random order
C.Nodes in ascending order
D.Nodes only for the left subtree

Related Study Sets

Create Your Own Study Set

Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.