370 likes | 464 Views
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.
E N D
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
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
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
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
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
A - root node C right child B left child Leaf nodes trees.ppt
D E F G H I J trees.ppt
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
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
5 7 8 2 5 8 7 9 0 0 6 9 6 2 Examples of binary search trees trees.ppt
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
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
TreeNode struct TreeNode { char data; NodePtr lchild; NodePtr rchild; }; trees.ppt
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
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
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
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
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
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
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
inorder void InorderTraverse(/* in */ NodePtr ptr) { if (ptr != NULL) { InOrderTraverse(LChild(ptr)); Visit(ptr); InOrderTraverse(Rchild(ptr)); } } trees.ppt
Inorder:0 3 5 6 7 8 9 5 8 3 7 9 0 6 trees.ppt
Preorder-visit node before subtrees void PreorderTraverse(/* in */ NodePtr ptr) { if (ptr != NULL) { Visit(ptr); PreOrderTraverse(LChild(ptr)); PreOrderTraverse(Rchild(ptr)); } } trees.ppt
Preorder: 5 3 0 8 7 6 9 5 8 3 7 9 0 6 trees.ppt
Postorder-visit node after subtrees void PostorderTraverse(/* in */ NodePtr ptr) { if (ptr != NULL) { PostOrderTraverse(LChild(ptr)); PostOrderTraverse(Rchild(ptr)); Visit(ptr); } } trees.ppt
Postorder: 0 3 6 7 9 8 5 5 8 3 7 9 0 6 trees.ppt
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
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
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
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
Each subtree represents an expression • evaluate both subtrees before performing the operation at the root trees.ppt
Examples of binary expression trees + * 6 5 4 3 6 + 5 4*3 trees.ppt
+ / + 8 2 3 * 4 6 ((6*4)+3) + (8/2))infix: 6 * 4 + 3 + 8/2prefix: + + * 6 4 3 / 8 2postfix: 6 4 * 3 + 8 2 / + trees.ppt
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
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
RPN evaluation of :6 4 * 3 + 8 2 / + Operand stack 6 6 4 24 24 3 27 27 8 27 8 2 27 4 31 trees.ppt
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