Download nodes

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
1
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
2
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
3
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
4
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
5
A - root node
B
C
left child
right child
Leaf nodes
trees.ppt
6
D
E
F
G
I
H
J
trees.ppt
7
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
8
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
9
5
7
8
2
7
5
8
9
0
0
6
9
6
2
Examples of binary search trees
trees.ppt
10
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
11
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
12
TreeNode
struct TreeNode
{
char
data;
NodePtr lchild;
NodePtr rchild;
};
trees.ppt
13
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
14
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
15
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
16
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
}
17
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
18
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
19
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
20
inorder
void InorderTraverse(/* in */ NodePtr ptr)
{
if (ptr != NULL)
{
InOrderTraverse(LChild(ptr));
Visit(ptr);
InOrderTraverse(Rchild(ptr));
}
}
trees.ppt
21
Inorder:0 3 5 6 7 8 9
5
8
3
7
9
0
6
trees.ppt
22
Preordervisit node before subtrees
void PreorderTraverse(/* in */ NodePtr ptr)
{
if (ptr != NULL)
{
Visit(ptr);
PreOrderTraverse(LChild(ptr));
PreOrderTraverse(Rchild(ptr));
}
}
trees.ppt
23
Preorder: 5 3 0 8 7 6 9
5
8
3
7
9
0
6
trees.ppt
24
Postordervisit node after subtrees
void PostorderTraverse(/* in */ NodePtr ptr)
{
if (ptr != NULL)
{
PostOrderTraverse(LChild(ptr));
PostOrderTraverse(Rchild(ptr));
Visit(ptr);
}
}
trees.ppt
25
Postorder: 0 3 6 7 9 8 5
5
8
3
7
9
0
6
trees.ppt
26
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
}
27
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
}
28
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
29
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
30
• Each subtree represents an expression
• evaluate both subtrees before performing
the operation at the root
trees.ppt
31
Examples of binary expression
trees
+
6
6+5
*
5
4
3
4*3
trees.ppt
32
+
/
+
6
8
3
*
2
4
((6*4)+3) + (8/2))
infix: 6 * 4 + 3 + 8/2
prefix: + + * 6 4 3 / 8 2
postfix: 6 4 * 3 + 8 2 / +
trees.ppt
33
• Infix notation
– inorder transversal
– 5+3
• Prefix notation
– preorder transversal
– +62
– consecutive operations performed right to left
• postfix notation - reverse Polish notation(RPN)
– postorder transversal
– 62*
– consecutive operations performed left to right
trees.ppt
34
RPN expression evaluation
WHILE 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
35
RPN evaluation of :
64*3+82/+
Operand stack
6
64
24
24 3
27
27 8
27 8 2
27 4
31
trees.ppt
36
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
trees.ppt
37
Related documents