ITCC 104 - Data Structures & Algorithms: Lesson 1: Trees Tree
ITCC 104 - Data Structures & Algorithms: Lesson 1: Trees Tree
Tree Terminologies
Here are some of the most common and
important terms:
a) Node: Any object or value stored in the tree
represents a node. In the preceding figure, The two diagrams following show the two types
the root and all of its children and of tree:
descendants are independent nodes.
b) Root: The base node of the tree. Ironically,
this node is typically depicted at the top of a
graphic representation of the tree. Note that
a root node, even if it has zero descendants,
represents an entire tree by itself.
c) Parent: Any node which contains 1...n child
nodes. The parent is only the parent in
respect to one of its children. Also note that
any parent node can have 0...n children
depending on the rules associated with the
tree's structure.
d) Child: Any node other than the root node is Common Operations
a child to one (and only one) other node. Many of the common operations associated with
The root node of any tree that is not a sub- trees can be defined in terms of a single node, or
tree of another structure is the only node that from the perspective of the same. Here is a list of
is not itself a child. the most common operations associated with trees:
e) Siblings: Siblings, also referred to as
children, represent the collection of all of Data: Associated with a single node, and
the child nodes to one particular parent. For returns the object or value contained in that
example, referring to the preceding figure, node.
the collection of two nodes below the root Children: Returns the collection of siblings
represents siblings. associated with this parent node.
f) Leaf: Any node that has no child nodes Parent: Some tree structures provide a
g) Edge: The route, or reference, between a mechanism to "climb" the tree, or traverse
parent and child node. the structure from any particular node back
toward the root.
Enumerate: Will return a list or some other
collection containing every descendant of a
particular node, including the root node
itself.
Insert: Allows a new node to be added as a
child of an existing node in the tree. Can
be somewhat complicated when the tree
structure has a limit to the number of
children that can be associated with a a) 2k -1: maximum number of nodes in a
particular parent. binary tree of height k
o When the maximum number of b) Skewed binary tree: A tree that either have
children permitted is already in 0 left subtrees or 0 right subtrees
place, one of those children must be c) Full binary tree of depth k: a binary tree of
relocated as a child of the new node depth kl with 2k-1 nodes
being inserted
Graft: A similar operation to insert, except
that the node being inserted has Tree Representations
descendants of its own, meaning it is a
multi-layer tree. Can be somewhat Using Arrays- static /sequential representation
complicated when the tree structure has a
limit to the number of children that can be
associated with a particular parent.
o When the maximum number of
children permitted is already in
place, one of those children must be
logically relocated as a child of a
leaf of the new tree being inserted. Using Linked list
Delete: Will remove a specified node from
the tree. If the node being deleted has
descendants, those nodes must be relocated
to the deleted node's parent in some
fashion, otherwise the operation is classified
as a prune.
Prune: Will remove a node and all of its
descendants from a tree. Traversing Binary Trees
Traversing a tree means visiting each node
in a specified order. This process is not as
Binary Trees commonly used as finding, inserting, and
If every node in a tree can have at most two deleting nodes.
children, the tree is called a binary tree. One reason for this is that traversal is not
particularly fast. But traversing a tree has
Binary tree some surprisingly useful applications and
is theoretically interesting.
a finite set of nodes which is either empty
There are three simple ways to traverse a
or consisting of a root with at most two
tree. They’re called preorder, inorder, and
branches to disjoint binary trees called the
postorder
left subtree and the right subtree
Inorder Traversal
An inorder traversal of a binary search tree
will cause all the nodes to be visited in
ascending order, based on their key values.
If you want to create a sorted list of the data
in a binary tree, this is one way to do it:
o Call itself to traverse the node’s left
subtree.
o Visit the node.
o Call itself to traverse the node’s right Like inOrder(B), inOrder(C) has no
subtree children, so task 1 returns with no action,
task 2 visits C, and task 3 returns with no
Traversing a 3-Node Tree action.
- Let’s look at a simple example to get an idea of 10. inOrder(B) now returns to inOrder(A).
how this recursive traversal routine works. Imagine 11. However, inOrder(A) is now done, so it
traversing a tree with only three nodes: a root (A), returns and the entire traversal is complete.
with a left child (B), and a right child (C), as
shown in the figure below:
Preorder and Postorder Traversals
A binary tree (not a binary search tree) can
be used to represent an algebraic
expression that involves the binary
arithmetic operators +, -, /, and *.
The root node holds an operator, and the
other nodes represent either a variable
name (like A, B, or C), or another operator.
Each subtree is an algebraic expression.
Deletion
1. Delete the node.
Binary Search Tree 2. If the deleted node is a non-terminal
node, the immediate predecessor
A node’s left child must have a key less (immediate successor) replaces the
than its parent, and a node’s right child deleted node.
must have a key greater than its parent. 3. Starting from the deepest level, take the
first node whose subtree heights differ
by more than one level as A.
4. Take the right child of A as B if it has
more descendants than the left child;
otherwise, take the left child of A as B.
5. Take the right child of B as C if it has
more descendants than the left child;
otherwise, take the left child of B as C. If
equal prioritize LL over LR or RR over
RL.
6. Take the inorder traversal of A, B, and C.
7. Take the middle node as the parent of the
All identifiers in the left subtree are less
two other nodes.
than the identifier in the root
8. Adjust other nodes.
All identifiers in the right subtree are
greater than the identifier in the root
The left and right subtrees are also binary
search trees Lesson 2: Graphs
Part 1: Visual Graph Concepts
AVL Trees Graph
Introduced by Adelson-Velskii and Landis a pair (V, E), where V is a set of nodes,
in 1962 called vertices, and E is a collection of
Height-balanced binary search trees pairs of vertices, called edges. Vertices and
Binary search trees in which the heights of edges are positions and store elements.
the subtrees of each node differ by at most
one level. Directed edge
Insertion of deletion of nodes causes the ordered pair of vertices (u, v)
search tree to be rebalanced. first vertex u is the origin
second vertex v is the destination
Insertion
Example: one-way road traffic
1. Insert the node at the proper point.
2. Starting from the point of insertion, take the
first node whose subtree heights differ by
more than one level as A.
3. If the new node is inserted in the left subtree Undirected edge
of A, take the left child of A as B; if inserted unordered pair of vertices (u, v)
in the right subtree of A, take the right child Example: railway lines
of A as B.
4. If the new node is inserted in the left subtree
of B, take the left child of B as C; if inserted
in the right subtree of B, take the right child
of B as C.
Directed graph A graph is connected if there is a path from
every vertex to every other vertex. If a
all the edges are directed graph is not connected then it consists of a
Example: route network set of connected components.
Adjacency Set
It is very much similar to adjacency list but
instead of using Linked lists, Disjoint Sets
Part 4: Graph Representation [UnionFind] are used.
Adjacency Matrix
In the matrix, each edge is represented by Part 5: Graph Traversals
two bits for undirected graphs. That means,
an edge from u to v is represented by 1 value Depth First Search [DFS]
in both Adj[u ,v ] and Adj[u,v]. To save DFS algorithm works in a manner similar
time, we can process only half of this to preorder traversal of the trees. Like
symmetric matrix. Also, we can assume that preorder traversal, internally this algorithm
there is an “edge” from each vertex to itself. also uses stack.
So, Adj[u, u] is set to 1 for all vertices. The intersections of the maze are the
If the graph is a directed graph, then we vertices and the paths between the
need to mark only one entry in the intersections are the edges of the graph. The
adjacency matrix. As an example, consider process of returning from the “dead end” is
the directed graph below. called backtracking. We are trying to go
away from the starting vertex into the graph
as deep as possible, until we have to
backtrack to the preceding grey vertex. In
DFS algorithm, we encounter the following
types of edges.
Applications of BFS
Finding all connected components in a
graph
Finding all nodes within one connected
component
Finding the shortest path between two nodes
Testing a graph for bipartiteness