Data Structures – Binary Tree. What is a tree? Where do you see trees? Ummm...outside file...
-
Upload
sibyl-baker -
Category
Documents
-
view
217 -
download
1
Transcript of Data Structures – Binary Tree. What is a tree? Where do you see trees? Ummm...outside file...
Tree Terminology
• node – any element of the tree• root – the topmost node of the tree• parent – a node that has one or more nodes
connected below it (children)• child – a node that has a connected node above
it (parent)• leaf – any child node at the bottom of the tree• subtree – a parent and all the nodes below it
So what’s a binary tree?
1. A parent can have at most TWO children (left child, right child)
2. The left child’s data and all the data in the left subtree is “less than” the parent’s data
3. The right child’s data all the data in the right subtree is “greater than” the parent’s data
• NOTE: The “less than” and “greater than” requirements of the subtrees is commonly known outside of the IB world as a Binary Search Tree
Practice 1
• Create a binary tree by inserting the following numbers in the given order:
6, 4, 2, 7, 8, 14, 9
Practice 2
• Create a binary tree by inserting the following numbers in the given order :
14, 75, 2, 34, 25, 26, 27, 28
Practice 3
• Create a binary tree by inserting the following strings:
Wanda, Alpos, Vazbyte, Fecso, Downs, Mata, Montante
Practice 4
• Create a binary tree by inserting the following strings:
Ramos, Dia, Khurelbaatar, Nice,
Ren, Shahid, Zetkulic
Adding to a Binary Tree
1. Start at the root
2. Compare against the current node1. Go left if less than current node OR insert if
there is none (creating a new leaf)
2. Go right if greater than current node OR insert if there is none (creating a new leaf)
3. Repeat step 2
Searching in a Binary Tree
1. Start at the root
2. Compare against the current node1. Found the node if they are equal
2. Go left if less than current node OR if there is no left, then does not exist
3. Go right if greater than current node OR if there is no left, then does not exist
3. Repeat step 2
Removing from a Binary Tree
1. Search for matching node
2. If node is a leaf, then unlink its parent
3. If node is a parent of one child, then link the node’s parent to the node’s child
4. If node is a parent of two children, then travel down its right subtree to find the left-most leaf (smallest value of the right subtree). Take the value and put it in the original node that was being removed. Unlink the right-most leaf that you found.
When do we use binary trees?
• ...whenever we need to search for quickly– Inherent binary searching capabilities– Large trees can be searched quickly
• What is the best case scenario?• What is the worst case scenario?• What is the average case scenario?
• Compare a Linked List to a Binary Tree
Tree Traversal
• Add, remove, search only go down 1 path
• How do you “walk-through” all the nodes of a tree?
In-order tree traversal
1. Left-subtree traversal if it exists and rerun
2. Action on the current node (e.g. print)
3. Right-subtree traversal if it exists and rerun
Note: In-order traversal often used to visit nodes in their inherent order
Pre-order tree traversal
1. Action on the current node (e.g. copy)
2. Left-subtree traversal if it exists and rerun
3. Right-subtree traversal if it exists and rerun
Note: Pre-order traversal is often used to duplicate a tree
Post-order tree traversal
1. Left-subtree traversal if it exists and rerun
2. Right-subtree traversal if it exists and rerun
3. Action on the current node (e.g. print)
Note: Post-order traversal is often used to completely delete or free up all nodes by visiting children and lowest levels first. (not really necessary with garbage collection)
Other Resources
• http://www.csanimated.com/animation.php?t=Binary_search_tree