Definition: non-cyclic connected graph
Wait... what is cyclic? what is connect? graph?
Definition: non-cyclic connected graph
Wait... what is cyclic? what is connect? graph?
Cyclic
Connected
non-cyclic
Connected
non-cyclic
not connected
cyclic and not connected is possible
0
1
2
3
height = 3
height = 2
height = 4
distance to the farest leaf
Each node can be root
Each node can be root
2. Given any pair of nodes, there exists a unique path that doesn't visit a node twice.
2. Given any pair of nodes, there exists a unique path that doesn't visit a node twice.
2. Given any pair of nodes, there exists a unique path that doesn't visit a node twice.
2. Given any pair of nodes, there exists a unique path that doesn't visit a node twice.
3. A tree have N nodes has exactly (N-1) edges
How to store a tree structure
Node is like a linked list !
Question : how many pointers needed?
struct Node{
int _data;
Node *_child1, *_child2, *_child3, ...?
(Node*)* array;
int index;
}
void addNode(Node * parent, int val){
if(index == N)
array = new Node*[2*N];
array[index] = new Node(val);
index++;
}
Node::Node(){
malloc(sizeof(Node*) * N);
}Solution : use linked list or dynamic array (STL vector)
#include <vector>
struct Node{
int _data;
vector<Node*> _childs;
}Another Solution : Use array of dynamic array (STL vector)
#include <vector>
using namespace std;
int data[SIZE];
vector<int> child[SIZE];
void addChild(int parent, int child){
child[parent].push_back(child);
}Use when the Probelm gives the index of each node.
Depth First Search (DFS)
DFS
DFS
DFS
DFS
DFS
DFS
DFS
DFS
DFS
DFS
1 2 4 7 5 3 6 8 9 10
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
Breadth First Search (BFS)
#include <vector>
#include <iostream>
using namespace std;
int data[MAXN];
vector<int> child[MAXN];
void DFS_recursive(int root){
cout << data[root] << " ";
for(int i = 0; i < child[root].length(); i++){
DFS_recur(child[root][i]);
}
}
void DFS_iterative(int root){
stack<int> tovisited;
tovisited.push(root);
while(!tovisited.emtpy())
{
int cur = tovisited.top();
cout << cur << " ";
tovisited.pop();
for(int i = 0; i < child[cur].length(); i++){
tovisited.push(child[cur][i]);
}
}
}
void BFS(int root){
queue<int> tovisited;
tovisited.push(root);
while(tovisited.empty())
{
int cur = tovisited.front();
cout << cur << " ";
tovisited.pop();
for(int i = 0; i < child[cur].length(); i++){
tovisited.push(child[cur][i]);
}
}
}how to traversal ?
pre/ in/ post order
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|
Why storing a normal binary tree in array is not good ?
Definition :
Search 8?
Search 8?
Search 8?
Search 8?
Search 8?
9, 2, 5 ,13, 11, 17, 10, 4, 14, 7
9, 2, 5 ,13, 11, 17, 10, 4, 14, 7
9, 2, 5 ,13, 11, 17, 10, 4, 14, 7
9, 2, 5 ,13, 11, 17, 10, 4, 14, 7
9, 2, 5 ,13, 11, 17, 10, 4, 14, 7
9, 2, 5 ,13, 11, 17, 10, 4, 14, 7
9, 2, 5 ,13, 11, 17, 10, 4, 14, 7
9, 2, 5 ,13, 11, 17, 10, 4, 14, 7
9, 2, 5 ,13, 11, 17, 10, 4, 14, 7
9, 2, 5 ,13, 11, 17, 10, 4, 14, 7
delete 13 ?
delete 13 ?
?
Insert in what order ?
depth -> log N N