0% found this document useful (0 votes)
97 views27 pages

Binary Trees: Erin Keith

The document discusses binary trees and their implementations using arrays and linked lists. It describes binary tree operations like traversals (preorder, inorder, postorder). It provides pseudocode for an array implementation using array nodes and for a linked list implementation using linked nodes. The linked list implementation includes protected recursive functions for operations like adding nodes and public wrapper functions.

Uploaded by

maya fisher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views27 pages

Binary Trees: Erin Keith

The document discusses binary trees and their implementations using arrays and linked lists. It describes binary tree operations like traversals (preorder, inorder, postorder). It provides pseudocode for an array implementation using array nodes and for a linked list implementation using linked nodes. The linked list implementation includes protected recursive functions for operations like adding nodes and public wrapper functions.

Uploaded by

maya fisher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Binary Trees

ERIN KEITH

16_BINARY_TREE 1
Topics
1. Traversals
2. Array Implementation
3. Linked Implementation

16_BINARY_TREE 2
Trees!

16_BINARY_TREE 3
Binary Tree ADT
Operations
• isEmpty(): boolean
• add(newEntry: ItemType): boolean
• remove(): boolean
• getEntry(): ItemType
• traversals: none
• ways to visit each node

16_BINARY_TREE 4
Traversal Operations
At each node
◦ Display data
◦ Traverse left subtree
◦ Traverse right subtree
But not necessarily in this order!
◦ Preorder
◦ Display data first (before traversals)
◦ Inorder
◦ Display data second (in between traversals)
◦ Postorder
◦ Display data last (after traversals)
16_BINARY_TREE 5
Preorder!

2 5

3 4 6

Elephant, Cat, Bird, Rabbit, Snake, Monkey


16_BINARY_TREE 6
Inorder!

2 6

1 3 5

Bird, Cat, Rabbit, Elephant, Monkey, Snake


16_BINARY_TREE 7
Postorder!

3 6

1 2 5

Bird, Rabbit, Cat, Monkey, Snake, Elephant


16_BINARY_TREE 8
Tree Interface
#ifndef TREE_INTERFACE
#define TREE_INTERFACE

template<class ItemType>
class TreeInterface {
public:
virtual bool isEmpty() const = 0;
virtual int getHeight() const = 0;
virtual int getNumberOfNodes() const = 0;
virtual ItemType getRootData() const = 0;
virtual bool add(const ItemType& newEntry) = 0;
virtual bool remove(const ItemType& anEntry) = 0;
virtual void clear() = 0;
virtual ItemType getEntry(const ItemType& anEntry) const = 0;
virtual bool contains(const ItemType& anEntry) const = 0;
virtual ~TreeInterface() { }
};
#endif

16_BINARY_TREE 9
Array Implementation
Array Trees need a special kind of Node

#ifndef ARRAY_BTREE_NODE
#define ARRAY_BTREE_NODE

template<class ItemType>
class ArrayBTreeNode{
private:
ItemType item; //Data!
int leftChild; //Index to left child
int rightChild; //Index to right child
public:
ArrayBTreeNode();
ArrayBTreeNode(const ItemType& newData, int left, int right);
void setItem(const ItemType& newData);
...
};
#include "ArrayBTreeNode.cpp"
#endif
16_BINARY_TREE 10
Array Implementation

16_BINARY_TREE 11
Array Implementation
#ifndef ARRAY_BTREE
#define ARRAY_BTREE

#include "TreeInterface.h"
#include "ArrayBTreeNode.h"

