二叉樹的建立和遍歷

2021-08-03 04:17:21 字數 1109 閱讀 4992

#pragma once

#include

#include

#include

#include

#include

using

namespace

std;

template

struct binarytreenode

};template

class binarytree

//構造

binarytree(t* arr, size_t _size, const t& invalid)

//拷貝構造

binarytree(binarytree& x)

//運算子過載:判斷兩個樹是否相等

binarytree& operator=(binarytree& x)

return *this;

}//析構

~binarytree()

//前序遍歷(遞迴)

prevorder()

//中序遍歷(遞迴)

inorder()

//後序遍歷(遞迴)

//層序遍歷

//前序遍歷(非遞迴)

//中序遍歷(非遞迴)

//後序遍歷(非遞迴)

protected:

node* _creattree(const t* arr, size_t size, const t& invalid, size_t index)

return root;

}void distory()

void _distory(node* root)

//node* _copybinarytree(node* root)

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

node->_left = _copybinarytree(root->_left);

node->_right = _copybinarytree(root->_right);

return node;

}protected:

node *_root;

};

關於遍歷和節點查詢明天再寫啦

二叉樹建立和遍歷

二叉樹建立遍歷規則 1.先序 根 左 右 2.中序 左 根 右 3.後序 左 右 根 二叉樹定義和輔助函式如下 struct node void visit int data int indata 先序建立二叉樹 struct node createbitree 先序建立乙個二叉樹 return t...

二叉樹建立和遍歷

include include 帶返回值建立二叉樹 最簡單方法 節點資料結構 struct bs node typedef struct bs node tree tree head,p,root 建立二元查詢樹 有返回值的可以不傳參 沒有的話如何傳參 輸入0代表到了某個葉子節點 tree crea...

二叉樹建立和遍歷

二叉樹建立遍歷規則 1.先序 根 左 右 2.中序 左 根 右 3.後序 左 右 根 二叉樹定義和輔助函式例如以下 struct node void visit int data int indata 先序建立二叉樹 struct node createbitree 先序建立乙個二叉樹 return...