Upload
avice-ross
View
223
Download
0
Embed Size (px)
Citation preview
trees.ppt 1
Introduction
• Many data structures are linear– unique first component– unique last component– other components have unique predecessor and
successor
• hierarchical– non-linear– each component may have several successors
trees.ppt 2
Trees
• Hierarchy in which each component except top is immediately beneath one other component
• root - single component at the top of a tree• leaves - component having no successors• nodes - trees components• parent - node immediately above(predecessor)• children - nodes directly below it(successor)• ancestor• descendant
trees.ppt 3
General tree
• An empty node is a tree
• A single node is a tree
• The structure formed by taking a node R and one or more separate trees and making R the parent of all roots of the trees is a tree
trees.ppt 4
More tree terminology
• Level of a node– level of root is 1– level of any other node is one more than its
parent
• height or depth of a tree– maximum of the levels of its leaves
trees.ppt 5
Binary Tree
• A node in a binary tree can have at most two children
• the two children of a node have special names: the left child and the right child
• every node of a binary tree has 0, 1, or 2 children
trees.ppt 8
A A
B B
If the two trees are general trees, they are different drawings of the same tree.
As binary trees, they are different trees.
trees.ppt 9
Binary Search Tree
• Specialized binary tree
• no two nodes in the tree contain the same data value
• data is from a data type in which less than or greater than is defined
• the data value of every node in the tree is– greater than any data value in its left subtree– less than any data value in its right subtree
trees.ppt 11
Searching a binary search tree
probePtr = binSrchTree.Root();
while(probePtr != NULL && Data(probePtr) != key)
if (key < Data(probePtr)
probePtr = Lchild(probePtr);
else
probePtr = Rchild(probePtr);
trees.ppt 12
Efficiency
• Maximum number of loop iterations equals the height of the tree
• degenerate binary tree - every node except the single leaf node has exactly one child– linear search
• full binary tree • balanced - most nodes have two children
– O(log2N)
trees.ppt 14
struct TreeNode;
typedef TreeNode* NodePtr;
class TreeType
{
public:
TreeType(); // creates empty tree
~TreeType(); // destructor
TreeType(const TreeType& originalTree);
bool IsEmpty() const;
int NumberOfNodes() const;
void RetrieveItem(ItemType& item, bool& found);
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
void PrintTree() const;
private:
NodePtr rootPtr;
};
trees.ppt 15
TreeType::TreeType()
{
rootPtr = NULL;
}
TreeType::~TreeType()
{
Destroy(rootPtr);
}
void Destroy(NodePtr& tree)
{
if (tree != NULL)
{
Destroy(tree->lchild);
Destroy(tree->rchild);
delete tree;
}
}
bool TreeType::IsEmpty() const
{
return (rootPtr == NULL);
}
trees.ppt 16
Inserting Values
• Apply modified binary search algorithm
• search algorithm terminates at a leaf - insertion point
• faster than sorted vectors
• additional memory for links
trees.ppt 17
void FindNode (NodePtr tree,ItemType& item, NodePtr& nodePtr, NodePtr& parentPtr)
{
nodePtr = tree;
parentPtr = NULL;
Boolean found = FALSE;
while (nodePtr != NULL && !found)
{
if (item < nodePtr->data)
{
parentPtr = nodePtr;
nodePtr = nodePtr->lchild;
}
else if (item > nodePtr->data)
{
parentPtr = nodePtr;
nodePtr = nodePtr->rchild;
}
else
found = TRUE;
}
}
trees.ppt 18
void TreeType::InsertItem(ItemType item)
{
NodePtr newNode;
NodePtr nodePtr;
NodePtr parentPtr;
newNode = new TreeNode;
newNode->data = item;
newNode->rchild = NULL;
newNode ->lchild = NULL;
FindNode(root,item,nodePtr,parentPtr);
if (parentPtr == NULL) // insert as root
root = newNode;
else if (item < parentPtr->data)
parentPtr->lchild = newNode;
else
parentPtr ->rchild = newNode; }
trees.ppt 19
int TreeType::NumberOfNodes() const
{
return CountNodes(rootPtr);
}
int CountNodes(NodePtr tree)
{
if (tree == NULL)
return 0;
else
return CountNodes(tree->lchild) + CountNodes(tree->rchild) + 1;
}
trees.ppt 20
Binary Tree Traversal• Tree traversal algorithm - algorithm for processing
or visiting every node• Inorder traversal
– visit all node is the left subtree of R,visit node R,visit all nodes in right subtree of R
• Postorder traversal– visit all node is the left subtree of R, visit all nodes in
right subtree of R, visit node R,
• Preorder traversal– visit node R, visit all node is the left subtree of R,visit
all nodes in right subtree of R
trees.ppt 21
inorder
void InorderTraverse(/* in */ NodePtr ptr)
{
if (ptr != NULL)
{
InOrderTraverse(LChild(ptr));
Visit(ptr);
InOrderTraverse(Rchild(ptr));
}
}
trees.ppt 23
Preorder-visit node before subtrees
void PreorderTraverse(/* in */ NodePtr ptr)
{
if (ptr != NULL)
{
Visit(ptr);
PreOrderTraverse(LChild(ptr));
PreOrderTraverse(Rchild(ptr));
}
}
trees.ppt 25
Postorder-visit node after subtrees
void PostorderTraverse(/* in */ NodePtr ptr)
{
if (ptr != NULL)
{
PostOrderTraverse(LChild(ptr));
PostOrderTraverse(Rchild(ptr));
Visit(ptr);
}
}
trees.ppt 27
Recursive versions:
void TreeType::InsertItem(ItemType item)
{
Insert(rootPtr,item);
}
void Insert(NodePtr& tree,ItemType item)
{
if (tree == NULL)
{ // insertion point found
tree = new TreeNode;
tree->rchild = NULL;
tree->lchild = NULL;
tree->data = item;
}
else if (item < tree->data)
Insert(tree->lchild,item); // insert in left subtree
else
Insert(tree->rchild,item);// insert in right subtree
}
trees.ppt 28
void TreeType::RetrieveItem(NodePtr tree,ItemType& item, bool& found)
{
Retrieve(root,item,found);
}
void Retrieve(NodePtr tree,ItemType& item, bool& found)
{
if (tree == NULL)
found = FALSE;
else if (item < tree->data)
Retrieve(tree->lchild,item,found);
else if (item > tree->data)
Retrieve(tree->rchild,item,found);
else
{
item = tree->data;
found = TRUE;
}
}
trees.ppt 29
void TreeType::TreeType (const TreeType& original Tree)
{
CopyTree(root,originalTree.root);
}
void CopyTree(NodePtr& copy,const NodeType originalTree)
{
if (originalTree == NULL)
copy = NULL;
else
{
copy = new TreeNode;
copy -> info = originalTree->Info;
CopyTree(copy->left,originalTree->left);
CopyTree(copy->right,originalTree->right);
}
}
trees.ppt 30
Binary Expression Tree
1. Each leaf node contains a single operand, and each nonleaf node contains a single operator
2. The left and right subtrees of an operator node represent the subexpressions that must be evaluated before applying the operator at the root of the subtree
trees.ppt 31
• Each subtree represents an expression
• evaluate both subtrees before performing the operation at the root
trees.ppt 33
+
+ /
* 3
6 4
8 2
((6*4)+3) + (8/2))infix: 6 * 4 + 3 + 8/2
prefix: + + * 6 4 3 / 8 2postfix: 6 4 * 3 + 8 2 / +
trees.ppt 34
• Infix notation– inorder transversal– 5 + 3
• Prefix notation– preorder transversal– + 6 2– consecutive operations performed right to left
• postfix notation - reverse Polish notation(RPN)– postorder transversal– 6 2 *– consecutive operations performed left to right
trees.ppt 35
RPN expression evaluationWHILE more tokens exist in RPN expression{ thisToken = next token in RPN expression; IF thisToken is an operand THEN Push thisToken onto operand stack; ELSE { Pop the two top values from operand stack;
Using these two values as operands, perform operation; Push the result onto operand stack; }
trees.ppt 37
semantics
• Mathematics define precedence rules• programming languages
– some have defined precedence rules, others use left to right or right to left
– most have associativity rules defined– in most parentheses override defaults
• postfix and prefix notation– no precedence rules or associative rules needed