template<class ItemType>
class ArrayBTree: public TreeInterface<ItemType>{
private:
static const int MAX_NODES = 100;
ArrayBTreeNode<ItemType> tree[MAX_NODES]; //Data!
int root; //Index of root
int free; //Index next free element

16_BINARY_TREE 12
Array Implementation

16_BINARY_TREE 13
Linked Implementation
#ifndef LINKED_BTREE_NODE
#define LINKED_BTREE_NODE

template<class ItemType>
class LinkedBTreeNode{
private:
ItemType item;
LinkedBTreeNode<ItemType>* leftChildPtr;
LinkedBTreeNode<ItemType>* rightChildPtr;

16_BINARY_TREE 14
Linked Implementation
public:
LinkedBTreeNode();
LinkedBTreeNode(const ItemType& newData,
Node<ItemType>* left,
Node<ItemType>* right);
void setItem(const ItemType& newData);
ItemType getItem() const;

bool isLeaf() const;

Node<ItemType>* getLeftChildPtr() const;


Node<ItemType>* getRightChildPtr() const;

void setLeftChildPtr(Node<ItemType>* newLeftChildPtr);


void setRightChildPtr(Node<ItemType>* newRightChildPtr);
};
#include "LinkedBTreeNode.cpp"
#endif

16_BINARY_TREE 15
Linked Implementation
#ifndef LINKED_BTREE
#define LINKED_BTREE

#include "TreeInterface.h"
#include "LinkedBTreeNode.h"

template<class ItemType>
class LinkedBTree: public TreeInterface<ItemType>{
private:
LinkedBTreeNode<ItemType>* rootPtr;

16_BINARY_TREE 16
Linked Implementation
protected:
int getHeightHelper(LinkedBTreeNode<ItemType>* subTreePtr) const;

int getNumberOfNodesHelper(LinkedBTreeNode<ItemType>* subTreePtr) const;

LinkedBTreeNode<ItemType>* balancedAdd(LinkedBTreeNode<ItemType>* subTreePtr,


LinkedBTreeNode<ItemType>* newNodePtr);

LinkedBTreeNode<ItemType>* removeValue(LinkedBTreeNode<ItemType>* subTreePtr,


const ItemType target, bool& isSuccessful);

LinkedBTreeNode<ItemType>* moveValuesUpTree(LinkedBTreeNode<ItemType>*
subTreePtr);

16_BINARY_TREE 17
Linked Implementation
LinkedBTreeNode<ItemType>* findNode(LinkedBTreeNode<ItemType>* treePtr, const
ItemType& target, bool& isSuccessful) const;

LinkedBTreeNode<ItemType>* copyTree(const LinkedBTreeNode<ItemType>*


oldTreeRootPtr) const;

void destroyTree(LinkedBTreeNode<ItemType>* subTreePtr);

void preorder(void visit(ItemType&), LinkedBTreeNode<ItemType>* treePtr)


const;

void inorder(void visit(ItemType&), LinkedBTreeNode<ItemType>* treePtr)


const;

void postorder(void visit(ItemType&), LinkedBTreeNode<ItemType>* treePtr)


const;

16_BINARY_TREE 18
Tree Implementation
Need outer (required) functions to start the recursive (protected)
function call
template<class ItemType>
bool LinkedBTree<ItemType>::add(const ItemType& newData){
LinkedBTreeNode<ItemType>* newNodePtr(newData);

rootPtr = balancedAdd(rootPtr, newNodePtr);

return true;
}

16_BINARY_TREE 19
Linked Implementation
public:
LinkedBTree();

bool isEmpty() const;


int getHeight() const;
int getNumberOfNodes() const;
ItemType getRootData() const;
bool add(const ItemType& newData);
bool remove(const ItemType& data);
void clear();
ItemType getEntry(const ItemType& anEntry) const;
bool contains(const ItemType& anEntry) const;

void preorderTraverse(void visit(ItemType&)) const;


void inorderTraverse(void visit(ItemType&)) const;
void postorderTraverse(void visit(ItemType&)) const;

~LinkedBTree();
};
#include "LinkedBTree.cpp"
16_BINARY_TREE 20
#endif
Tree Implementation
The public tree interface functions are like a wrapper function
that gets called from outside the class.

The protected class function does the actual work using


recursion.
template<class ItemType>
bool LinkedBTree<ItemType>::add(const ItemType& newData){
LinkedBTreeNode<ItemType>* newNodePtr(newData);

rootPtr = balancedAdd(rootPtr, newNodePtr);

return true;
}

16_BINARY_TREE 21
Linked Implementation
#include "LinkedBTree.h"

template<class ItemType>
LinkedBTree<ItemType>::LinkedBTree(){
rootPtr = nullptr;
}

template<class ItemType>
bool LinkedBTree<ItemType>::isEmpty() const{
return rootPtr == nullptr;
}

16_BINARY_TREE 22
Linked Implementation
template<class ItemType>
int LinkedBTree<ItemType>::getHeight() const{
return getHeightHelper(rootPtr);
}

template<class ItemType>
int LinkedBTree<ItemType>::
getHeightHelper(LinkedBTreeNode<ItemType>* subTreePtr) const{
if (subtTreePtr == nullptr){
return 0;
}
else{
return 1 + max(getHeightHelper(subTreePtr->getLeftChildPtr()),
getHeightHelper(subTreePtr->getRightChildPtr()));
}
}

16_BINARY_TREE 23
Linked Implementation
template<class ItemType>
bool LinkedBTree<ItemType>::add(const ItemType& newData){
LinkedBTreeNode<ItemType>* newNodePtr = new LinkedBTreeNode<ItemType>(newData);
rootPtr = balancedAdd(rootPtr, newNodePtr);
return true;
}

16_BINARY_TREE 24
Linked Implementation
template<class ItemType>
LinkedBTreeNode<ItemType>* LinkedBTree<ItemType>::
balancedAdd(LinkedBTreeNode<ItemType>* subTreePtr,
LinkedBTreeNode<ItemType>* newNodePtr){
if (subTreePtr == nullptr)
return newNodePtr;
else{
LinkedBTreeNode<ItemType>* leftPtr = subTreePtr->getLeftChildPtr();
LinkedBTreeNode<ItemType>* rightPtr = subTreePtr->getRightChildPtr();
if (getHeighetHelper(leftPtr) > getHeightHelper(rightPtr)){
rightPtr = balancedAdd(rightPtr, newNodePtr);
subTreePtr->setRightChildPtr(rightPtr);
}
else{
leftPtr = balancedAdd(leftPtr, newNodePtr);
subTreePtr->setLeftChild(leftPtr);
}
return subTreePtr;
}
} 16_BINARY_TREE 25
Binary Search Tree

16_BINARY_TREE 26
Next Class
Binary Search Trees
Textbook:
• Chapters 16
Internet:
•https://sbme-tutorials.github.io/2019/data-structure
s/notes/7_week7.html#operations
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-93-tree-implementations-bina
ry-search-tree.pdf

16_BINARY_TREE 27

You might also like