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

Data Structures Midterms

The document provides an overview of tree data structures, focusing on binary trees and their properties, including definitions of key terms such as root, leaf, and subtree. It explains various types of binary trees, traversal methods, and basic operations like insertion and searching, as well as the concept of heaps and their operations. Additionally, it covers the time complexity of operations in heaps, emphasizing efficiency in data management.

Uploaded by

Mark Nazaria
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)
8 views4 pages

Data Structures Midterms

The document provides an overview of tree data structures, focusing on binary trees and their properties, including definitions of key terms such as root, leaf, and subtree. It explains various types of binary trees, traversal methods, and basic operations like insertion and searching, as well as the concept of heaps and their operations. Additionally, it covers the time complexity of operations in heaps, emphasizing efficiency in data management.

Uploaded by

Mark Nazaria
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

TREE

à Tree represents the nodes connected by


edges.

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.

Three ways which we use to traverse a tree:


1. In-order Traversal
2. Pre-order Traversal
3. Post-order Traversal
Tree Node
The Code to write a tree node would be similar Generally, we traverse a tree to search or locate
to what is given below. It has a data part and a given item or key in the tree or to print all the
references to its left and right child nodes. values it contains.
Struct node{ In-Order Traversal
Int data;
struct node *leftChild; à In this traversal method, the left subtree
struct node *rightChild; is visited first, then the root and later the
}; right sub-tree.
à We should always remember that every
in a tree, all nodes share common construct node may represent a subtree itself.

BST Basic Operations

Insert
à Inserts an element in a tree/ creates tree.

Search
à Searches an element in a tree.

Preorder Traversal D => B => E => A => F => C => G


à Traverse tree in a pre-order manner Algorithm
1. Recursively traverse left subtree.
In-order Traversals 2. Visit the Root Node.
à Traverses a tree in an in-order manner. 3. Recursively traverse right subtree.

Post-order Traversals Pre-Order Traversal


à Traverses a tree in a post-order manner.
à In this traversal method, the root node is Note: A max-heap has the same definition
visited first, then the left subtree and except that the Key of every node is >= the keys
finally the right subtree of the children

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.

Inserting into a minheap


à Suppose you want to insert a new value x
in the heap
à Create a new node at the “end” of the
heap (or put x at the end of the array)
à If x is >= its parent, done.
à Otherwise, we have to restore the heap:
- Repeatedly swap x with its parent
until either x reaches the root of x
becomes >= its parents.

You might also like