Title: Binary Trees
1Chapter 11
2Chapter Objectives
- Learn about binary trees
- Explore various binary tree traversal algorithms
- Learn how to organize data in a binary search
tree - Discover how to insert and delete items in a
binary search tree - Explore nonrecursive binary tree traversal
algorithms - Learn about AVL (height-balanced) trees
3Binary Trees
- Definition A binary tree, T, is either empty or
such that - T has a special node called the root node
- T has two sets of nodes, LT and RT, called the
left subtree and right subtree of T,
respectively - LT and RT are binary trees
4Binary Tree
5Binary Tree With One Node
The root node of the binary tree A LA
empty RA empty
6Binary Trees With Two Nodes
7Binary Trees With Two Nodes
8Various Binary Trees With Three Nodes
9Binary Trees
- Following struct defines the node of a binary
tree - templateltclass elemTypegt
- struct nodeType
-
- elemType info
- nodeTypeltelemTypegt llink
- nodeTypeltelemTypegt rlink
10Nodes
- For each node
- Data is stored in info
- The pointer to the left child is stored in llink
- The pointer to the right child is stored in rlink
11General Binary Tree
12Binary Tree Definitions
- Leaf node that has no left and right children
- Parent node with at least one child node
- Level of a node number of branches on the path
from root to node - Height of a binary tree number of nodes no the
longest path from root to node
13Height of a Binary Tree
- Recursive algorithm to find height of binary
tree - (height(p) denotes height of binary tree with
root p) - if(p is NULL)
- height(p) 0
- else
- height(p) 1 max(height(p-gtllink),
height(p-gtrlink))
14Height of a Binary Tree
- Function to implement above algorithm
- templateltclass elemTypegt
- int height(nodeTypeltelemTypegt p)
-
- if(p NULL)
- return 0
- else
- return 1 max(height(p-gtllink),
- height(p-gtrlink))
15Copy Tree
- Useful operation on binary trees is to make
identical copy of binary tree - Use function copyTree when we overload assignment
operator and implement copy constructor
16Copy Tree
- templateltclass elemTypegt
- void copyTree(nodeTypeltelemTypegt
copiedTreeRoot, - nodeTypeltelemTypegt otherTreeRoot)
-
- if(otherTreeRoot NULL)
- copiedTreeRoot NULL
- else
-
- copiedTreeRoot new nodeTypeltelemTypegt
- copiedTreeRoot-gtinfo otherTreeRoot-gtinfo
- copyTree(copiedTreeRoot-gtllink,
otherTreeRoot-gtllink) - copyTree(copiedTreeRoot-gtrlink,
otherTreeRoot-gtrlink) -
- //end copyTree
17Binary Tree Traversal
- Must start with the root, then
- Visit the node first or
- Visit the subtrees first
- Three different traversals
- Inorder
- Preorder
- Postorder
18Traversals
- Inorder
- Traverse the left subtree
- Visit the node
- Traverse the right subtree
- Preorder
- Visit the node
- Traverse the left subtree
- Traverse the right subtree
19Traversals
- Postorder
- Traverse the left subtree
- Traverse the right subtree
- Visit the node
20Binary Tree Inorder Traversal
21Binary Tree Inorder Traversal
templateltclass elemTypegt void inorder(nodeTypeltele
mTypegt p) if(p ! NULL)
inorder(p-gtllink) coutltltp-gtinfoltlt
inorder(p-gtrlink)
22Binary Tree Traversals
templateltclass elemTypegt void postorder(nodeTypelte
lemTypegt p) if(p ! NULL)
postorder(p-gtllink) postorder(p-gtrlink)
coutltltp-gtinfoltlt 1
templateltclass elemTypegt void preorder(nodeTypeltel
emTypegt p) if(p ! NULL)
coutltltp-gtinfoltlt preorder(p-gtllink)
preorder(p-gtrlink)
23Implementing Binary Trees class binaryTreeType
Functions
- Public
- isEmpty
- inorderTraversal
- preorderTraversal
- postorderTraversal
- treeHeight
- treeNodeCount
- treeLeavesCount
- destroyTree
- Private
- copyTree
- Destroy
- Inorder, preorder, postorder
- Height
- Max
- nodeCount
- leavesCount
24Binary Search Trees
- Data in each node
- Larger than the data in its left child
- Smaller than the data in its right child
- A binary search tree,t, is either empty or
- T has a special node called the root node
- T has two sets of nodes, LT and RT, called the
left subtree and right subtree of T, respectively - Key in root node larger than every key in left
subtree and smaller than every key in right
subtree - LT and RT are binary search trees
25Binary Search Trees
26Operations Performed on Binary Search Trees
- Determine whether the binary search tree is empty
- Search the binary search tree for a particular
item - Insert an item in the binary search tree
- Delete an item from the binary search tree
27Operations Performed on Binary Search Trees
- Find the height of the binary search tree
- Find the number of nodes in the binary search
tree - Find the number of leaves in the binary search
tree - Traverse the binary search tree
- Copy the binary search tree
28Binary Search Tree Analysis
Worst Case Linear tree
29Binary Search Tree Analysis
- Theorem Let T be a binary search tree with n
nodes, where n gt 0.The average number of nodes
visited in a search of T is approximately
1.39log2n - Number of comparisons required to determine
whether x is in T is one more than the number of
comparisons required to insert x in T - Number of comparisons required to insert x in T
same as the number of comparisons made in
unsuccessful search, reflecting that x is not in T
30Binary Search Tree Analysis
It is also known that
Solving Equations (11-1) and (11-2)
31Nonrecursive Inorder Traversal
32Nonrecursive Inorder Traversal General Algorithm
- current root //start traversing the binary
tree at - // the root node
- while(current is not NULL or stack is nonempty)
- if(current is not NULL)
-
- push current onto stack
- current current-gtllink
-
- else
-
- pop stack into current
- visit current //visit the node
- current current-gtrlink //move to
the - //right
child -
33Nonrecursive Preorder Traversal General Algorithm
- 1. current root //start the traversal at the
root node - 2. while(current is not NULL or stack is
nonempty) - if(current is not NULL)
-
- visit current
- push current onto stack
- current current-gtllink
-
- else
-
- pop stack into current
- current current-gtrlink //prepare to
visit - //the right
subtree -
34Nonrecursive Postorder Traversal
- current root //start traversal at root node
- v 0
- if(current is NULL)
- the binary tree is empty
- if(current is not NULL)
- push current into stack
- push 1 onto stack
- current current-gtllink
- while(stack is not empty)
- if(current is not NULL and v is 0)
-
- push current and 1 onto stack
- current current-gtllink
-
35Nonrecursive Postorder Traversal (Continued)
- else
-
- pop stack into current and v
- if(v 1)
-
- push current and 2 onto stack
- current current-gtrlink
- v 0
-
- else
- visit current
-
36AVL (Height-balanced Trees)
- A perfectly balanced binary tree is a binary tree
such that - The height of the left and right subtrees of the
root are equal - The left and right subtrees of the root are
perfectly balanced binary trees
37Perfectly Balanced Binary Tree
38AVL (Height-balanced Trees)
- An AVL tree (or height-balanced tree) is a binary
search tree such that - The height of the left and right subtrees of the
root differ by at most 1 - The left and right subtrees of the root are AVL
trees
39AVL Trees
40Non-AVL Trees
41Insertion Into AVL Tree
42Insertion Into AVL Trees
43Insertion Into AVL Trees
44Insertion Into AVL Trees
45Insertion Into AVL Trees
46AVL Tree Rotations
- Reconstruction procedure rotating tree
- left rotation and right rotation
- Suppose that the rotation occurs at node x
- Left rotation certain nodes from the right
subtree of x move to its left subtree the root
of the right subtree of x becomes the new root of
the reconstructed subtree - Right rotation at x certain nodes from the left
subtree of x move to its right subtree the root
of the left subtree of x becomes the new root of
the reconstructed subtree
47AVL Tree Rotations
48AVL Tree Rotations
49AVL Tree Rotations
50AVL Tree Rotations
51AVL Tree Rotations
52AVL Tree Rotations
53Deletion From AVL Trees
- Case 1 the node to be deleted is a leaf
- Case 2 the node to be deleted has no right
child, that is, its right subtree is empty - Case 3 the node to be deleted has no left child,
that is, its left subtree is empty - Case 4 the node to be deleted has a left child
and a right child
54Analysis AVL Trees
Consider all the possible AVL trees of height h.
Let Th be an AVL tree of height h such that Th
has the fewest number of nodes. Let Thl denote
the left subtree of Th and Thr denote the right
subtree of Th. Then
where Th denotes the number of nodes in Th.
55Analysis AVL Trees
Suppose that Thl is of height h 1 and Thr is of
height h 2. Thl is an AVL tree of height h 1
such that Thl has the fewest number of nodes
among all AVL trees of height h 1. Thr is an
AVL tree of height h 2 that has the fewest
number of nodes among all AVL trees of height h
2. Thl is of the form Th -1 and Thr is of the
form Th -2. Hence
56Analysis AVL Trees
Let Fh2 Th 1. Then
Called a Fibonacci sequence solution to Fh is
given by
Hence
From this it can be concluded that
57Chapter Summary
- Binary trees
- Binary search trees
- Recursive traversal algorithms
- Nonrecursive traversal algorithms
- AVL trees