Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.

Similar presentations


Presentation on theme: "Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor."— Presentation transcript:

1 trees.ppt1 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

2 trees.ppt2 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

3 trees.ppt3 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

4 trees.ppt4 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

5 trees.ppt5 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

6 trees.ppt6 B left child A - root node C right child Leaf nodes

7 trees.ppt7 D E F G H IJ

8 8 A A BB If the two trees are general trees, they are different drawings of the same tree. As binary trees, they are different trees.

9 trees.ppt9 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

10 trees.ppt10 5 2 0 8 7 6 9 58 9 7 60 2 Examples of binary search trees

11 trees.ppt11 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);

12 trees.ppt12 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(log 2 N)

13 trees.ppt13 TreeNode struct TreeNode { char data; NodePtrlchild; NodePtr rchild; };

14 trees.ppt14 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; };

15 trees.ppt15 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); }

16 trees.ppt16 Inserting Values Apply modified binary search algorithm search algorithm terminates at a leaf - insertion point faster than sorted vectors additional memory for links

17 trees.ppt17 void FindNode (NodePtr tree,ItemType& item, NodePtr& nodePtr, NodePtr& parentPtr) { nodePtr = tree; parentPtr = NULL; Boolean found = FALSE; while (nodePtr != NULL && !found) { if (item data) { parentPtr = nodePtr; nodePtr = nodePtr->lchild; } else if (item > nodePtr->data) { parentPtr = nodePtr; nodePtr = nodePtr->rchild; } else found = TRUE; }

18 trees.ppt18 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 data) parentPtr->lchild = newNode; else parentPtr ->rchild = newNode; }

19 trees.ppt19 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; }

20 trees.ppt20 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

21 trees.ppt21 inorder void InorderTraverse(/* in */ NodePtr ptr) { if (ptr != NULL) { InOrderTraverse(LChild(ptr)); Visit(ptr); InOrderTraverse(Rchild(ptr)); }

22 trees.ppt22 Inorder:0 3 5 6 7 8 9 5 0 8 7 6 9 3

23 trees.ppt23 Preorder- visit node before subtrees void PreorderTraverse(/* in */ NodePtr ptr) { if (ptr != NULL) { Visit(ptr); PreOrderTraverse(LChild(ptr)); PreOrderTraverse(Rchild(ptr)); }

24 trees.ppt24 Preorder: 5 3 0 8 7 6 9 5 0 8 7 6 9 3

25 trees.ppt25 Postorder- visit node after subtrees void PostorderTraverse(/* in */ NodePtr ptr) { if (ptr != NULL) { PostOrderTraverse(LChild(ptr)); PostOrderTraverse(Rchild(ptr)); Visit(ptr); }

26 trees.ppt26 Postorder: 0 3 6 7 9 8 5 5 0 8 7 6 9 3

27 trees.ppt27 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 data) Insert(tree->lchild,item); // insert in left subtree else Insert(tree->rchild,item);// insert in right subtree }

28 trees.ppt28 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 data) Retrieve(tree->lchild,item,found); else if (item > tree->data) Retrieve(tree->rchild,item,found); else { item = tree->data; found = TRUE; }

29 trees.ppt29 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); }

30 trees.ppt30 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

31 trees.ppt31 Each subtree represents an expression evaluate both subtrees before performing the operation at the root

32 trees.ppt32 Examples of binary expression trees + 56 * 34 6 + 54*3

33 trees.ppt33 + + / * 3 6 4 82 (( 6*4)+3) + (8/2)) infix: 6 * 4 + 3 + 8/2 prefix: + + * 6 4 3 / 8 2 postfix: 6 4 * 3 + 8 2 / +

34 trees.ppt34 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

35 trees.ppt35 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; }

36 trees.ppt36 RPN evaluation of : 6 4 * 3 + 8 2 / + Operand stack6 6 4 24 24 3 27 27 8 27 8 2 27 4 31

37 trees.ppt37 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


Download ppt "Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor."

Similar presentations


Ads by Google