tailieunhanh - Lecture Data Structures: Lesson 27

Lecture Data Structures: Lesson 27 provide students with knowledge about properties of binary tree; threaded binary tree; the overhead of stack operations during recursive calls can be costly; the same would true if we use a non-recursive but stack-driven traversal procedure; . | Data Structures Lecture 27 Sohail Aslam 1 Properties of Binary Tree Property A binary tree with N internal nodes has 2N links N-1 links to internal nodes and N 1 links to external nodes. 2 Threaded Binary Tree Property A binary tree with N internal nodes has 2N links N-1 links to internal nodes and N 1 links to external nodes. A B C internal link D E F external link G E F Internal links 8 External links 10 3 Properties of Binary Tree Property A binary tree with N internal nodes has 2N links N-1 links to internal nodes and N 1 links to external nodes. In every rooted tree each node except the root has a unique parent. Every link connects a node to its parent so there are N-1 links connecting internal nodes. Similarly each of the N 1 external nodes has one link to its parent. Thus N-1 N 1 2N links. 4 Threaded Binary Trees 5 Threaded Binary Tree In many applications binary tree traversals are carried out repeatedly. The overhead of stack operations during recursive calls can be costly. The same would true if we use a non-recursive but stack-driven traversal procedure It would be useful to modify the tree data structure which represents the binary tree so as to speed up say the inorder traversal process make it quot stack-free quot . 6 Threaded Binary Tree Oddly most of the pointer fields in our representation of binary trees are NULL Since every node except the root is pointed to there are only N-1 non-NULL pointers out of a possible 2N for an N node tree so that N 1 pointers are NULL. 7 Threaded Binary Tree A Internal nodes 9 External nodes 10 B C internal node D E F G E F external node 8 Threaded Binary Tree The threaded tree data structure will replace these NULL pointers with pointers to the inorder successor predecessor of a node as appropriate. We ll need to know whenever formerly NULL pointers have been replaced by non NULL pointers to successor predecessor nodes since otherwise there s no way to distinguish those pointers from the customary pointers to .