1
Binary Trees Informal defn: each node has 0, 1, or 2 childrenInformal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure thatFormal defn: a binary tree is a structure that
contains no nodes, or contains no nodes, or is comprised of three disjoint sets of nodes:is comprised of three disjoint sets of nodes:
a a rootroota binary tree called its a binary tree called its left subtreeleft subtreea binary tree called its a binary tree called its right subtreeright subtree
A binary tree that contains no nodes is called A binary tree that contains no nodes is called emptyempty
Note: the position of a child Note: the position of a child mattersmatters!!
2
Binary Trees FullFull binary tree : binary tree :
All internal nodes have two children.All internal nodes have two children. CompleteComplete binary tree : binary tree :
All leaves have the same depthAll leaves have the same depth All internal nodes have two childrenAll internal nodes have two children
A complete binary tree of height A complete binary tree of height hh has 2 has 2hh-1 -1 internal nodes and 2internal nodes and 2hh leaves leaves
Also: a binary tree with Also: a binary tree with nn nodes has nodes has height at height at least least lg lgn n
3
Tree applications Expression evaluations (note how different Expression evaluations (note how different
traversals result in different notation)traversals result in different notation) Parsing (as part of the compilation process) Parsing (as part of the compilation process) Storing and retrieving information by a keyStoring and retrieving information by a key Representing structured objects (e.g. the universe Representing structured objects (e.g. the universe
in an adventure game)in an adventure game) Useful when needing to make a decision on how Useful when needing to make a decision on how
to proceed (tic-tac-toe, chess)to proceed (tic-tac-toe, chess)
4
Binary tree traversal Traversing a tree = visiting its nodesTraversing a tree = visiting its nodes There are three ways to traverse a binary treeThere are three ways to traverse a binary tree
preorder : visit root, preorder : visit root, visit left subtree, visit left subtree,
visit right subtree visit right subtree inorder : visit left subtree, inorder : visit left subtree,
visit root, visit root, visit right subtree visit right subtree
postorder : visit left subtree, postorder : visit left subtree, visit right subtree, visit right subtree,
visit root visit root
5
Example: Inorder traversal
template <class T>
Tree<T>::Inorder(TreeNode<T> *subroot ) {
if (subroot!=NULL) {
Inorder(subroot left);
cout << subrootelement;
Inorder(subroot right);
}
}
6
Binary trees
Use for storing and retrieving informationUse for storing and retrieving information We want to insert, delete and search at least as fast We want to insert, delete and search at least as fast
and, if possible, faster than with a linked listand, if possible, faster than with a linked list We want to take advantage of the lgWe want to take advantage of the lgnn height height Idea: Store information in an ordered way (use a Idea: Store information in an ordered way (use a
keykey)) Result: a Binary Search Tree (BST)Result: a Binary Search Tree (BST)
7
Binary Search Tree (BST)
A BST is a binary tree with the following A BST is a binary tree with the following property:property: The key of the root is larger than any key in the The key of the root is larger than any key in the
left subtree and smaller than any key in the left subtree and smaller than any key in the right subtree (the subtrees are also BSTs)right subtree (the subtrees are also BSTs)
Note: This definition does not allow duplicate Note: This definition does not allow duplicate keys. keys.
It’s now easy to search for an elementIt’s now easy to search for an element
8
Binary Search Tree (BST)
How do we insert an element into a BST? How do we insert an element into a BST? We have to make sure it is inserted at the correct We have to make sure it is inserted at the correct
position.position. It is a combination of Search and Insert.It is a combination of Search and Insert.
9
Binary Search Tree (BST)
How do we remove an element from a BST? How do we remove an element from a BST? Removing a leaf is easyRemoving a leaf is easy Removing an internal node can be tricky.Removing an internal node can be tricky. The tree will need to be rearrangedThe tree will need to be rearranged (how?)(how?)
10
Balanced trees
The good news: Why not sort a sequence by The good news: Why not sort a sequence by inserting the elements into a BST?inserting the elements into a BST? on average O(nlgn) comparisonson average O(nlgn) comparisons we get to keep the treewe get to keep the tree
The bad news: The insertion procedure can result in The bad news: The insertion procedure can result in a tree of height a tree of height nn after inserting after inserting nn elements. elements.
We would prefer to get trees that are guaranteed to We would prefer to get trees that are guaranteed to have logarithmic height in the worst case.have logarithmic height in the worst case.
Such trees are called Such trees are called balancedbalanced. .
11
AVL trees
AVL tree = a binary search tree with the following AVL tree = a binary search tree with the following property: for every node the heights of the left and property: for every node the heights of the left and right subtrees differ at most by one.right subtrees differ at most by one.
That’s all very nice but how do we guarantee it? That’s all very nice but how do we guarantee it? We have to somehow modify the insert and delete We have to somehow modify the insert and delete
functions. functions. If, after an insertion or deletion, the property is not If, after an insertion or deletion, the property is not
satisfied, we “rotate” the tree to make it balanced.satisfied, we “rotate” the tree to make it balanced.
12
AVL trees
When can an insertion of a child y at node x cause When can an insertion of a child y at node x cause an imbalance?an imbalance? when both x and y are left childrenwhen both x and y are left children when both x and y are right childrenwhen both x and y are right children when x is a right child and y is a left childwhen x is a right child and y is a left child when y is a right child and x is a left childwhen y is a right child and x is a left child
13
AVL trees
There are two types of rotations:There are two types of rotations: single rotationsingle rotation
fixes imbalance of type 1/2fixes imbalance of type 1/2 double rotationdouble rotation
fixes imbalance of type 3/4fixes imbalance of type 3/4 Let’s draw some trees...Let’s draw some trees...
14
AVL trees How/when do we decide whether to rotate?How/when do we decide whether to rotate? Example: insertionExample: insertion
step 1: walk down the tree to insert the node in step 1: walk down the tree to insert the node in the correct positionthe correct position
step 2: walk up the tree checking the property at step 2: walk up the tree checking the property at each nodeeach node
we need a helper function to determine the heights we need a helper function to determine the heights of the subtrees of each node. of the subtrees of each node.
we need to be able to determine whether to we need to be able to determine whether to perform a single or a double rotationperform a single or a double rotation
15
AVL trees
Just how balanced are AVL trees?Just how balanced are AVL trees? It can be shown that the worst case height of an It can be shown that the worst case height of an
AVL tree is at most 44% more than the minimum AVL tree is at most 44% more than the minimum possible for BST (i.e. approximately 1.44 lgn)possible for BST (i.e. approximately 1.44 lgn)
What does this mean?What does this mean? Searching/Inserting/Removing now take O(lgn) Searching/Inserting/Removing now take O(lgn)
in the worst case.in the worst case.
Top Related