Trees Flashcards

1
Q

How would you define a tree informally? How can it be implemented in programming?

A

An abstract data type for hierarchical storage of information.
In programming we can set a pointer at root and then move to an arbitrary element as they are all connected

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

What is a rule about elements, that root and the ends (leafs) of the branches don’t follow?

A

Each element has a parent and zero or more children

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

Can you think of some examples of trees?

A

Linux directory structure

Family tree

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

Do you remember the formal definition of a tree?

A

A tree T is a non-empty set of nodes storing useful information in a parent-child relationship with the following properties:

  • T has a special node r referred to as the root
  • Each node v of T different from r has a parent node u
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What do we call external and internal nodes?

A

External -> leaf node -> 0 children

Internal -> 1 or more children

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

What about the formal definition of a sub-tree (rooted at node v)?

A

A tree consisting of all the descendants of v in T including v itself

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

How would you describe an ordered tree?

A

A tree where a linear ordering relation is defined for the children of each node, i.e we can see an order in each level.

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

How would you describe an ordered binary tree? When is it proper?

A

It is an ordered tree where each node has at most two children. The tree is proper if each internal node has two children (these are labelled left and right child and left comes before right)

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

What is an application of a binary tree?

A

Arithmetic expression evaluation. Easily apply a traversal algorithm on a binary tree where leaves are numbers and internals are operations.

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

More on programming implementation of trees, how can we access the abstract tree data type?

A

Using Accesor methods , root(),
parent(v), returns pointer parent node of v
and children(v), returns iterator for children which may
be empty

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

What are more methods that we need on the abstract tree datatype other than the accessors?

A

Query: isInternal, isExternal, isRoot
Generic: size, elements (i.e iterator of elements at nodes), positions (i.e iterator of all nodes), swapElements(v,w), replaceElement(v,e)

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

What at the complexities of tree ADT methods?

A

NOT ADDED YET

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

There’s another method we could define to return the depth of a tree T. What is depth of v? What type of algorithm is used to calculate this?

A

It is the number of ancestors of v, excluding itself.

The depth algorithm is recursive: base case, root depth is 0, step case depth of v is one plus depth of parent.

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

There’s another method we could define to return the height of a tree T. What is height of T? What type of algorithm is used to calculate height of v in T?

A

height of T = the maximum depth of an external node of T. A recursive algorithm: base case, if v is external height is 0, else it is 1 _ max height of child of a child of v

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

What is tree traversal best described as?

A

A systematic way of accessing all the nodes of T

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

What are the two different traversal schemes?

A

Preorder: root visited (and action performed on it )first then subtrees at children visited recursively. action BEFORE calling on subtree
Post order: action after calling on subtree

17
Q

When is preorder traversal useful? What is it’s time complexity?

A

Produce linear ordering of the nodes in a tree where parents are always before their children.e.g for book example. Time complexity is O(n)

18
Q

When is postorder traversal useful?

A

Calculating arithmetic expression, finding size of directory

19
Q

What additional accessor methods do binary trees support? What extra annotation can we use on the nodes?

A

Leftchild(),RightChild(),sibling()

We can now use levels to denote all the nodes of a binary tree at the same depth d as the level d of T

20
Q

If m the number of nodes on level c and n the number of nodes on level d how many times m is n ? (n = X*m)

A

X = 2

21
Q

What is in-order traversal? What trees can it be performed on?

A

In order traversal is like a combination of post and pre

Can be performed on binary trees.

22
Q

What data structure could we use to represent a binary tree?

A

A vector bases structure: Based on simple way of numbering the nodes of T (level numbering function p(v)).

23
Q

What are 2 disadvantages of storing a binary tree in an array, based on a numbering function?

A

If the tree is not proper there will be blank spaces in the array.

May not have the best performance.

24
Q

What is a more space efficient way to to store a binary tree?

A

Linked structure. Nodes represented by an object that references element v, and the positions associated with parent and children.

25
Q

How could we use a linked data structure to store general trees (more tan 2 children)?

A

We would have to add an array between a parent node and it’s children to hole the pointers to the children

26
Q

What abstract data type is the heap data structure based on?

A

Priority queue

27
Q

what is the heap order property?

A

Key of child is always greater than or equal to key of parent

28
Q

what are the parts of a heap?

A
  1. the binary tree itself
  2. a reference to the (peak) node
  3. a comparator
29
Q

What are the two properties we have to maintain when operating on trees?

A
  1. Completeness

2. Heap Order Property

30
Q

What do we call an item in a dictionary?

A

the (key,element) pair