Data Structures Midterms
Data Structures Midterms
Binary Tree
à Special data structure used for data
storage purposes.
à A binary tree has a special condition that
each node can have a maximum of two
children.
à A binary tree has the benefits of both an DEFINITION OF TERMS
ordered array and a linked list as search
is as quick as in a sorted array and Path
insertion or deletion operation are as fast à Path refers to the sequence of nodes
as in linked list. along the edges of a tree.
Root
à The node at the top of the tree.
à There is only one root per tree and one
path from the root node to any node.
Parent
à Any node except the root node has one
edge upward to a node called parent
Full Binary Tree
Child
à A binary tree where all the leaves are on
à The node below a given node connected
the same level and every non-leaf has
by its edge downward.
two children
Leaf
à The node which does not have any child
Subtree
à Represents the descendants of a node.
Non-Full Binary Tree
à A parent node has only one child Visiting
à Checking the value of a node when
control is on the node.
Traversing
à Means passing through nodes in a
Canonical Labeling of Full Binary Trees specific order
à Label the nodes from 1 to n from the top
to the bottom, left to right. Levels
à Represents the generation of a node.
à If the root node is at level 0, then its next
child node is at level 1, its grandchild is
at level 2, and so on.
Keys Insert Operation
à Represents a value of a node based on The very first insertion creates the tree.
which a search operation is to be carried Afterwards, whenever an element is to be
out for a node. inserted, first locate its proper location. Start
searching from the root node, then if the data is
Binary Search Tree Representation less than the key value, search for the empty
Binary Search Tree exhibits a special behavior. A location in the left subtree and insert the data.
node’s left child must have a value less than its Otherwise, search for the empty location in the
parent’s value and the node’s right child must right subtree and insert the data.
have a value greater than its parent’s value.
Tree Traversal
Traversal is a process to visit all the nodes of a
tree and may print their values too. Because all
nodes are connected via edges, we always start
from the root node. That is, we cannot randomly
access a node in a tree.
Insert
à Inserts an element in a tree/ creates tree.
Search
à Searches an element in a tree.
Operations on Heaps
à Delete the minimum value and return it.
This operation is called deleteMin.
à Insert a new data value
DeleteMin in Min-heaps
à The minimum value in a min-heap is at
the root.
A => B => D => E => C => F => G à To delete the min, you just can’t remove
the data value of the root, because every
Algorithm node must hold a key.
1. Visit the Root Node. à Instead, take the last node from the
2. Recursively traverse left subtree. heap, move its key to the root, and delete
3. Recursively traverse right subtree. the last node.
à But now, the tree is no longer a heap (still
Post-Order Traversal almost complete, but the root key value
à In this traversal method, the root node is may no longer be < the keys of its
visited last, hence the name. children)
à First we traverse the left subtree, then
the right subtree and finally the root
node.
Restore Heap
D => E => B => F => G => C => A
à Swap the new root key with the smaller
Algorithm child.
1. Recursively traverse left subtree. à Now the potential bug is at one level
2. Recursively traverse left subtree. down. If it is not already < the keys of its
3. Visit the Root Node. children, swap it with its smaller child.
à Keep repeating the last step until the
APPLICATION OF ALMOST COMPLETE BINARY “bug” key becomes < its children, or the
TREES: HEAPS it becomes a leaf.
Heap
à A heap (or min-heap to be precise) is an
almost complete binary tree where:
1. Every node holds a data value (or
key)
2. The key of every node is < the keys
of the children
Time complexity of insert and deletemin
à Both operations takes time proportional
to the height of the tree
§ When storing the heap, the bug
moves from level to level until, in the
worst case, it becomes a leaf (in
deletemin) or the root (in insert)
§ Each move to a new level takes
constant time.
§ Therefore, the time is proportional to
the number of levels, which is the
height of the tree.
à But the height is 0(log n)
à Therefore, both insert and deletemin
take 0(log n) time, which is very fast.