Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- Computer Organization
- Computer Science
- Data Engineer
- 빅데이터 커리어 가이드북
- HEAPS
- hash
- Restroom
- Newyork
- Linked List
- BST
- 데이터 엔지니어
- Heap
- dataStructure
- Data Structure
- data scientist
- Binary Tree
- Study
- data
- algorithm
- Preparing for the Google Cloud Professional Data Engineer Exam
- exam
- 화장실 지도
- 빅데이터
- priority queue
- binary search tree
- 뉴욕 화장실
- Data Analyst
- Algorithms
- 데이터 분석가
- 빅데이터 지식
Archives
- Today
- Total
Jaegool_'s log
IT 279: Algorithms and Data Structures, Week 3 Trees recap 본문
1. What are the height and depth of the J node in the tree above?
height = 1, Depth: 2
2. Which of the following is NOT a leaf node? D
3. Which of the following terms best describes the relationship of G to M? Aunt
HW 4. More Linked List Practice
void doUnion(const list<int>& list1, const list<int>& list2, list<int>& result)
{
list<int>::const_iterator iter1;
list<int>::const_iterator iter2;
// your code here -- make sure to read the comment on the prototype before beginning work
// Remember that you are expected to have an efficient solution: O(m+n), not O(m*n)
iter1 = list1.begin();
iter2 = list2.begin();
while(iter1 != list1.end() && iter2 != list2.end()){
if(*iter1 < *iter2){ // put smaller one first into the list
result.push_back(*iter1);
iter1++;
}
else if(*iter1 > *iter2){
result.push_back(*iter2);
iter2++;
} else{
result.push_back(*iter1);
iter1++;
iter2++;
}
}
while(iter1 != list1.end()){
result.push_back(*iter1);
iter1++;
}
while(iter2 != list2.end()){
result.push_back(*iter2);
iter2++;
}
}
void doIntersection(const list<int>& list1, const list<int>& list2, list<int>& result)
{
list<int>::const_iterator iter1;
list<int>::const_iterator iter2;
// your code here -- make sure to read the comment on the prototype before beginning work
// Remember that you are expected to have an efficient solution: O(m+n), not O(m*n)
// Note that an efficient answer to this is very closely related to an efficient answer to doUnion
iter1 = list1.begin();
iter2 = list2.begin();
while(iter1 != list1.end() && iter2 != list2.end()){
if(*iter1 < *iter2){
iter1++;
}
else if(*iter1 > *iter2){
iter2++;
}
else {
result.push_back(*iter1);
iter1++;
iter2++;
}
}
}
Tree cannot be empty - always has at least one node.
Order of child nodes does not matter.
A tree with N nodes has N-1 edges.
Binary Search Tree
The Dictionary ADT
- unique keys
- insert, remove, find
Sorted dictionary implementation
We always insert a leaf
Binary Tree Notes:
Each non-empty node has exactly two subtrees
Binary tree may be empty
The subtrees are ordered
1-based height: h has at least h and most 2^h-1 elements in it
The height of a binary tree that contains n, n>0, elements is at most n and at least ceiling of log2(n+1).
Full binary tree vs complete binary tree
Full Binary Tree (or Strictly Binary Tree):
- Every node in the tree is either a leaf node or has exactly two children.
- This means there are no nodes with only one child.
Complete Binary Tree:
- All levels, except possibly the last, are completely filled.
- The last level has all its nodes on the left side.
root is index 1.(index 0, typically size of the tree)
node i's children are at indexes 2i and 2i+1
- Inorder (Left, Root, Right) Traversal:
- Process:
- Traverse the left subtree.
- Visit the root.
- Traverse the right subtree.
- Usefulness:
- For binary search trees (BSTs), an inorder traversal retrieves data in sorted order.
- Used to convert a BST into a sorted linked list or array.
- Process:
- Preorder (Root, Left, Right) Traversal:
- Process:
- Visit the root.
- Traverse the left subtree.
- Traverse the right subtree.
- Usefulness:
- Often used to create a copy of the tree.
- Useful in prefix notation (Polish notation) of an expression.
- Can be used to get a serialized form of a binary tree.
- Process:
- Postorder (Left, Right, Root) Traversal:
- Process:
- Traverse the left subtree.
- Traverse the right subtree.
- Visit the root.
- Usefulness:
- Useful in postfix notation (Reverse Polish notation) of an expression.
- Helps in deleting the tree. Delete children before the parent.
- Used in algorithms to compute properties of nodes based on their descendants (like calculating the size of a subtree rooted at a node).
- Process:
- Level Order (or Breadth-First) Traversal:
- Process:
- Visit the root.
- Traverse nodes at depth 1.
- Traverse nodes at depth 2.
- And so on...
- Usefulness:
- Useful to get a serialized form of a binary tree that preserves the structure without null placeholders.
- Essential for algorithms that work level by level, like determining the minimum depth of a tree, or printing nodes at a given depth.
- Commonly implemented using a queue.
- Process:
// Inorder(LNR)
void inorder(TreeNode* root) {
if (root == nullptr) return;
inorder(root->left);
cout << root->val << " ";
inorder(root->right);
}
// Preorder(NLR)
void preorder(TreeNode* root) {
if (root == nullptr) return;
cout << root->val << " ";
preorder(root->left);
preorder(root->right);
}
// Postorder(LRN)
void postorder(TreeNode* root) {
if (root == nullptr) return;
postorder(root->left);
postorder(root->right);
cout << root->val << " ";
}
'Courses' 카테고리의 다른 글
IT 279: Algorithms and Data Structures, Week 5 Hash Tables (1) | 2023.10.05 |
---|---|
IT 279: Algorithms and Data Structures, Week 4 Search Trees (0) | 2023.10.04 |
IT 279: Algorithms and Data Structures, Week 2 recap (0) | 2023.10.03 |
IT 279: Algorithms and Data Structures, Week 1 recap (0) | 2023.10.02 |
IT 495 - Client meeting [Pixel Bit Road: funding platform for games] (0) | 2023.09.25 |