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
- 문자열
- 티스토리챌린지
- 파이썬
- baekjoon
- 오블완
- function
- 함수
- 반복문
- 포인터
- Class
- C++
- OOP
- Object Oriented Programming
- array
- 알고리즘
- Pre-processing
- vscode
- assignment operator
- 백준
- pass by reference
- string
- raw data
- Data Science
- Deep Learning
- const
- predictive analysis
- programming
- pointer
- 배열
- Python
Archives
- Today
- Total
Channi Studies
[Data Structure] BST Parent Node Pointers, and Recursion 본문
Data Science/Data Structure & Algorithm
[Data Structure] BST Parent Node Pointers, and Recursion
Chan Lee 2025. 4. 23. 05:25Parent Pointers
A BST implementation often includes a parent pointer inside each node.
A balnaced BST, such as an AVL tree or red-black tree, may utilize the parent pointer to traverse up the tree from a particular node to find a node's parent, grandparent, or siblings.
Balanced Binary Tree - GeeksforGeeks
Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.
www.geeksforgeeks.org
// BST Insert with nodes containing parent pointers pseudocode
BSTInsert(tree, node) {
if (tree⇢root == null) {
tree⇢root = node
node⇢parent = null
return
}
cur = tree⇢root
while (cur != null) {
if (node⇢key < cur⇢key) {
if (cur⇢left == null) {
cur⇢left = node
node⇢parent = cur
cur = null
}
else
cur = cur⇢left
}
else {
if (cur⇢right == null) {
cur⇢right = node
node⇢parent = cur
cur = null
}
else
cur = cur⇢right
}
}
}
// BST Replace Pseudocode
BSTReplaceChild(parent, currentChild, newChild) {
if (parent⇢left != currentChild &&
parent⇢right != currentChild)
return false
if (parent⇢left == currentChild)
parent⇢left = newChild
else
parent⇢right = newChild
if (newChild != null)
newChild⇢parent = parent
return true
}
// BST Remove Key and Remove Node Pseudocode
BSTRemoveKey(tree, key) {
node = BSTSearch(tree, key)
BSTRemoveNode(tree, node)
}
BSTRemoveNode(tree, node) {
if (node == null)
return
// Case 1: Internal node with 2 children
if (node⇢left != null && node⇢right != null) {
// Find successor
succNode = node⇢right
while (succNode⇢left)
succNode = succNode⇢left
// Copy value/data from succNode to node
node = Copy succNode
// Recursively remove succNode
BSTRemoveNode(tree, succNode)
}
// Case 2: Root node (with 1 or 0 children)
else if (node == tree⇢root) {
if (node⇢left != null)
tree⇢root = node⇢left
else
tree⇢root = node⇢right
// Make sure the new root, if non-null, has a null parent
if (tree⇢root != null)
tree⇢root⇢parent = null
}
// Case 3: Internal with left child only
else if (node⇢left != null)
BSTReplaceChild(node⇢parent, node, node⇢left)
// Case 4: Internal with right child only OR leaf
else
BSTReplaceChild(node⇢parent, node, node⇢right)
}
Notice that the case for a root node with 2 children is covered in Case 1: Internal Node w/ 2 Children.
'Data Science > Data Structure & Algorithm' 카테고리의 다른 글
| [Data Structure] AVL Tree - 1: Introduction (0) | 2025.05.01 |
|---|---|
| [Data Structure] Python: Binary Search Tree (0) | 2025.04.23 |
| [Data Structure] BST Inorder Traversal, Height, and Insertion Order (0) | 2025.04.22 |
| [Data Structure] BST Operations - Search, Insert, and Remove (0) | 2025.04.22 |
| [Data Structure] Binary Search Trees (0) | 2025.04.22 |