Professional Documents
Culture Documents
Data Structures: Dr. Abubaker M. F. Abushofa Department of Electrical & Electronic Engineering, Faculty of Engineering
Data Structures: Dr. Abubaker M. F. Abushofa Department of Electrical & Electronic Engineering, Faculty of Engineering
Data Structures: Dr. Abubaker M. F. Abushofa Department of Electrical & Electronic Engineering, Faculty of Engineering
ly
Data Structures
Data Structures EE432
Fall 2018 - 2019
Level 3
G H I J
Parents: A, B, C, D, F Leaves: E, G, H, I, J
Children: B, C, D, E, F, G, H, I, J Internal Nodes: B, C, D, F
Siblings: {B, C}, {D, E, F}, {G, H}, {I, J}
EE432 Dr. Abubaker Abushofa 7/35
http://www.abushofa.net.ly
Tree Representation
A
Subtree
C D G H I
Binary Trees
• A binary tree is a tree in which no node can
have more than two subtrees. In other words,
Binary Trees
A
C D F
Tree Properties
The height of binary trees can be mathematically
predicted. Given that we need to store N nodes in a
binary tree, the maximum height, Hmax, is:
J
E
J
E
F
EE432 Dr. Abubaker Abushofa 17/53
http://www.abushofa.net.ly
J
E T
F
EE432 Dr. Abubaker Abushofa 18/53
http://www.abushofa.net.ly
J
E T
A F
EE432 Dr. Abubaker Abushofa 19/53
http://www.abushofa.net.ly
Depth-First Traversal
Given that a binary tree consists of a root, a
left subtree, and a right subtree, we can
Depth-First Traversal
Depth-First Traversal
A
C D F
Preorder Traversal
Process Order A B C D E F
“Walking” Order
B E
C D F
Inorder Traversal
Process Order C B D A E F
B E
C D F
Postorder Traversal
Process Order C D B F E A
“Walking” Order
B E
C D F
Tree Traversal
Breadth Traversal
• In the breadth-first traversal of a binary tree,
we process all of the children of a node before
Specification (TreeType.h)
#include <iostream>
struct TreeNode;
Specification (TreeType.h)
bool IsFull() const;
int LengthIs() const;
void SearchForItem(char& item, bool& found) const;
Structure
#include “TreeType.h”
struct TreeNode
CountNodes(tree)
int TreeType::LengthIs() const
{
return CountNodes(root);
Function GetPredecessor
void GetPredecessor(TreeNode* tree, char& data)
{
while (tree->right != NULL)
Function Print
• Definition: Prints the items in the binary
search tree in order from smallest to largest.
TreeType::~TreeType()
{
Destroy(root);
}
Copying a Tree
TreeType::TreeType(const TreeType& originalTree)
{
CopyTree(root, originalTree.root);
Copying a Tree
void CopyTree(TreeNode*& copy, const TreeNode*
originalTree)
{
Implementation (example)
int main()
{
TreeType tree1, tree2;
char chr;
Any question?
Department of Electrical and Electronic Engineering
Data Structures EE432
Fall 2018 - 2019