(New page: = Binary Trees = Transcribed by John Jachna on 4/28/11<br> <br> Visualization of a Binary Tree: [http://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_tree.svg upload.wikimedi...) |
|||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
− | = Binary Trees = | + | [[Category:ECE264]] [[Category:Programming]] [[Category:C]] |
+ | = [[ECE264]]: Binary Trees = | ||
− | Transcribed by John Jachna on 4/28/11<br> | + | Transcribed by John Jachna on 4/28/11<br> |
− | <br> | + | <br> |
Visualization of a Binary Tree: [http://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_tree.svg upload.wikimedia.org/wikipedia/commons/f/f7/Binary_tree.svg] | Visualization of a Binary Tree: [http://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_tree.svg upload.wikimedia.org/wikipedia/commons/f/f7/Binary_tree.svg] | ||
Line 11: | Line 12: | ||
<br> <br>Binary Trees are made up of structures containing a value and pointers to structures for left and right values:<br> | <br> <br>Binary Trees are made up of structures containing a value and pointers to structures for left and right values:<br> | ||
<blockquote>typedef struct Node<br>{<br> int value;<br> struct Node *left, *right;<br>} Node;<br> </blockquote> | <blockquote>typedef struct Node<br>{<br> int value;<br> struct Node *left, *right;<br>} Node;<br> </blockquote> | ||
− | <br> | Sorted Array | BST*<br>----------|--------------------|--------<br>insert | O(n) | Olog(n)<br>delete | O(n) | Olog(n)<br>search | Olog(n) | Olog(n) | + | <br> | Sorted Array | BST*<br>----------|--------------------|--------<br>insert | O(n) | Olog(n)<br>delete | O(n) | Olog(n)<br>search | Olog(n) | Olog(n) |
*(if balanced)<br> <br> Recall that a constant ( O(1) ) is ideal. Olog(n) (logarithmic) is good, followed by O(n) (linear) and then O(n^2) (squared) | *(if balanced)<br> <br> Recall that a constant ( O(1) ) is ideal. Olog(n) (logarithmic) is good, followed by O(n) (linear) and then O(n^2) (squared) | ||
− | A node N in a Binary Search Tree:<br> - has two or less children<br> - N->value is greater than N->left<br> - N-> value is less than N->right | + | A node N in a Binary Search Tree:<br> - has two or less children<br> - N->value is greater than N->left<br> - N-> value is less than N->right |
− | <br> | + | <br> |
− | <blockquote> | + | <blockquote>Node* Tree_insert(Node* root, Node* n)<br>{<br> if (root == NULL) return n;<br> if (n->value < root->value)<br> {<br> root->left = Tree_insert(root->right, n)<br> }<br> return root;<br>} Node* Tree_search(Node* root, int value)<br>{<br> if (root == NULL) return NULL;<br> if (root->value == value) return root;<br> if (root->value > value) return Tree_search(root->left, value);<br> else return Tree_search(root->right, value);<br>} <br> </blockquote> |
− | Node* Tree_insert(Node* root, Node* n)<br>{<br> if (root == NULL) return n;<br> if (n->value < root->value)<br> {<br> root->left = Tree_insert(root->right, n)<br> }<br> return root;<br>} | + | |
− | + | ||
− | Node* Tree_search(Node* root, int value)<br>{<br> if (root == NULL) return NULL;<br> if (root->value == value) return root;<br> if (root->value > value) return Tree_search(root->left, value);<br> else return Tree_search(root->right, value);<br>} | + | |
− | + | ||
− | <br> | + | |
− | </blockquote> | + | |
Deleting an element:<br> Case 1: element is a leaf => just delete the node<br> Case 2: element has one child => replace the node with its child<br> Case 3: element has two children =><br> - Delete the node, then<br> - Replace with the biggest number in the left subtree (PREDECESSOR) or the smallest in the right subtree (SUCCESSOR)<br> - To find the largest value in a tree, go as far right as possible (go right until the right pointer is NULL)<br> - To find the smallest value in a tree, go as far left as possible<br><br> | Deleting an element:<br> Case 1: element is a leaf => just delete the node<br> Case 2: element has one child => replace the node with its child<br> Case 3: element has two children =><br> - Delete the node, then<br> - Replace with the biggest number in the left subtree (PREDECESSOR) or the smallest in the right subtree (SUCCESSOR)<br> - To find the largest value in a tree, go as far right as possible (go right until the right pointer is NULL)<br> - To find the smallest value in a tree, go as far left as possible<br><br> | ||
+ | ---- | ||
+ | [[2011_Spring_ECE_264_Lu|Back to ECE264, Spring 2011, Prof. Lu]] |
Latest revision as of 05:26, 11 July 2012
ECE264: Binary Trees
Transcribed by John Jachna on 4/28/11
Visualization of a Binary Tree: upload.wikimedia.org/wikipedia/commons/f/f7/Binary_tree.svg
The top is called the Root Items that point to other nodes are Parents. The nodes a parent points to are called children.
A node may have 2, 1, or 0 children. If a node has no children, it is called a leaf.
The links connecting parents to children are called edges.
In a Binary Search Tree, the left value is always less than the right value.
Binary Trees are made up of structures containing a value and pointers to structures for left and right values:
typedef struct Node
{
int value;
struct Node *left, *right;
} Node;
| Sorted Array | BST*
----------|--------------------|--------
insert | O(n) | Olog(n)
delete | O(n) | Olog(n)
search | Olog(n) | Olog(n)
- (if balanced)
Recall that a constant ( O(1) ) is ideal. Olog(n) (logarithmic) is good, followed by O(n) (linear) and then O(n^2) (squared)
A node N in a Binary Search Tree:
- has two or less children
- N->value is greater than N->left
- N-> value is less than N->right
Node* Tree_insert(Node* root, Node* n)
{
if (root == NULL) return n;
if (n->value < root->value)
{
root->left = Tree_insert(root->right, n)
}
return root;
} Node* Tree_search(Node* root, int value)
{
if (root == NULL) return NULL;
if (root->value == value) return root;
if (root->value > value) return Tree_search(root->left, value);
else return Tree_search(root->right, value);
}
Deleting an element:
Case 1: element is a leaf => just delete the node
Case 2: element has one child => replace the node with its child
Case 3: element has two children =>
- Delete the node, then
- Replace with the biggest number in the left subtree (PREDECESSOR) or the smallest in the right subtree (SUCCESSOR)
- To find the largest value in a tree, go as far right as possible (go right until the right pointer is NULL)
- To find the smallest value in a tree, go as far left as possible