Chapter 08 Binary Trees and Binary Search Trees John Urrutia
2013, All Rights Reserved
Slide 2
The Definition of a Tree A tree is a nonlinear data structure
that is used to store data in a hierarchical manner (ex. Family
tree) A tree is a set of nodes connected by edges (lines connecting
the nodes) The nodes represent the entities (ie. People, data) that
make up the organization John Urrutia 2013, All Rights
Reserved
Slide 3
Parts of a Tree The top of the tree is called the root node If
the node is connected to other nodes ( children) it is a parent
node A node without a child node is called a leaf Visiting all the
nodes of the tree in a distinct order is called a traversal Moving
from one node to the other is called a path or branch John Urrutia
2013, All Rights Reserved
Slide 4
Parts of a Tree The root node is Level 0 (note: some trees dont
have a root) The parents child nodes are at the parents Level +1 A
Level represents all nodes at the same level in the hierarchy A
subtree is any parent node with all of the associated child nodes.
John Urrutia 2013, All Rights Reserved
Slide 5
Unordered Trees An unordered tree has no significance by the
value of data stored or the order in which it is stored John
Urrutia 2013, All Rights Reserved
Slide 6
Binary Trees Each parent node is limited to only two children
This allows for efficiency in programming ( inserting, deleting,
and searching ) The child nodes in a Binary tree are called the
left and right nodes Data with lesser values are always stored on
the left and greater values are always stored on the right This
guarantees a specific order to the trees levels Left node Right
node John Urrutia 2013, All Rights Reserved
Slide 7
Building a Binary Search Tree First we construct a generic Node
Class that is similar to the Node Class of the linked list code
Each node consists of these data elements public class Node {
public int Data; //Nodes Data Element public Node Left; //Ref to
left Node (< Data) public Node Right; //Ref to left Node (>
Data) public void DisplayNode() { Console.Write(Data + " "); } John
Urrutia 2013, All Rights Reserved
Slide 8
Building a Binary Search Tree Next we build a Binary Search
tree class(BST) public class BinarySearchTree { public Node root;
public BinarySearchTree() //Default Constructor { root = null; } -
The first Node object represents the root node of the tree - The
default constructor sets the root node to null, creating a null
Node on which to build John Urrutia 2013, All Rights Reserved
Slide 9
Building a Binary Search Tree Next, we include an Insert method
to add new nodes to our tree public void Insert(int i) { Node
newNode = new Node(); newNode.Data = i; if (root == null) //First
node in tree is assigned root = newNode; //as the ROOT of the tree
else { // code to add the node into the right or left subtree }
This method creates an object of the Node class called newNode
Assigns newNode.Data to the data passed by the caller then checks
to see if the (BST) has a non-null root node John Urrutia 2013, All
Rights Reserved
Slide 10
Building a Binary Search Tree This determines the correct
position for the new node public void Insert(int i) //CONTINUED
Node current = root; //Set current node to root of tree Node
parent; //Create parent node while (true) //find where the current
node belongs { parent = current; //set the prospective parent if (i
< current.Data) //if the new node data is < the parent {
//traverse to the left subtree and current = current.Left;
//replace the prospective parent if (current == null) //if
prospective parent is leaf node { parent.Left = newNode; //add this
new node as the left node and break; //stop } else //traverse to
the right subtree { } John Urrutia 2013, All Rights Reserved
Slide 11
Set the current node to the right child node of the current
node. // else //traverse to the right subtree // { current =
current.Right; //replace the prospective parent if (current ==
null) //if prospective parent is leaf node { parent.Right =
newNode; //add this new node as the right node and break; //stop }
} // End of while (true) } //End of Insert(int i) } //End of
BinarySearchTree class Building a Binary Search Tree John Urrutia
2013, All Rights Reserved
Slide 12
Building a Binary Search Tree New nodes John Urrutia 2013, All
Rights Reserved 5681 227730 9210
Slide 13
Traversing a Binary Search Tree There a three traversal methods
used in the BST: Inorder, Preorder, and Postorder. Inorder
traversal visits all the nodes in ascending order Preorder
traversal visits the root node first, followed by the subtree nodes
moving from left to right Postorder traversals visits the subtree
nodes left to right first, followed by the root node we will
discussed in more detail later (order of operations) John Urrutia
2013, All Rights Reserved
Slide 14
Inorder Traversal 15 1070 5 50 6080 John Urrutia 2013, All
Rights Reserved
Slide 15
Inorder Traversal Code public void InOrder(Node theRoot) { if
(!(theRoot == null)) //End of tree? { //No, then
InOrder(theRoot.Left); //Traverse left theRoot.DisplayNode();
//Print node InOrder(theRoot.Right); //Traverse right } //Yes, go
home } This is written as a recursive procedure John Urrutia 2013,
All Rights Reserved
Slide 16
Inorder Traversal Code static void Main(string[] args) {
BinarySearchTree nums = new BinarySearchTree() nums.Insert(23);
nums.Insert(45); nums.Insert(16); nums.Insert(37); nums.Insert(3);
nums.Insert(99); nums.Insert(22); Console.Write("Inorder traversal:
"); nums.InOrder(nums.root); Output: 3, 16, 22, 23, 37, 45, 99 John
Urrutia 2013, All Rights Reserved
Slide 17
Preorder Traversal : 22 1645 3 23 3799 John Urrutia 2013, All
Rights Reserved
Slide 18
Preorder Traversal : public void PreOrder(Node theRoot) { if
(!(theRoot == null)) //End of tree? { //No, then
theRoot.DisplayNode(); //Print node PreOrder(theRoot.Left);
//Traverse left PreOrder(theRoot.Right); //Traverse right } //Yes,
go home } This is written as a recursive procedure Output: 23, 16,
3, 22, 45, 37, 99 John Urrutia 2013, All Rights Reserved
Slide 19
Postorder Traversal : 22 1645 3 23 3799 John Urrutia 2013, All
Rights Reserved
Slide 20
Postorder Traversal public void PostOrder(Node theRoot) { if
(!(theRoot == null)) //End of tree? { //No, then
PostOrder(theRoot.Left); //Traverse left PostOrder(theRoot.Right);
//Traverse right theRoot.DisplayNode(); //Print node } //Yes, go
home } This is written as a recursive procedure Output: 3, 22, 16,
37, 99, 45, 23 John Urrutia 2013, All Rights Reserved
Slide 21
Finding a Node Three Methods: Minimum, Maximum and Find Minimum
Value: The BST always organizes the smallest or Minimum value to be
the leftmost Child Node or This Node John Urrutia 2013, All Rights
Reserved
Slide 22
Finding a Node Minimum Values: The leftmost Node public int
FindMin() { Node current = root; while (!(current.Left == null))
current = current.Left; return current.Data; } John Urrutia 2013,
All Rights Reserved
Slide 23
Finding a Node Maximum Value: The BST always organizes the
largest or Maximum value will always be the rightmost child Node
John Urrutia 2013, All Rights Reserved
Slide 24
Finding a Node Maximum Values: The rightmost Node public int
FindMax() { Node current = root; while (!(current.Right == null))
current = current.Right; return current.Data; } John Urrutia 2011,
All Rights Reserved John Urrutia 2013, All Rights Reserved
Slide 25
Finding a Node Find method: used to determine if a specified
value is stored in the BST The method creates a Node object and
proceeds to compare it to the other Nodes in the tree by moving
left or right until it is found or there are no more Nodes to
search: John Urrutia 2013, All Rights Reserved
Slide 26
Finding a Node John Urrutia 2013, All Rights Reserved
Slide 27
Finding a Node public Node Find(int key) { Node current = root;
//Start at root while (current.Data != key) //key found? { //No,
then if (key < current.Data) //key < data? current =
current.Left; //Yes, reset to left subtree else current =
current.Right; //No, reset to right subtree if (current == null)
//End of Tree? return null; //Yes, key not found } return current;
//key was found, return node } John Urrutia 2013, All Rights
Reserved
Slide 28
Deleting a Leaf Node Think of it as pulling a real life leaf
off of a tree. There is nothing else attached to it. The Node to be
removed simply found and set to null Before After John Urrutia
2013, All Rights Reserved
Slide 29
Deleting Node with One Child The node is cut from the tree
links single child to the parent of the removed node John Urrutia
2013, All Rights Reserved
Slide 30
22 16 3 45 23 3799 John Urrutia 2013, All Rights Reserved
Deleting with Two Children This is more complex. We cant just
connect the child Node to the deleted Nodes parent. Delete Node 23
CANT HAVE TWO LEFT NODES
Slide 31
22 16 3 45 23 3799 John Urrutia 2013, All Rights Reserved
Deleting with Two Children Replacing the parent with the leftmost
leaf Node of the rightmost subtree (successor) Loss of balance
Slide 32
Delete Method Method used to delete a value from the BST First
checks to see if the value is a leaf If not, find the value that
needs to be placed in deleted node to ensure correct tree structure
For the left subtree this is will be rightmost successor node For
the right subtree this will be the leftmost successor node This
will always work but introduces another problem. Loss of balance.
John Urrutia 2013, All Rights Reserved
Slide 33
Deleting Nightmare The successor to any Node is the in order
Node that follows it. John Urrutia 2013, All Rights Reserved 15
1070 5 50 6080 5 10 15 50 60 70 - 80 6080
Slide 34
Deleting Nightmare Worst Case the successor has 1 child in the
right subtree. John Urrutia 2013, All Rights Reserved 15 1070 5 50
806080 65 Step 1:SuccessorParent leftChild = Successor.rightChild
Step 2:Successor rightChild = DeletedNode.rightChild Step
3:ParentNode rightChild = Successor Step 4:Successor leftChild =
DeletedNode.leftChild SP S DN
Slide 35
BST Imbalance oops The previous examples have shown perfect
scenario BSTs. The trees were fairly balanced but what can easily
happen is shown here.
Self-Balancing BSTs A balanced tree maintains a predefined
ratio between its height and width. This offers a guaranteed BST
search performance of close to log 2 (n) Several Types: AVL trees
Red-Black trees 2-3 and 2-3-4 trees Splay trees B-trees, and
more
Slide 38
The Huffman Code We have seen BSTs used to: Efficiently search
for elements in an ordered list Represent a way of processing
algebraic expressions using precedence BSTs can also be used to
compress messages using Huffman codes. John Urrutia 2013, All
Rights Reserved
Slide 39
The Huffman Code SUSIE SAYS IT IS EASY John Urrutia 2013, All
Rights Reserved
Slide 40
The Huffman Code Assigns a unique set of bits for each
character in the message which will save bytes. I.E. now is the
time for all good men to come to the aid of their country. ( are
the blanks/spaces between words) 1 st Create a priority queue by
character frequency in the message 2 nd Assign a bit string to
represent each character 3 rd translate the characters to the bit
string John Urrutia 2013, All Rights Reserved
Slide 41
The Huffman Code now is the time for all good men to come to
the aid of their country. ( are the blanks/spaces between words) 1
st Create a priority queue by character frequency from the message
John Urrutia 2013, All Rights Reserved oteihmnracdflgsuwy 15
976433332222211111
Slide 42
The Huffman Code now is the time for all good men to come to
the aid of their country. ( are the blanks/spaces between words) 2
nd Assign a bit string to represent each character John Urrutia
2013, All Rights Reserved oteihmnracdfl 001001011001101110 0111 0
1111 0 0111 10 1111 10 0111 110 1111 110 0111 1110 1111 1110 gsuwy
0111 1111 10 1111 1111 10 0111 1111 110 1111 1111 110 0111 1111
1110
Slide 43
The Huffman Code now is the time for all good men to come to
the aid of their country. ( are the blanks/spaces between words) 3
rd translate the characters to the bit string John Urrutia 2013,
All Rights Reserved 11110101111111111000 0110111111111000
010111011000 01001100111011000 011111101001111000
111110111111101111111000 01111111101010111111000 0111011001111000
0101000 0111110100111011000 0101000 01011101100
11111100110111111000 100111111000 0101110110011001111000
0111110100111111111011110010011110011111111110
Slide 44
The Huffman Code now is the time for all good men to come to
the aid of their country. ( are the blanks/spaces between words) 3
rd translate the characters to the bit string John Urrutia 2013,
All Rights Reserved
111101011111111110000110111111111000010111011000
010011001110110000111111010011110001111101111111
011111110000111111110101011111100001110110011110
000101000011111010011101100001010000101110110011
111100110111111000100111111000010111011001100111
100001111101001111111110111100100111100111111111 10 F5 FF 86 FF 85
D8 4C EC 3F 4F 1F 7F 7F 0F F5 7E 1D 9E 14 3E 9D 85 0B B3 F3 7E 27
71 76 67 87 D3 FE F2 79 FF 80
Slide 45
The Huffman Code now is the time for all good men to come to
the aid of their country. ( are the blanks/spaces between words)
Efficiency 53 down to 35 ~30% savings in space John Urrutia 2013,
All Rights Reserved