chapter 11 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 11 PowerPoint Presentation
Download Presentation
Chapter 11

Loading in 2 Seconds...

play fullscreen
1 / 57

Chapter 11 - PowerPoint PPT Presentation


  • 307 Views
  • Uploaded on

Chapter 11. Binary Trees. Chapter 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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 11' - laraine


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 11

Chapter 11

Binary Trees

Data Structures Using C++

chapter objectives
Chapter 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

Data Structures Using C++

binary trees
Binary 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

Data Structures Using C++

binary tree
Binary Tree

Data Structures Using C++

binary tree with one node
Binary Tree With One Node

The root node of the binary tree = A

LA = empty

RA = empty

Data Structures Using C++

binary trees with two nodes
Binary Trees With Two Nodes

Data Structures Using C++

binary trees with two nodes1
Binary Trees With Two Nodes

Data Structures Using C++

various binary trees with three nodes
Various Binary Trees With Three Nodes

Data Structures Using C++

binary trees1
Binary Trees

Following struct defines the node of a binary tree:

template<class elemType>

struct nodeType

{

elemType info;

nodeType<elemType> *llink;

nodeType<elemType> *rlink;

};

Data Structures Using C++

nodes
Nodes
  • 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

Data Structures Using C++

general binary tree
General Binary Tree

Data Structures Using C++

binary tree definitions
Binary 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

Data Structures Using C++

height of a binary tree
Height 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->llink), height(p->rlink))

Data Structures Using C++

height of a binary tree1
Height of a Binary Tree

Function to implement above algorithm:

template<class elemType>

int height(nodeType<elemType> *p)

{

if(p == NULL)

return 0;

else

return 1 + max(height(p->llink),

height(p->rlink));

}

Data Structures Using C++

copy tree
Copy 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

Data Structures Using C++

copy tree1
Copy Tree

template<class elemType>

void copyTree(nodeType<elemType>* &copiedTreeRoot,

nodeType<elemType>* otherTreeRoot)

{

if(otherTreeRoot == NULL)

copiedTreeRoot = NULL;

else

{

copiedTreeRoot = new nodeType<elemType>;

copiedTreeRoot->info = otherTreeRoot->info;

copyTree(copiedTreeRoot->llink, otherTreeRoot->llink);

copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink);

}

}//end copyTree

Data Structures Using C++

binary tree traversal
Binary Tree Traversal
  • Must start with the root, then
    • Visit the node first or
    • Visit the subtrees first
  • Three different traversals
    • Inorder
    • Preorder
    • Postorder

Data Structures Using C++

traversals
Traversals
  • Inorder
    • Traverse the left subtree
    • Visit the node
    • Traverse the right subtree
  • Preorder
    • Visit the node
    • Traverse the left subtree
    • Traverse the right subtree

Data Structures Using C++

traversals1
Traversals
  • Postorder
    • Traverse the left subtree
    • Traverse the right subtree
    • Visit the node

Data Structures Using C++

binary tree inorder traversal
Binary Tree: Inorder Traversal

Data Structures Using C++

binary tree inorder traversal1
Binary Tree: Inorder Traversal

template<class elemType>

void inorder(nodeType<elemType> *p)

{

if(p != NULL)

{

inorder(p->llink);

cout<<p->info<<” “;

inorder(p->rlink);

}

}

Data Structures Using C++

binary tree traversals
Binary Tree: Traversals

template<class elemType>

void postorder(nodeType<elemType> *p)

{

if(p != NULL)

{

postorder(p->llink);

postorder(p->rlink);

cout<<p->info<<” “;

}

}1

template<class elemType>

void preorder(nodeType<elemType> *p)

{

if(p != NULL)

{

cout<<p->info<<” “;

preorder(p->llink);

preorder(p->rlink);

}

}

Data Structures Using C++

implementing binary trees class binarytreetype functions
Public

isEmpty

inorderTraversal

preorderTraversal

postorderTraversal

treeHeight

treeNodeCount

treeLeavesCount

destroyTree

Implementing Binary Trees: class binaryTreeType Functions
  • Private
    • copyTree
    • Destroy
    • Inorder, preorder, postorder
    • Height
    • Max
    • nodeCount
    • leavesCount

Data Structures Using C++

