二叉樹的遞迴與非遞迴

2021-07-31 16:46:26 字數 3267 閱讀 8878

#include #include #include #include using namespace std;

templatestruct binarytreenode

};template class binarytree

binarytree(t* a, size_t n, const t& invalid)

binarytree(const binarytree& t)

binarytree& operator=( binarytree& t)

~binarytree()

node* createtree(const t* a, size_t n, const t& invalid, size_t& index)

return root;

} //先序遍歷(遞迴法)

void prevorder()

//先序遍歷非遞迴

void prevordernorr( )

node* top = s.top();

s.pop();

cur = top->_right;

} cout << endl;

} //後序遍歷

void postorder()

//後序遍歷非遞迴

void postordernorr()

cur = s.top();

if (null==cur->_right||prev==cur->_right)

else

cur = cur->_right;

}cout << endl;

}

//中序遍歷

void inorder()

//中序遍歷非遞迴

void inordernorr()

node* top = s.top();

s.pop();

cout << top->_data << " ";

cur = top->_right;

} cout << endl;

} //節點個數

size_t size()

//葉子節點個數

size_t leafsize()

//樹的深度

size_t depth()

size_t getklevel(size_t k)

// 查詢

node* find(size_t x)

//層序遍歷

void levelorder()

while (!q.empty())

if (front->_right)

}cout << endl; }

protected:

node* _copy(node* root)

node* newroot = new node(root->_data);

newroot->_left = _copy(root->_left);

newroot->_right = _copy(root->_right);

return newroot;

} void _destroytree(node* root)

_destroytree(root->_left);

_destroytree(root->_right);

delete root;

} void _prevorder(binarytreenode* root)

}void _postorder(binarytreenode* root) }

void _inorder(binarytreenode* root) }

int _size(binarytreenode* root)

return _size(root->_left) + _size(root->_right) + 1;

} int _leafsize(binarytreenode* root)

else if (root->_left == null&&root->_right == null)

return _leafsize(root->_left) + _leafsize(root->_right);

} int _depth(node* root)

int left = _depth(root->_left);

int right = _depth(root->_right);

return left > right ? left + 1 : right + 1;

} int _getklevel(node* root, size_t k)

else if (k==1)

return _getklevel(root->_left, k - 1) + _getklevel(root->_right, k - 1);

} node* _find(node* root, const t& x)

if (root->_data==x)

node* ret = _find(root->_left,x);

if (ret != null)

return ret;

return _find(root->_right, x);

} private:

binarytreenode* _root;

};void testbinarytree()

; binarytreet1(array,sizeof(array)/sizeof(array[0]),'#');

binarytreet2(t1);

binarytreet3;

t3 = t2;

t2.levelorder();

t3.levelorder();

t1.levelorder();

t1.prevorder();

t1.prevordernorr();

t1.inorder();

t1.inordernorr();

t1.postorder();

t1.postordernorr();

cout << endl;

cout << t1.size() << endl;

cout << t1.leafsize() << endl;

cout << t1.depth() << endl;

cout << t1.getklevel(2) << endl;

cout << t1.find(2) << endl;

}

二叉樹 遞迴 非遞迴

include include include include using namespace std typedef struct node bintree typedef struct node1 btnode void creatbintree char s,bintree root 建立二叉...

二叉樹的遞迴與非遞迴遍歷

規則 根左右 遞迴前序遍歷 void preorder bintree root 非遞迴前序遍歷 思路 1 訪問結點p,並將結點p入棧,輸出p節點的值 2 判斷結點p的左孩子是否為空,若為空,則取棧頂結點並進行出棧操作,並將棧頂結點的右孩子置為當前的結點p 若不為空,則將p的左孩子置為當前的結點p ...

二叉樹的遍歷(遞迴與非遞迴)

二叉樹的遞迴遍歷的邏輯較簡單,寫法也簡單,遞迴遍歷左子樹,遞迴遍歷右子樹 對於二叉樹的非遞迴遍歷,我們需要借助stack 棧 這種資料結構來儲存我們每次遍歷到的節點,然後按照一定的順序彈出。二叉樹的節點結構定義為 struct treenode 下面是二叉樹的遞迴遍歷 先序遞迴遍歷 void pre...