Array implementation of a Tree - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Array implementation of a Tree

Description:

set right child. set left child. Constructor for BST. A tree is ... make the left child the new node. else. insert the new data into the left tree ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 18
Provided by: randyl3
Category:

less

Transcript and Presenter's Notes

Title: Array implementation of a Tree


1
Array implementation of a Tree
0
A
A
1
B
2
C
3
1
D
B
C
4
E
5
F
6
0
E
D
F
7
G
8
H
9
0
J
I
G
H
10
0
11
I
12
J
LeftChild Index Parent Index 2 1 RightChild
Index Parent Index2 2 if (Index gt arraySize)
then node is null ParentNode (Index-1)/2 //
integer division
2
An Array Implementation of a Tree
  • The root is stored at location 0
  • The left child is stored at location parent2 1
    if parent21 lt n
  • The right child is stored at location parent22
    if parent22 lt n
  • Parent is (child-1)/2 if 0lt childltn
  • Left sibling is node-1 (even nodes 0ltnodeltn)
  • Right sibling is node1 (odd nodes (node1) lt n)

3
Create the array representation
A
B
C
E
D
F
G
M
L
I
J
K
H
N
O
4
Create the array representation
0
A
2
B
6
C
14
D
30
E
62
F
126
G
5
The array representation is efficient for
complete binary trees
A
B
C
E
D
F
G
I
J
H
6
Binary Search Tree (BST)
  • The left subtree contains values smaller than the
    root
  • The right subtree contains values larger than the
    root
  • Use a BST to store the following data
  • L, Y, N, C, H, B, U, R, G
  • Y, U, R, N, L, H, G, C, B
  • Which traversal method is likely to be most
    useful with a BST?

7
LYNCHBURG
L
C
Y
B
H
N
U
G
R
8
Alphabetized Lynchburg
Y
U
R
N
L
H
G
C
B
9
What operations will be supported by the BST ADT
  • Create a new tree (constructor)
  • Delete an existing tree (destructor)
  • insertData()
  • findData()
  • removeData()
  • PrintPostfix()
  • PrintInfix()
  • PrintPrefix()
  • PrintBreadthFirst()
  • Copy a tree (us a copy constructor)
  • A copy constructor is a constructor that take a
    reference to its own type as an argument. It
    creates a duplicate instance.

10
Implementing a Binary Tree with Pointers(Data
members of TreeNode)
Root
Value
Right Child
Left Child
Value
Right Child
Left Child
Value
Right Child
Left Child
As in the List implementation, a tree is made up
of nodes of a helper class. In this case a tree
will be made up of TreeNodes. The Tree class
will have a root data member that points to the
root TreeNode of the tree.
11
Implement the TreeNode Class
  • Implement a tree node class for integers
  • Implement these functions for tree node
  • constructor
  • destructor
  • get right child
  • get left child
  • set right child
  • set left child

12
Constructor for BST
  • A tree is
  • A pointer to the TreeNode that is the root of the
    tree
  • This pointer is null in the default case
  • We will also support a constructor that has an
    initial value for the root.
  • For the first time we include a copy
    constructor that can initialize a new instance
    of class BST to have the same value as an
    existing class.

13
There will be 3 constructors
  • BinarySearchTree()
  • Create a null rooted tree
  • BinarySearchTree(int data)
  • BinarySearchTree(BinarySearchTree original)

14
InsertData(int newData)
  • currentNode the root
  • insert(TreeNode currentNode)
  • if (newData lt currentNode)
  • if (currentNode-gtgetRightChild 0)
  • make the right child the new node
  • else
  • insert the new data into the
    right tree
  • else
  • if (currentNode-gtgetLeftChild 0)
  • make the left child the new node
  • else
  • insert the new data into the left
    tree

15
Removing a node from a BST
  • If node is a leaf, just delete it
  • If the node has one child, make the grandparent
    of the child the childs parent - delete the
    original parent
  • If the node has two children, replace it with the
    smallest node of the right subtree. The smallest
    node of the right subtree can be easily removed
    as it has at most one child.

16
LYNCHBURG
L
C
Y
B
H
N
U
G
R
17
LYNCHBURG
L
C
Y
B
H
N
U
G
J
R
Exchange G for C
Write a Comment
User Comments (0)
About PowerShow.com