binary search trees
Binary 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

Data Structures Using C++

binary search trees1
Binary Search Trees

Data Structures Using C++

operations performed on binary search trees
Operations 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

Data Structures Using C++

operations performed on binary search trees1
Operations 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

Data Structures Using C++

binary search tree analysis
Binary Search Tree Analysis

Worst Case: Linear tree

Data Structures Using C++

binary search tree analysis1
Binary Search Tree Analysis
  • Theorem: Let T be a binary search tree with n nodes, where n > 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

Data Structures Using C++

binary search tree analysis2
Binary Search Tree Analysis

It follows that:

It is also known that:

Solving Equations (11-1) and (11-2)

Data Structures Using C++

nonrecursive inorder traversal
Nonrecursive Inorder Traversal

Data Structures Using C++

nonrecursive inorder traversal general algorithm
Nonrecursive 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->llink;

}

else

{

pop stack into current;

visit current; //visit the node

current = current->rlink; //move to the

//right child

}

Data Structures Using C++

nonrecursive preorder traversal general algorithm
Nonrecursive 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->llink;

}

else

{

pop stack into current;

current = current->rlink; //prepare to visit

//the right subtree

}

Data Structures Using C++

nonrecursive postorder traversal
Nonrecursive 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->llink;
    • while(stack is not empty)

if(current is not NULL and v is 0)

{

push current and 1 onto stack;

current = current->llink;

}

Data Structures Using C++

nonrecursive postorder traversal continued
Nonrecursive Postorder Traversal (Continued)

else

{

pop stack into current and v;

if(v == 1)

{

push current and 2 onto stack;

current = current->rlink;

v = 0;

}

else

visit current;

}

Data Structures Using C++

avl height balanced trees
AVL (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

Data Structures Using C++

perfectly balanced binary tree
Perfectly Balanced Binary Tree

Data Structures Using C++

avl height balanced trees1
AVL (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

Data Structures Using C++

avl trees
AVL Trees

Data Structures Using C++

non avl trees
Non-AVL Trees

Data Structures Using C++

insertion into avl tree
Insertion Into AVL Tree

Data Structures Using C++

insertion into avl trees
Insertion Into AVL Trees

Data Structures Using C++

insertion into avl trees1
Insertion Into AVL Trees

Data Structures Using C++

insertion into avl trees2
Insertion Into AVL Trees

Data Structures Using C++

insertion into avl trees3
Insertion Into AVL Trees

Data Structures Using C++

avl tree rotations
AVL 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

Data Structures Using C++

avl tree rotations1
AVL Tree Rotations

Data Structures Using C++

avl tree rotations2
AVL Tree Rotations

Data Structures Using C++

avl tree rotations3
AVL Tree Rotations

Data Structures Using C++

avl tree rotations4
AVL Tree Rotations

Data Structures Using C++

avl tree rotations5
AVL Tree Rotations

Data Structures Using C++

avl tree rotations6
AVL Tree Rotations

Data Structures Using C++

deletion from avl trees
Deletion 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

Data Structures Using C++

analysis avl trees
Analysis: AVL Trees

Consider all the possible AVL trees of height h. Let Thbe an AVL tree of height h such that Thhas the fewest number of nodes. Let Thldenote the left subtree of Thand Thrdenote the right subtree of Th. Then:

where | Th| denotes the number of nodes in Th.

Data Structures Using C++

analysis avl trees1
Analysis: AVL Trees

Suppose that Thlis of height h – 1 and Thris of height h – 2. Thlis an AVL tree of height h – 1 such that Thlhas the fewest number of nodes among all AVL trees of height h – 1. Thris an AVL tree of height h – 2 that has the fewest number of nodes among all AVL trees of height h – 2. Thlis of the form Th-1 and Thris of the form Th-2. Hence:

Data Structures Using C++

analysis avl trees2
Analysis: AVL Trees

Let Fh+2 = |Th | + 1. Then:

Called a Fibonacci sequence; solution to Fh is given by:

Hence

From this it can be concluded that

Data Structures Using C++

chapter summary
Chapter Summary
  • Binary trees
  • Binary search trees
  • Recursive traversal algorithms
  • Nonrecursive traversal algorithms
  • AVL trees

Data Structures Using C++