# DS_Lec08

Document Sample

```					Data Structures in C++                         1                             Binary trees

Data Structures in C++
Lecture 8: Binary trees

8-1 What are binary trees?
A binary tree is either empty, or it consists of a node called the root together with two
binary trees called the left sub-tree and the right sub-tree of the root.

8-2 Binary Search Tree (BST)
A binary search tree is a tree that may be empty. If it is not empty, then it has the
following properties:
   Every element has a key (data), no two elements can have the same key
   The key in the left sub-tree must be smaller than the key in the root
   The key in the right sub-tree must be larger than the key in the root
   The left and the right sub-tree are also binary search trees

Example 8-2-1
Which of the following tress is a binary search tree?

10                                   10

5                              20           17                           20

(a)                                           (b)

5                                  7

5                             12            2                         4

(c)                                     (d)
Data Structures in C++                     2                                  Binary trees

Solution
   Tree (a) is a BST.
   Tree (b) is not a BST; the key is the left node is greater than the key in the root.
   Tree (c) is not a BST; the key in the left node equals the key in the root.
   Tree (d) is not a BST; the key is the right node is less than the key in the root.

Example 8-2-2
Show what happens when we insert the following keys into an initially empty binary
search tree in the order given.

7, 3, 1, 5, 10, 8, 15

Draw the final version of the tree.

Solution

7

3                                   10

1                    5           8                       15

Example 8-2-3
Show what happens when we insert the following keys into an initially empty binary
search tree in the order given.

7, 1, 5, 3, 15, 10, 8

Solution
Data Structures in C++                   3                           Binary trees

7

1                                   15

5                10

3                 8

Example 8-2-4
Show what happens when we insert the following keys into an initially empty binary
search tree in the order given.
e, b, d, f, a, g, c
Solution
Data Structures in C++                      4                             Binary trees

8-3 Traversal of binary trees
One of the most important operations on a binary tree is traversal, moving through all
the nodes of the binary tree, visiting each one in turn. There are three types of binary tree
traversal:
8-3-1 Pre-order Traversal
Visit the node, then the left sub-tree then the right sub-tree.
8-3-2 In-order Traversal
Visit the left sub-tree, then the node, then the right sub-tree.
8-3-2 Post-order Traversal
Visit the left sub-tree, then the right sub-tree, then the node.
Example 8-3-1
Show the output of visiting each of the following trees according to pre-order
traversal, in-order traversal and post-order traversal.

7

3                                   10

1                     5           8                  15
Data Structures in C++                     5                                 Binary trees

*

+                                   -

a                    b          c                        d

8-4 Implementing binary trees as linked lists
A binary tree can be declared as a linked list; in this case each tree element will be a
structure that contains three members: the data, the address of the left sub-tree and the

8-4-1Programming Example
Write a C++ program that implements binary search tree as a linked list. Assume that
the three will contain elements of type int. The program must contain the following
functions:

   insert() to insert a new element into a binary search tree
   inorder() to traverse a binary search tree according to the in-order traversal
   preorder() to traverse a binary search tree according to the pre-order traversal
   postorder() to traverse a binary search tree according to the post-order traversal

The program must contain a main() function that performs the following operations:

1. Create an empty binary search tree.
2. Insert the following keys into the tree in the order given
11, 5, 3, 12, 17
3. Call each of the functions inorder(), preorder() and postorder() to traverse the tree.
Data Structures in C++                      6             Binary trees

Solution
#include<iostream.h>
typedef int T;
struct Node
{
T data;
Node* left;
Node* right;
};
Node* insert(Node* p, T v)
{
Node* x;
x = new Node;
x->data = v;
x->left = x->right = NULL;
if (p == NULL)
p=x;
else if (v < p->data)
p->left = insert(p->left, v);
else if (v > p->data)
p->right = insert(p->right, v);
return p; // return the new tree after insertion
}
void preorder(Node* p)
{
if (p != NULL)
{
cout<<p->data<<endl;
preorder(p->left);
preorder(p->right);
}
}
void postorder(Node* p)
{
if (p != NULL)
{
postorder(p->left);
postorder(p->right);
cout<<p->data<<endl;
}
Data Structures in C++                    7   Binary trees
}
void inorder(Node* p)
{
if (p != NULL)
{
inorder(p->left);
cout<<p->data<<endl;
inorder(p->right);
}
}
void main()
{
Node* tree;
tree = NULL;
tree =insert(tree,11);
tree=insert(tree,5);
tree=insert(tree,3);
tree=insert(tree,12);
tree=insert(tree,7);
cout<<"\nPreorder Traverse:\n";
preorder(tree);
cout<<"\nPostorder Traverse:\n";
postorder(tree);
cout<<"\nInorder Traverse:\n";
inorder(tree);
}

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 1 posted: 3/31/2012 language: pages: 7
How are you planning on using Docstoc?