Ch 4: Trees and Graphs Flashcards
Trees:
Basic Types of Trees
- Tree
- N-ary Tree
- Binary Tree
- Binary Search Tree
- Self Balancing Binary Search Tree
- Red-Black Trees
- AVL Trees
- Self Balancing Binary Search Tree
- Binary Heaps
- Min Heap
- Max Heap
- Binary Search Tree
- Tries (Prefix Trees)
Graphs:
Types of graphs and related data structures
- Directed Graph
- Undirected Graph
- Cyclic Graph
- Acyclic Graph
Data Structures:
* Adjacency List
* Adjacency Matrix
Trees:
Tree Basic Description
- A data structure composed of Nodes which can contain any data type as values
- It is a type of Graph
- Each tree has a Root Node
- The Root Node has 0 or more Child Nodes
- Each Child Node has 0 or more Child Nodes
- Each Node may or may not have a pointer to it’s parent node
- The tree cannot contain Cycles
- Nodes may or may not be in a particular order
Trees:
Leaf Node
A node in a tree that has no children
Trees:
Root Node
The top-most node in a tree.
Trees:
Basic Tree Class Definition
class Tree { class Node { public String name; public Node[] children; } public Node root; }
Trees:
Binary Tree basic description
A tree in which each node has up to 2 children
Trees:
N-ary Tree basic description
A tree in which each node has up to N children
Trees:
Binary Search Tree basic description
A Binary Tree in which every node fits a specific ordering property.
Some definitions allow duplicate values, some do not.
example:
For each node with value n,
All left descendants <= n
and
All right descendants > n
Trees:
Binary Tree Classifications
- Balanced or Unbalanced
- Completeness
- Full
- Perfect
Trees:
Binary Tree Properties:
Balanced
A tree is balanced when the left and right subtrees are very close to the same size.
* Don’t have to be exactly the same size
* Should be close enough that insert and find operations are O(logn)
* Not necessarily as balanced as it could be
Trees:
Common Types of Balanced Trees
- Red-Black Trees
- AVL Trees
Trees:
Binary Tree Properties:
Completeness
A Binary Tree is Complete when:
* Every level of the tree is full, except the bottom
* Last level can be incomplete, but must be filled from left to right
Trees:
Binary Tree Properties:
Fullness
A Binary Tree is Full when:
* Each Node has either 0 or 2 Children
Trees:
Binary Tree Properties:
Perfect Binary Tree Properties
A Perfect Binary Tree
is both Complete, and Full.
This is rare, as it can only exist when the tree contains exactly
2^k - 1 nodes,
where k = number of levels in the tree
Trees:
Binary Tree Traversal methods
- In-Order Traversal
- Pre-Order Traversal
- Post-Order Traversal
Trees:
Binary Tree
In-Order Traversal
Each node of the tree is visited recursively, from left to right
Visitation Order:
Left subtree -> Current Node -> Right Subtree
In a Binary Search Tree, where the nodes are organized in ascending order, the nodes are visited in ascending order
Trees:
Binary Tree
In-Order Traversal
pseudocode
void inOrderTraversal( TreeNode node) {
if (node != null) {
inOrderTraversal(node.left); // left child
visit(node); // current
inOrderTraversal(node.right); // right child
}
}
Trees:
Binary Tree
Pre-order Traversal
Each node of the tree is visited recursively
Current Node visited BEFORE it’s children.
Visitation Order:
Current Node -> Left Subtree -> Right Subtree
The Root Node is always the first node visited.
Trees:
Binary Tree
Pre-Order Traversal Pseudocode
void preOrderTraversal( TreeNode node) {
if (node != null) {
visit(node); // current
preOrderTraversal(node.left); // left child
preOrderTraversal(node.right); // right child
}
}
Trees:
Binary Tree
Post-Order Traversal
Each node is visited recursively,
Current Node is visited AFTER it’s children
Visitation Order:
Left Subtree -> Right Subtree -> Current Node
The Root Node is always the last node visited