(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...)
 
Line 1: Line 1:
 
= Binary Trees  =
 
= 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:&nbsp;[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:&nbsp;[http://upload.wikimedia.org/wikipedia/commons/f/f7/Binary_tree.svg upload.wikimedia.org/wikipedia/commons/f/f7/Binary_tree.svg]  
Line 11: Line 11:
 
<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | 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-&gt;value is greater than N-&gt;left<br> - N-&gt; value is less than N-&gt;right
+
A node N in a Binary Search Tree:<br> - has two or less children<br> - N-&gt;value is greater than N-&gt;left<br> - N-&gt; value is less than N-&gt;right  
  
<br>
+
<br>  
<blockquote>
+
<blockquote>Node* Tree_insert(Node* root, Node* n)<br>{<br> if (root == NULL) return n;<br> if (n-&gt;value &lt; root-&gt;value)<br> {<br> root-&gt;left = Tree_insert(root-&gt;right, n)<br> }<br> return root;<br>} Node* Tree_search(Node* root, int value)<br>{<br> if (root == NULL) return NULL;<br> if (root-&gt;value == value) return root;<br> if (root-&gt;value &gt; value) return Tree_search(root-&gt;left, value);<br> else return Tree_search(root-&gt;right, value);<br>} <br> </blockquote>  
Node* Tree_insert(Node* root, Node* n)<br>{<br> if (root == NULL) return n;<br> if (n-&gt;value &lt; root-&gt;value)<br> {<br> root-&gt;left = Tree_insert(root-&gt;right, n)<br> }<br> return root;<br>}
+
 
+
Node* Tree_search(Node* root, int value)<br>{<br> if (root == NULL) return NULL;<br> if (root-&gt;value == value) return root;<br> if (root-&gt;value &gt; value) return Tree_search(root-&gt;left, value);<br> else return Tree_search(root-&gt;right, value);<br>}
+
 
+
<br>
+
</blockquote>
+
 
Deleting an element:<br> Case 1: element is a leaf =&gt; just delete the node<br> Case 2: element has one child =&gt; replace the node with its child<br> Case 3: element has two children =&gt;<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 =&gt; just delete the node<br> Case 2: element has one child =&gt; replace the node with its child<br> Case 3: element has two children =&gt;<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>

Revision as of 06:21, 28 April 2011

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

Alumni Liaison

Sees the importance of signal filtering in medical imaging

Dhruv Lamba, BSEE2010